📜  在循环链表中搜索元素的Java程序

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

在循环链表中搜索元素的Java程序

链表是一种线性数据结构,其中每个节点都有一个数据部分和一个指向下一个节点的地址部分。循环链表是一种链表,其中最后一个节点指向第一个节点,形成一个循环节点。例子:

Input : CList = 6->5->4->3->2, find = 3
Output: Element is present

Input : CList = 6->5->4->3->2, find = 1
Output: Element is not present

在循环链表中搜索元素

例如,如果要搜索的键是 30,链表是 5->4->3->2,那么函数应该返回 false。如果要搜索的键是 4,则该函数应返回 true。

方法:

  1. 初始化一个节点指针,temp = head
  2. 初始化一个计数器 f=0(检查元素是否存在于链表中)
  3. 如果头部为空,则打印列表为空
  4. 否则开始遍历链表,如果在链表中找到的元素在 f 中递增。
  5. 如果计数器为零,则未找到打印元素

下面是上述方法的实现:

Java
// Java program to Search an Element
// in a Circular Linked List
public class search {
    class Node {
  
        int data;
        Node next;
        public Node(int data) { this.data = data; }
    }
    // declaring head pointer as null
    public Node head = null;
    public Node tempo = null;
    
    // function adds new nodes at the end of list
    public void addNode(int data)
    {
        Node new1 = new Node(data);
        
        // If linked list is empty
        if (head == null) {
            head = new1;
        }
        else {
            tempo.next = new1;
        }
  
        tempo = new1;
        
        // last node points to first node
        tempo.next = head;
    }
    public void find(int key)
    {
        // temp will traverse the circular
        // linked list for searching element
        Node temp = head;
        
        // counter used to check if
        // element is found or not
        int f = 0;
        if (head == null) {
            System.out.println("List is empty");
        }
        else {
            do {
                if (temp.data == key) {
                    System.out.println(
                        "element is present");
                    f = 1;
                    break;
                }
                temp = temp.next;
            } while (temp != head);
            if (f == 0) {
                System.out.println(
                    "element is not present");
            }
        }
    }
    public static void main(String[] args)
    {
        search s = new search();
        
        // Adds data to the list
        s.addNode(5);
        s.addNode(4);
        s.addNode(3);
        s.addNode(2);
        
        // Search for node 2 in the list
        s.find(2);
        
        // Search for node 6 in the list
        s.find(6);
    }
}


输出
element is present
element is not present

时间复杂度: O(N),其中 N 是循环链表的长度。