📌  相关文章
📜  数据结构示例-单向链表中搜索元素

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

程序以搜索单链列表中的元素

说明

在此程序中,我们需要在给定的单链列表中搜索一个节点。

为了解决这个问题,我们将使用节点电流遍历列表。当前指向头部并开始将搜索到的节点数据与当前节点数据进行比较。如果它们相等,则将标志设置为true并print消息以及搜索到的节点的位置。

对于例如在上面的列表中,搜索节点说4,可以在位置4找到。

算法

  • 创建一个具有两个属性的类Node:data和next。下一个是指向列表中下一个节点的指针。
  • 创建另一个具有两个属性的头SearchLinkedList:head和tail。
  • addNode()将向列表添加一个新节点:
    1. 创建一个新节点。
    2. 它首先检查head是否等于null,这意味着列表为空。
    3. 如果列表为空,则头和尾都将指向新添加的节点。
    4. 如果列表不为空,则新节点将被添加到列表的末尾,使得尾部的下一个将指向新添加的节点。这个新节点将成为列表的新尾部。
  • searchNode()将在列表中搜索一个节点:
    1. 变量i将跟踪搜索到的节点的位置。
    2. 变量标志将存储布尔值false。
    3. 节点电流将指向头节点。
    4. 通过将电流增加到current.next并将i增加到i + 1来遍历循环。
    5. 将每个节点的数据与搜索到的节点进行比较。如果找到匹配项,请将标志设置为true。
    6. 如果该标志为true,则显示搜索到的节点的位置。
    7. 否则,显示消息“列表中不存在元素”。
  • 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 SearchLinkedList:
    #Represent the head and tail of the singly linked list
    def __init__(self):
        self.head = None;
        self.tail = None;
        
    #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;
            
    #searchNode() will search for a given node in the list
    def searchNode(self, data):
        current = self.head;
        i = 1;
        flag = False;
        
        #Checks whether list is empty
        if(self.head == None):
            print("List is empty");
        else:
            while(current != None):
                #Compares node to be found with each node present in the list
                if(current.data == data):
                    flag = True;
                    break;
                i=i+1;
                current = current.next;
        if(flag):
            print("Element is present in the list at the position : " + str(i));
        else:
            print("Element is not present in the list");
 
sList = SearchLinkedList();
 
#Add nodes to the list
sList.addNode(1);
sList.addNode(2);
sList.addNode(3);
sList.addNode(4);
 
#Search for node 2 in the list
sList.searchNode(2);
#Search for the node  in the list
sList.searchNode(7);

输出:

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

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;
 
//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;
    }
}
 
//searchNode() will search for a given node in the list
void searchNode(int data) {
    struct node *current = head;
    int i = 1;
    bool flag = false;
    
    //Checks whether list is empty
    if(head == NULL) {
        printf("List is empty \n");
    }
    else {
        while(current != NULL) {
             //Compares node to be found with each node present in the list
            if(current->data == data) {
                flag = true;
                break;
            }
            i++;
            current = current->next;
        }
    }
    if(flag)
         printf("Element is present in the list at the position: %d\n", i);
    else
         printf("Element is not present in the list\n");
}
    
int main()
{
    //Add nodes to the list
    addNode(1);
    addNode(2);
    addNode(3);
    addNode(4);
    
    //Search for node 2 in the list
    searchNode(2);
    //Search for node  in the list
    searchNode(7);
        
    return 0;
}

输出:

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

JAVA

public class SearchLinkedList {
    
    //Represent a node of singly linked list
    class Node{
        int data;
        Node next;
        
        public Node(int data) {
            this.data = data;
            this.next = null;
        }
    }
 
    //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;
        }
    }
    
    //searchNode() will search for a given node in the list
    public void searchNode(int data) {
        Node current = head;
        int i = 1;
        boolean flag = false;
        
        //Checks whether list is empty
        if(head == null) {
            System.out.println("List is empty");
        }
        else {
            while(current != null) {
                 //Compares node to be found with each node present in the list
                if(current.data == data) {
                    flag = true;
                    break;
                }
                i++;
                current = current.next;
            }
        }
        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) {
        
        SearchLinkedList sList = new SearchLinkedList();
        
        //Add nodes to the list
        sList.addNode(1);
        sList.addNode(2);
        sList.addNode(3);
        sList.addNode(4);
        
        //Search for node 2 in the list
        sList.searchNode(2);
        //Search for a node  in the list
        sList.searchNode(7);
    }
}

输出:

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

C#

using System;
                    
public class CreateList
{
    //Represent a node of singly linked list
    public class Node{
        public T data;
        public Node next;
        
        public Node(T value) {
            data = value;
            next = null;
        }
    }
        
    public class SearchLinkedList{
        //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(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;
            }
        }
    
        //searchNode() will search for a given node in the list
        public void searchNode(T data) {
            Node current = head;
            int i = 1;
            Boolean flag = false;
 
            //Checks whether list is empty
            if(head == null) {
                Console.WriteLine("List is empty");
            }
            else {
                while(current != null) {
                     //Compares node to be found with each node present in the list
                    if(current.data.Equals(data)) {
                        flag = true;
                        break;
                    }
                    i++;
                    current = current.next;
                }
            }
            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()
    {
        SearchLinkedList sList = new SearchLinkedList();
        
        //Add nodes to the list
        sList.addNode(1);
        sList.addNode(2);
        sList.addNode(3);
        sList.addNode(4);
        
        //Search for node 2 in the list
        sList.searchNode(2);
        //Search for a node  in the list
        sList.searchNode(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 SearchLinkedList{
    //Represent the head and tail of the singly linked list
    public $head;
    public $tail;
    function __construct(){
        $this->head = NULL;
        $this->tail = NULL;
    }
    
    //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;
        }
    }
    
    //searchNode() will search for a given node in the list
    function searchNode($data) {
        $current = $this->head;
        $i = 1;
        $flag = false;
        
        //Checks whether list is empty
        if($this->head == null) {
            print("List is empty
"); } else { while($current != null) { //Compares node to be found with each node present in the list if($current->data == $data) { $flag = true; break; } $i++; $current = $current->next; } } if($flag) print("Element is present in the list at the position : " . $i); else print("
Element is not present in the list"); } } $sList = new SearchLinkedList(); //Add nodes to the list $sList->addNode(1); $sList->addNode(2); $sList->addNode(3); $sList->addNode(4); //Search for node 2 in the list $sList->searchNode(2); //Search for node in the list $sList->searchNode(7); ?>

输出:

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