📜  用于在链表中搜索元素的Java程序

📅  最后修改于: 2022-05-13 01:55:15.481000             🧑  作者: Mango

用于在链表中搜索元素的Java程序

编写一个函数,在给定的单链表中搜索给定的键“x”。如果 x 存在于链表中,则该函数应返回 true,否则返回 false。

bool search(Node *head, int x)

例如,如果要搜索的键是 15,链表是 14->21->11->30->10,那么函数应该返回 false。如果要搜索的键是 14,那么函数应该返回 true。
迭代解决方案:

1) Initialize a node pointer, current = head.
2) Do following while current is not NULL
    a) current->key is equal to the key being searched return true.
    b) current = current->next
3) Return false 

以下是上述算法的迭代实现以搜索给定的键。

Java
// Iterative Java program to search 
// an element in linked list
  
//Node class
class Node
{
    int data;
    Node next;
    Node(int d)
    {
        data = d;
        next = null;
    }
}
  
//Linked list class
class LinkedList
{
    // Head of list
    Node head;    
  
    // Inserts a new node at the front 
    // of the list
    public void push(int new_data)
    {
        //Allocate new node and putting data
        Node new_node = new Node(new_data);
  
        //Make next of new node as head
        new_node.next = head;
  
        //Move the head to point to new Node
        head = new_node;
    }
  
    // Checks whether the value x is present 
    // in linked list
    public boolean search(Node head, int x)
    {
        // Initialize current
        Node current = head;    
        while (current != null)
        {
            // Data found
            if (current.data == x)
                return true;    
            current = current.next;
        }
      
        // Data not found
        return false;    
    }
  
    // Driver code
    public static void main(String args[])
    {
        // Start with the empty list
        LinkedList llist = new LinkedList();
  
        // Use push() to construct list
        // 14->21->11->30->10 
        llist.push(10);
        llist.push(30);
        llist.push(11);
        llist.push(21);
        llist.push(14);
  
        if (llist.search(llist.head, 21))
            System.out.println("Yes");
        else
            System.out.println("No");
    }
}
// This code is contributed by Pratik Agarwal


Java
// Recursive Java program to search an element
// in linked list
  
// Node class
class Node
{
    int data;
    Node next;
    Node(int d)
    {
        data = d;
        next = null;
    }
}
  
// Linked list class
class LinkedList
{
    // Head of list
    Node head;    
  
    // Inserts a new node at the 
    // front of the list
    public void push(int new_data)
    {
        // Allocate new node and putting data
        Node new_node = new Node(new_data);
  
        // Make next of new node as head
        new_node.next = head;
  
        // Move the head to point to new Node
        head = new_node;
    }
  
    // Checks whether the value x is present
    // in linked list
    public boolean search(Node head, int x)
    {
        // Base case
        if (head == null)
            return false;
  
        // If key is present in current node,
        // return true
        if (head.data == x)
            return true;
  
        // Recur for remaining list
        return search(head.next, x);
    }
  
    // Driver code
    public static void main(String args[])
    {
        // Start with the empty list
        LinkedList llist = new LinkedList();
  
        // Use push() to construct list
        // 14->21->11->30->10 
        llist.push(10);
        llist.push(30);
        llist.push(11);
        llist.push(21);
        llist.push(14);
  
        if (llist.search(llist.head, 21))
            System.out.println("Yes");
        else
            System.out.println("No");
    }
}
// This code is contributed by Pratik Agarwal


输出:

Yes

递归解决方案:

bool search(head, x)
1) If head is NULL, return false.
2) If head's key is same as x, return true;
3) Else return search(head->next, x) 

以下是上述算法的递归实现,用于搜索给定的键。

Java

// Recursive Java program to search an element
// in linked list
  
// Node class
class Node
{
    int data;
    Node next;
    Node(int d)
    {
        data = d;
        next = null;
    }
}
  
// Linked list class
class LinkedList
{
    // Head of list
    Node head;    
  
    // Inserts a new node at the 
    // front of the list
    public void push(int new_data)
    {
        // Allocate new node and putting data
        Node new_node = new Node(new_data);
  
        // Make next of new node as head
        new_node.next = head;
  
        // Move the head to point to new Node
        head = new_node;
    }
  
    // Checks whether the value x is present
    // in linked list
    public boolean search(Node head, int x)
    {
        // Base case
        if (head == null)
            return false;
  
        // If key is present in current node,
        // return true
        if (head.data == x)
            return true;
  
        // Recur for remaining list
        return search(head.next, x);
    }
  
    // Driver code
    public static void main(String args[])
    {
        // Start with the empty list
        LinkedList llist = new LinkedList();
  
        // Use push() to construct list
        // 14->21->11->30->10 
        llist.push(10);
        llist.push(30);
        llist.push(11);
        llist.push(21);
        llist.push(14);
  
        if (llist.search(llist.head, 21))
            System.out.println("Yes");
        else
            System.out.println("No");
    }
}
// This code is contributed by Pratik Agarwal

输出:

Yes

有关详细信息,请参阅有关在链接列表(迭代和递归)中搜索元素的完整文章!