📜  数据结构示例-在循环链表中搜索元素

📅  最后修改于: 2020-10-15 06:22:43             🧑  作者: Mango

问:程序搜索循环链表中的元素。

说明

在此程序中,我们创建一个循环链接列表并在列表中搜索一个节点。

9-> 5-> 2-> 7-> 3

考虑上面的例子。假设我们需要搜索节点5。要解决此问题,我们将遍历列表并将每个节点与5进行比较。如果找到匹配项,则将标志设置为true,并打印出节点5的位置。在此示例中,节点5位于位置2。

算法

  • 定义一个Node类,该类代表列表中的一个节点。它具有两个属性数据,下一个将指向下一个节点。
  • 定义另一个用于创建循环链表的类,它具有两个节点:head和tail。
  • search()将在列表中搜索一个节点:
    1. 变量i将跟踪搜索到的节点的位置。
    2. 变量标志将存储布尔值false。
    3. 电流将指向头节点。
    4. 通过将电流增加到current.next并将i增加到i + 1来遍历循环。
    5. 将每个节点的数据与搜索到的节点进行比较。如果找到匹配项,则将标志设置为true。
    6. 如果flag为true,则打印搜索到的节点的位置。
    7. 否则,print消息“列表中不存在元素”。

示例:

Python

#Represents the node of list.
class Node:
    def __init__(self,data):
        self.data = data;
        self.next = None;
 
class CreateList:
    #Declaring head and tail pointer as null.
    def __init__(self):
        self.head = Node(None);
        self.tail = Node(None);
        self.head.next = self.tail;
        self.tail.next = self.head;
    
    #This function will add the new node at the end of the list.
    def add(self,data):
        newNode = Node(data);
        #Checks if the list is empty.
        if self.head.data is None:
            #If list is empty, both head and tail would point to new node.
            self.head = newNode;
            self.tail = newNode;
            newNode.next = self.head;
        else:
            #tail will point to new node.
            self.tail.next = newNode;
            #New node will become new tail.
            self.tail = newNode;
            #Since, it is circular linked list tail will point to head.
            self.tail.next = self.head;
            
    #Searches for a node in the list
    def search(self,element):
        current = self.head;
        i = 1;
        flag = False;
        #Checks whether list is empty
        if(self.head == None):
            print("List is empty");
        else:
            while(True): 
                #Compares element to be found with each node present in the list
                if(current.data ==  element):
                    flag = True;
                    break;
                current = current.next;
                i = i + 1;
                if(current == self.head):
                    break;
            if(flag):
                print("Element is present in the list at the position :  " + str(i));
            else:
                print("Element is not present in the list");
 
class CircularLinkedList:
    cl = CreateList();
    #Adds data to the list
    cl.add(1);
    cl.add(2);
    cl.add(3);
    cl.add(4);
    #Search for node 2 in the list
    cl.search(2);
    #Search for node in the list
    cl.search(7);

输出:

Element is present in the list at the position : 2
Element is not present in the list

C

#include 
#include 
#include 
#include  
 
//Represents the node of list.
struct node{
    int data;
    struct node *next;
};
 
//Declaring head and tail pointer as null.
struct node *head = NULL;
struct node *tail = NULL;
 
//This function will add the new node at the end of the list.
void add(int data){
    //Create new node
    struct node *newNode = (struct node*)malloc(sizeof(struct node));
    newNode->data = data;
    //Checks if the list is empty.
    if(head == NULL){
        //If list is empty, both head and tail would point to new node.
        head = newNode;
        tail = newNode;
        newNode->next = head;
    }else {
        //tail will point to new node.
        tail->next = newNode;
        //New node will become new tail.
        tail = newNode;
        //Since, it is circular linked list tail will point to head.
        tail->next = head;
    }
}
 
//Searches for a node in the list
void search(int element) {
    struct node *current = head;
    int i = 1;
    bool flag = false;
    //Checks whether list is empty
    if(head == NULL) {
        printf("List is empty");
    }
    else {
         do{
             //Compares element to be found with each node present in the list
            if(current->data ==  element) {
                flag = true;
                break;
            }
            current = current->next;
            i++;
        }while(current != head);
         if(flag)
            printf("Element is present in the list at the position : %d", i);
        else
            printf("\nElement is not present in the list");
    }
}
    
int main()
{
    //Adds data to the list
   add(1);
   add(2);
   add(3);
   add(4);
   //Search for node 2 in the list
   search(2);
   //Search for node in the list
   search(7);
   
   return 0;
}

输出:

Element is present in the list at the position : 2
Element is not present in the list

JAVA

public class SearchNode {
    //Represents the node of list.
    public class Node{
        int data;
        Node next;
        public Node(int data) {
            this.data = data;
        }
    }
    
    //Declaring head and tail pointer as null.
    public Node head = null;
    public Node tail = null;
    
