Java Program to Search an Element in a Circular Linked List


What are Liked List and Circular Linked List?

Linked list is data structure in which every node contains two parts, a data and address path. These parts point to a next node, which always creates an interlinking with previous nodes. Based on this, a circular linked list is where the last node has an inter link with the first one, that is what these types of linked lists are known as circular linked lists.

In the Java environment when we search for an element circular linked list, it is required to create a temporary node in the list to point. With this there are two more variables we need to declare. They are track index and track search. If the Temp node is void at the starting point then it’s important to traverse the list, because it contains no item at that moment.

How Circular Linked List Works and Its Applications?

Working Method of a Circular Linked List

For Circular linked list a user can input data anywhere in that particular list (in an array which is not possible for being an adjacent memory). In this linked list the backward data stores the address node of the next one. In this way the data points in a circular manner to each other which forms a circular chain which is dynamic in size. Here dynamic means; the memory allocation will be done as per requirement.

Below points are needed to be remembered

  • Any node can be a starting point for circular linked list

  • Data list can be traversed from any random node

  • There is no pointer for the first node here

Application of Circular Linked List

  • Circular linked lists used in our personal computers were at the same time multiple applications performing their tasks.

  • Used to create a circular queue.

  • To swap between players in a loop for multiplayer games.

  • For undo function in word or Photoshop applications.

Algorithm of Circular Linked List

Implementation and operation method of a circular linked list is very easy. There are two features, data and next. To define another circular linked list we can use: head and tail. New nodes are always defined by 'current node', which will point to the head of a linked list. After each iteration point will move to next node.

  • Step 1 − Declare a newNode() by the given value.

  • Step 2 − Search for the void list.

  • Step 3 − If the result is void then, head = newNode().

  • Step 4 − Else, define node pointer as temp and initialize.

Syntax of a Circular Linked List

struct Node {int dataset; struct Node * to next;};

In this syntax, each node present in the list has a data and pointer portion to create a new node at the time of receiving new inputs.

We can use below approaches for searching an element in a particular list −

  • By adding new data into a particular list

  • By searching Elements in a particular Circular Linked List

By Adding New Data into a Particular Linked List

Adding some new element into a new node helps to find out some particular data from a circular linked list. At first, you need to insert a new node to the allocated memory. After storing a new data, you can change the next data to a new node. You can also store another data at the end of the node and apply traverse.

Example

public class SearchNodearb {   
   public class Nodefind{  
      int datafall;  
      Nodefind next;  
      public Nodefind(int datafall) {  
         this.datafall = datafall;  
      }  
   }  
   public Nodefind head = null;  
   public Nodefind tail = null;  
   public void add(int datafall){   
      Nodefind newNode1 = new Nodefind(datafall);      
      if(head == null) {        
         head = newNode1;  
         tail = newNode1;  
         newNode1.next = head;  
      }  
      else {     
         tail.next = newNode1;
            
         tail = newNode1;         
         tail.next = head;  
      }  
   }  
   public void search(int element) {  
      Nodefind current = head;  
      int i = 1;  
      boolean flagon = false;  
              
      if(head == null) {  
         System.out.println("List is totally Void! So Sad!");  
      }  
      else {  
         do{  
            if(current.datafall ==  element) {  
               flagon = true;  
               break;  
            }  
            current = current.next;  
            i++;  
         }while(current != head);  
         if(flagon)  
         System.out.println("Element is present in the list with a position tag : " + i);  
         else  
         System.out.println("Element is not present in the list");  
      }  
   }  
   public static void main(String[] args) {  
      SearchNodearb cl1 = new SearchNodearb();            
      cl1.add(1000);  
      cl1.add(5000);  
      cl1.add(3);  
      cl1.add(4);  
      cl1.search(2);  
      cl1.search(5000);  
   }  
} 

Output

Element is not present in the list
Element is present in the list with a position tag: 2

By Searching Elements in a Particular Circular Linked List

First, you need to initialize a node then counter f=0. If the position of head is void then the list whole is void. Else traverse the full list. If the output is zero then the element is not found in the list.

Example

public class search {
   class Nodeval {
      int data;
      Nodeval next;
      public Nodeval(int data) { this.data = data; }
   }
   public Nodeval head = null;
   public Nodeval tempo = null;
   public void addNode2001(int data){
      Nodeval new10 = new Nodeval(data);
      if (head == null) {
         head = new10;
      }
      else {
         tempo.next = new10;
      }
      tempo = new10;
      tempo.next = head;
   }
   public void find(int key){
      Nodeval temp07 = head;     
      int f = 0;
      if (head == null) {
         System.out.println("List is empty, Please Fill It ASAP");
      }
      else {
         do {
            if (temp07.data == key) {
               System.out.println(
               "element is present in the running list");
               f = 1;
               break;
            }
            temp07 = temp07.next;
         } while (temp07 != head);
         if (f == 0) {
            System.out.println(
            "element is not present here, I am sorry!");
         }
      }
   }
   public static void main(String[] args){
      search srdd = new search();
      srdd.addNode2001(5);
      srdd.addNode2001(4);
      srdd.addNode2001(3);
      srdd.addNode2001(2);
      srdd.find(2);
      srdd.find(6);
   }
} 

Output

element is present in the running list
element is not present here, I am sorry!

Conclusion

There are lots of advantages and disadvantages of using the circular linked list. The most important advantage is that the operation can be initiated from any node of the list to traverse. There is no needed to use NULL and very useful for CPU round robin scheduling. But the biggest disadvantage is if the list is not written in a correct programming, it may become an infinite loop and the server can be hanged. Thus from this article, we have learnt how to search elements in the Circular Linked List using Java.

Updated on: 31-Mar-2023

255 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements