Skip to content
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
59 changes: 59 additions & 0 deletions search_element_in_Circular_LinkedList.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,59 @@
public class SearchNode {
public class Node{
int data;
Node next;
public Node(int data) {
this.data = data;
}
}

public Node head = null;
public Node tail = null;

public void add(int data){
Node newNode = new Node(data);
if(head == null) {
head = newNode;
tail = newNode;
newNode.next = head;
}
else {
tail.next = newNode;
tail = newNode;
tail.next = head;
}
}

public void search(int element) {
Node current = head;
int i = 1;
boolean flag = false;
if(head == null) {
System.out.println("List is empty");
}
else {
do{
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();
cl.add(1);
cl.add(2);
cl.add(3);
cl.add(4);
cl.search(2);
cl.search(7);
}
}