📌  相关文章
📜  数据结构示例-判断单向链表是否有回文

📅  最后修改于: 2020-10-15 04:48:38             🧑  作者: Mango

确定单个链接列表是否为回文式的程序

说明

在此程序中,我们需要检查给定的单链表是否是回文。回文列表是与之相反的列表。

上图中给出的列表是回文,因为它等同于其反向列表,即1、2、3、2、1。要检查列表是否是回文,我们遍历该列表并检查是否有来自起始部分与结束部分中的任何元素都不匹配,然后将变量标志设置为false并中断循环。

最后,如果标志为假,则列表为回文,否则为非。下面给出检查列表是否为回文的算法。

算法

  • 创建一个具有两个属性的类Node:data和next。下一个是指向列表中下一个节点的指针。
  • 创建另一个具有三个属性的回文类:头,尾和大小。
  • addNode()将向列表添加一个新节点:
    1. 创建一个新节点。
    2. 它首先检查head是否等于null,这意味着列表为空。
    3. 如果列表为空,则头和尾都将指向新添加的节点。
    4. 如果列表不为空,则新节点将被添加到列表的末尾,使得尾部的下一个将指向新添加的节点。这个新节点将成为列表的新尾部。
  • reverseList()将颠倒列表中存在的节点的顺序:
    1. 当前节点将表示需要从其反向列表的节点。
    2. 节点prevNode表示当前节点的前一个节点,nextNode表示当前节点的下一个节点。
    3. 通过将每个节点的prevNode与nextNode交换,可以反转该列表。
  • isPalindrome()将检查给定列表是否为回文:
    1. 声明一个节点电流,该电流最初将指向头节点。
    2. 变量标志将存储布尔值true。
    3. 通过将列表的大小除以2计算列表的中点。
    4. 遍历列表,直到当前指向中间节点。
    5. 使用reverseList()反转中间节点之后的列表,直到最后一个节点。该列表将在列表的后半部分。
    6. 现在,比较列表上半部分和下半部分的节点。
    7. 如果任何一个节点都不匹配,则将标志设置为false并中断循环。
    8. 如果该标志在循环之后为真,则表明该列表是回文。
    9. 如果该标志为假,则该列表不是回文。
  • display()将显示列表中存在的节点:
    1. 定义一个节点电流,该电流将初始指向列表的开头。
    2. 遍历列表,直到当前指向null。
    3. 通过在每次迭代中使电流指向其旁边的节点来显示每个节点。

示例:

Python

#Represent a node of the singly linked list
class Node:
    def __init__(self,data):
        self.data = data;
        self.next = None;
        