    //This function will add the new node at the end of the list.
    public void add(int data){
        //Create new node
        Node newNode = new Node(data);
        //Checks if the list is empty.
        if(head == null) {
             //If list is empty, both head and tail would point to new node.
            head = newNode;
            tail = newNode;
            newNode.next = head;
        }
        else {
            //tail will point to new node.
            tail.next = newNode;
            //New node will become new tail.
            tail = newNode;
            //Since, it is circular linked list tail will point to head.
            tail.next = head;
        }
    }
    
    //Searches for a node in the list
    public void search(int element) {
        Node current = head;
        int i = 1;
        boolean flag = false;
        //Checks whether list is empty
        if(head == null) {
            System.out.println("List is empty");
        }
        else {
             do{
                 //Compares element to be found with each node present in the list
                if(current.data ==  element) {
                    flag = true;
                    break;
                }
                current = current.next;
                i++;
            }while(current != head);
             if(flag)
                 System.out.println("Element is present in the list at the position : " + i);
            else
                 System.out.println("Element is not present in the list");
        }
    }
    
    public static void main(String[] args) {
        SearchNode cl = new SearchNode();
        //Adds data to the list
        cl.add(1);
        cl.add(2);
        cl.add(3);
        cl.add(4);
        //Search for node 2 in the list
        cl.search(2);
        //Search for node in the list
        cl.search(7);
    }
}

输出:

Element is present in the list at the position : 2
Element is not present in the list

C#

 using System; 
namespace CircularLinkedList 
{                     
    public class Program
    {
        //Represents the node of list.
        public class Node{
            public T data;
            public Node next;
            public Node(T value) {
                data = value;
                next = null;
            }
        }
        
        public class CreateList{
            protected Node head = null;             
             protected Node tail = null;
            
            //This function will add the new node at the end of the list.
            public void add(T data){
                //Create new node
                Node newNode = new Node(data);
                //Checks if the list is empty.
                if(head == null){
                    head = newNode;
                    tail = newNode;
                    newNode.next = head;
                }else{
                    //tail will point to new node.
                    tail.next = newNode;
                    //New node will become new tail.
                    tail = newNode;
                    //Since, it is circular linked list tail will point to head.
                    tail.next = head;
                }
            }
        
            //Searches for a node in the list
            public void search(T element) {
                Node current = head;
                int i = 1;
                bool flag = false;
                //Checks whether list is empty
                if(head == null) {
                    Console.WriteLine("List is empty");
                }
                else {
                     do{
                         //Compares element to be found with each node present in the list
                        if(current.data.Equals(element)) {
                            flag = true;
                            break;
                        }
                        current = current.next;
                        i++;
                    }while(current != head);
                     if(flag)
                         Console.WriteLine("Element is present in the list at the position : " + i);
                    else
                         Console.WriteLine("Element is not present in the list");
                }
            }
        }
        
        public static void Main()
        {
            
        CreateList cl = new CreateList();
        //Adds data to the list
        cl.add(1);
        cl.add(2);
        cl.add(3);
        cl.add(4);
        //Search for node 2 in the list
        cl.search(2);
        //Search for node in the list
        cl.search(7);
        }    
    }
}

输出:

Element is present in the list at the position : 2
Element is not present in the list

PHP:




data = $data;
        $this->next = NULL;
    }
}
class CreateList{
    //Declaring head and tail pointer as null.
    private $head;
    private $tail;
    function __construct(){
        $this->head = NULL;
        $this->tail = NULL;
    }
    //This function will add the new node at the end of the list.
    function add($data){
        //Create new node
        $newNode = new Node($data);
        //Checks if the list is empty.
        if($this->head == NULL){
            //If list is empty, both head and tail would point to new node.
            $this->head = $newNode;
            $this->tail = $newNode;
            $newNode->next = $this->head;
        }
        else{
            //tail will point to new node.
            $this->tail->next = $newNode;
            //New node will become new tail.
            $this->tail = $newNode;
            //Since, it is circular linked list tail will point to head.
            $this->tail->next = $this->head;
        }
    }
    
    //Searches for a node in the list
    function search($element) {
        $current = $this->head;
        $i = 1;
        $flag = false;
        //Checks whether list is empty
        if($this->head == NULL) {
            echo "List is empty";
        }
        else {
             do{
                 //Compares element to be found with each node present in the list
                if($current->data == $element) {
                    $flag = true;
                    break;
                }
                $current = $current->next;
                $i++;
            }while($current != $this->head);
             if($flag)
                 echo "Element is present in the list at the position : $i 
"; else echo "Element is not present in the list
"; } } } $cl = new CreateList(); //Adds data to the list $cl->add(1); $cl->add(2); $cl->add(3); $cl->add(4); //Search for node 2 in the list $cl->search(2); //Search for node in the list $cl->search(7); ?>

输出:

Element is present in the list at the position : 2
Element is not present in the list