class Palindrome:
    #Represent the head and tail of the singly linked list
    def __init__(self):
        self.head = None;
        self.tail = None;
        self.size = 0;
        
    #addNode() will add a new node to the list
    def addNode(self, data):
        #Create a new node
        newNode = Node(data);
        
        #Checks if the list is empty
        if(self.head == None):
            #If list is empty, both head and tail will point to new node
            self.head = newNode;
            self.tail = newNode;
        else:
            #newNode will be added after tail such that tail's next will point to newNode
            self.tail.next = newNode;
            #newNode will become new tail of the list
            self.tail = newNode;
        #Size will count the number of nodes present in the list
        self.size = self.size + 1;
        
    #reverseList() will reverse the singly linked list and return the head of the list
    def reverseList(self, temp):
        current = temp;
        prevNode = None;
        nextNode = None;
        
        #Swap the previous and next nodes of each node to reverse the direction of the list
        while(current != None):
            nextNode = current.next;
            current.next = prevNode;
            prevNode = current;
            current = nextNode;
        return prevNode;
        
    #isPalindrome() will determine whether given list is palindrome or not.
    def isPalindrome(self):
        current = self.head;
        flag = True;
        
        #Store the mid position of the list
        mid = (self.size//2) if(self.size%2 == 0) else ((self.size+1)//2);
        
        #Finds the middle node in given singly linked list
        for i in range(1, mid):
            current = current.next;
            
        #Reverse the list after middle node to end
        revHead = self.reverseList(current.next);
        
        #Compare nodes of first half and second half of list
        while(self.head != None and revHead != None):
            if(self.head.data != revHead.data):
                flag = False;
                break;
                
            self.head = self.head.next;
            revHead = revHead.next;
            
        if(flag):
            print("Given singly linked list is a palindrome");
        else:
            print("Given singly linked list is not a palindrome");
            
    #display() will display all the nodes present in the list
    def display(self):
        #Node current will point to head
        current = self.head;
        
        if(self.head == None):
            print("List is empty");
            return;
        print("Nodes of singly linked list: ");
        while(current != None):
            #Prints each node by incrementing pointer
            print(current.data , end=" ");
            current = current.next;
        print();
 
sList = Palindrome();
 
#Add nodes to the list
sList.addNode(1);
sList.addNode(2);
sList.addNode(3);
sList.addNode(2);
sList.addNode(1);
 
sList.display();
 
#Checks whether given list is palindrome or not
sList.isPalindrome();

输出:

 Nodes of the singly linked list:
1 2 3 2 1 
Given singly linked list is a palindrome

C

#include 
#include 
 
//Represent a node of the singly linked list
struct node{
    int data;
    struct node *next;
};    
 
//Represent the head and tail of the singly linked list
struct node *head, *tail = NULL;
int size = 0;
 
//addNode() will add a new node to the list
void addNode(int data) {
    //Create a new node
    struct node *newNode = (struct node*)malloc(sizeof(struct node));
    newNode->data = data;
    newNode->next = NULL;
    
    //Checks if the list is empty
    if(head == NULL) {
        //If list is empty, both head and tail will point to new node
        head = newNode;
        tail = newNode;
    }
    else {
        //newNode will be added after tail such that tail's next will point to newNode
        tail->next = newNode;
        //newNode will become new tail of the list
        tail = newNode;
    }
    //Size will count the number of nodes present in the list
    size++;
}
 
//reverseList() will reverse the singly linked list and return the head of the list
struct node* reverseList(struct node *temp){
    struct node *current = temp;
    struct node *prevNode = NULL, *nextNode = NULL;
    
   //Swap the previous and next nodes of each node to reverse the direction of the list
    while(current != NULL){
        nextNode = current->next;
        current->next = prevNode;
        prevNode = current;
        current = nextNode;
    }
    return prevNode;
}
 
//isPalindrome() will determine whether given list is palindrome or not.
void isPalindrome(){
    struct node *current = head;
    bool flag = true;
    
    //Store the mid position of the list
    int mid = (size%2 == 0)? (size/2) : ((size+1)/2);
    
    //Finds the middle node in given singly linked list
    for(int i=1; inext;
    }
    
    //Reverse the list after middle node to end
    struct node *revHead = reverseList(current->next);
 
    //Compare nodes of first half and second half of list
    while(head != NULL && revHead != NULL){
        if(head->data != revHead->data){
            flag = false;
            break;
        }
        head = head->next;
        revHead = revHead->next;
    }
 
    if(flag)
        printf("Given singly linked list is a palindrome\n");
    else
        printf("Given singly linked list is not a palindrome\n");
}
    
//display() will display all the nodes present in the list
void display() {
    //Node current will point to head
    struct node *current = head;
    
    if(head == NULL) {
        printf("List is empty\n");
        return;
    }
    printf("Nodes of singly linked list: \n");
    while(current != NULL) {
        //Prints each node by incrementing pointer
        printf("%d ", current->data);
        current = current->next;
    }
    printf("\n");
}
    
int main()
{
    //Add nodes to the list
    addNode(1);
    addNode(2);
    addNode(3);
    addNode(2);
    addNode(1);
    
    display();
    
    //Checks whether given list is palindrome or not
    isPalindrome();
    
    return 0;
}

输出:

Nodes of the singly linked list:
1 2 3 2 1 
Given singly linked list is a palindrome

JAVA

public class Palindrome {
    
    //Represent a node of the singly linked list
    class Node{
        int data;
        Node next;
        
        public Node(int data) {
            this.data = data;
            this.next = null;
        }
    }
 
    public int size;
    //Represent the head and tail of the singly linked list
    public Node head = null;
    public Node tail = null;
    
    //addNode() will add a new node to the list
    public void addNode(int data) {
        //Create a new node
        Node newNode = new Node(data);
        
        //Checks if the list is empty
        if(head == null) {
            //If list is empty, both head and tail will point to new node
            head = newNode;
            tail = newNode;
        }
        else {
            //newNode will be added after tail such that tail's next will point to newNode
            tail.next = newNode;
            //newNode will become new tail of the list
            tail = newNode;
        }
        //Size will count the number of nodes present in the list
        size++;
    }
    
    //reverseList() will reverse the singly linked list and return the head of the list
    public Node reverseList(Node temp){
        Node current = temp;
        Node prevNode = null, nextNode = null;
        
       //Swap the previous and next nodes of each node to reverse the direction of the list
        while(current != null){
            nextNode = current.next;
            current.next = prevNode;
            prevNode = current;
            current = nextNode;
        }
        return prevNode;
    }
    
    //isPalindrome() will determine whether given list is palindrome or not.
    public void isPalindrome(){
        Node current = head;
        boolean flag = true;
        
        //Store the mid position of the list
        int mid = (size%2 == 0)? (size/2) : ((size+1)/2);
        
        //Finds the middle node in given singly linked list
        for(int i=1; i

输出:

Nodes of singly linked list: 
1 2 3 2 1 
Given singly linked list is a palindrome

C#

using System;
                    
public class CreateList
{
    //Represent a node of the singly linked list
    public class Node{
        public T data;
        public Node next;
        
        public Node(T value) {
            data = value;
            next = null;
        }
    }
        
    public class Palindrome{
        //Represent the head and tail of the singly linked list
        public Node head = null;             
         public Node tail = null;
        public int size;
        
        //addNode() will add a new node to the list
        public void addNode(T data) {
            //Create a new node
            Node newNode = new Node(data);
 
            //Checks if the list is empty
            if(head == null) {
                //If list is empty, both head and tail will point to new node
                head = newNode;
                tail = newNode;
            }
            else {
                //newNode will be added after tail such that tail's next will point to newNode
                tail.next = newNode;
                //newNode will become new tail of the list
                tail = newNode;
            }
            //Size will count the number of nodes present in the list
            size++;
        }
        //reverseList() will reverse the singly linked list and return the head of the list
        public Node reverseList(Node temp){
            Node current = temp;
            Node prevNode = null, nextNode = null;
 
           //Swap the previous and next nodes of each node to reverse the direction of the list
            while(current != null){
                nextNode = current.next;
                current.next = prevNode;
                prevNode = current;
                current = nextNode;
            }
            return prevNode;
        }
 
        //isPalindrome() will determine whether given list is palindrome or not.
        public void isPalindrome(){
            Node current=head;
            Boolean flag = true;
 
            //Store the mid position of the list
            int mid = (size%2 == 0)? (size/2) : ((size+1)/2);
 
            //Finds the middle node in given singly linked list
            for(int i=1; i revHead = reverseList(current.next);
 
            //Compare nodes of first half and second half of list
            while(head != null && revHead != null){
                if(!(head.data.Equals(revHead.data))){
                    flag = false;
                    break;
                }
                head = head.next;
                revHead = revHead.next;
            }
 
            if(flag)
                Console.WriteLine("Given singly linked list is a palindrome");
            else
                Console.WriteLine("Given singly linked list is not a palindrome");
        }
        
        //display() will display all the nodes present in the list
        public void display() {
            //Node current will point to head
            Node current = head;
            
            if(head == null) {
                Console.WriteLine("List is empty");
                return;
            }
            Console.WriteLine("Nodes of singly linked list: ");
            while(current != null) {
                //Prints each node by incrementing pointer
                Console.Write(current.data + " ");
                current = current.next;
            }
            Console.WriteLine();
        }
    }
    
    public static void Main()
    {
        Palindrome sList = new Palindrome();
        
        //Add nodes to the list
        sList.addNode(1);
        sList.addNode(2);
        sList.addNode(3);
        sList.addNode(2);
        sList.addNode(1);
        
        sList.display();
        
        //Checks whether given list is palindrome or not
        sList.isPalindrome();    
    }
}           

输出:

Nodes of singly linked list: 
1 2 3 2 1 
Given singly linked list is a palindrome

PHP:




data = $data;
        $this->next = NULL;
    }
}
class Palindrome{
    //Represent the head and tail of the singly linked list
    public $head;
    public $tail;
    function __construct(){
        $this->head = NULL;
        $this->tail = NULL;
        $this->size = 0;
    }
    
    //addNode() will add a new node to the list
    function addNode($data) {
        //Create a new node
        $newNode = new Node($data);
        
        //Checks if the list is empty
        if($this->head == NULL) {
            //If list is empty, both head and tail will point to new node
            $this->head = $newNode;
            $this->tail = $newNode;
        }
        else {
            //newNode will be added after tail such that tail's next will point to newNode
            $this->tail->next = $newNode;
            //newNode will become new tail of the list
            $this->tail = $newNode;
        }
        //Size will count the number of nodes present in the list
        $this->size++;
    }
    
    //reverseList() will reverse the singly linked list and return the head of the list
    function reverseList($temp){
        $current = $temp;
        $prevNode = null;
        $nextNode = null;
        
       //Swap the previous and next nodes of each node to reverse the direction of the list
        while($current != null){
            $nextNode = $current->next;
            $current->next = $prevNode;
            $prevNode = $current;
            $current = $nextNode;
        }
        return $prevNode;
    }
    
    //isPalindrome() will determine whether given list is palindrome or not.
    function isPalindrome(){
        $current = $this->head;
        $flag = true;
        
        //Store the mid position of the list
        $mid = ($this->size%2 == 0)? ($this->size/2) : (($this->size+1)/2);
        
        //Finds the middle node in given singly linked list
        for($i=1; $i<$mid; $i++){
            $current = $current->next;
        }
        
        //Reverse the list after middle node to end
        $revHead = $this->reverseList($current->next);
 
        //Compare nodes of first half and second half of list
        while($this->head != null && $revHead != null){
            if($this->head->data != $revHead->data){
                $flag = false;
                break;
            }
            $this->head = $this->head->next;
            $revHead = $revHead->next;
        }
 
        if($flag)
            print("Given singly linked list is a palindrome 
"); else print("Given singly linked list is not a palindrome
"); } //display() will display all the nodes present in the list function display() { //Node current will point to head $current = $this->head; if($this->head == NULL) { print("List is empty
"); return; } print("Nodes of singly linked list:
"); while($current != NULL) { //Prints each node by incrementing pointer print($current->data . " "); $current = $current->next; } print("
"); } } $sList = new Palindrome(); //Add nodes to the list $sList->addNode(1); $sList->addNode(2); $sList->addNode(3); $sList->addNode(2); $sList->addNode(1); $sList->display(); //Checks whether given list is palindrome or not $sList->isPalindrome(); ?>

输出:

 Nodes of singly linked list: 
1 2 3 2 1 
Given singly linked list is a palindrome