Searching in Circular Linked list
Last Updated :
26 Sep, 2023
Given a Circular Linked List CList, and an element K, the task is to check if this element K is present in the Circular Linked list or not.
Example:
Input: CList = 6->5->4->3->2, K = 3
Output: Found
Input: CList = 6->5->4->3->2, K = 1
Output: Not Found
Approach: The approach to find an element in Circular Linked List can be visualized as follows:
- Keep a pointer to the head of the Circular Linked List. This will help us to determine if the Linked List has been traversed already.
- Now traverse the Linked List one by one and check if the element is present or not.
- If the element is present, return true.
- Now if the element is not present and the traversal pointer reaches the head again, it means the element is not present in the CLL. Therefore return false.
Below is the implementation of the above approach:
C++
#include <iostream>
using namespace std;
class Search {
private :
class Node {
public :
int data;
Node* next;
Node( int data)
{
this ->data = data;
next = nullptr;
}
};
public :
Node* head = nullptr;
Node* tempo = nullptr;
void addNode( int data)
{
Node* new1 = new Node(data);
if (head == nullptr) {
head = new1;
}
else {
tempo->next = new1;
}
tempo = new1;
tempo->next = head;
}
void find( int key)
{
Node* temp = head;
int f = 0;
if (head == nullptr) {
cout << "List is empty" << endl;
}
else {
do {
if (temp->data == key) {
cout << key << " Found" << endl;
f = 1;
break ;
}
temp = temp->next;
} while (temp != head);
if (f == 0) {
cout << key << " Not Found" << endl;
}
}
}
};
int main()
{
Search s;
s.addNode(5);
s.addNode(4);
s.addNode(3);
s.addNode(2);
int key = 2;
s.find(key);
key = 6;
s.find(key);
return 0;
}
|
Java
public class Search {
class Node {
int data;
Node next;
public Node( int data) { this .data = data; }
}
public Node head = null ;
public Node tempo = null ;
public void addNode( int data)
{
Node new1 = new Node(data);
if (head == null ) {
head = new1;
}
else {
tempo.next = new1;
}
tempo = new1;
tempo.next = head;
}
public void find( int key)
{
Node temp = head;
int f = 0 ;
if (head == null ) {
System.out.println( "List is empty" );
}
else {
do {
if (temp.data == key) {
System.out.println(key + " Found" );
f = 1 ;
break ;
}
temp = temp.next;
} while (temp != head);
if (f == 0 ) {
System.out.println(key + " Not Found" );
}
}
}
public static void main(String[] args)
{
Search s = new Search();
s.addNode( 5 );
s.addNode( 4 );
s.addNode( 3 );
s.addNode( 2 );
int key = 2 ;
s.find(key);
key = 6 ;
s.find(key);
}
}
|
Python3
class Search:
class Node:
def __init__( self , data):
self .data = data
self . next = None
def __init__( self ):
self .head = None
self .tempo = None
def addNode( self , data):
new1 = Search.Node(data)
if self .head is None :
self .head = new1
else :
self .tempo. next = new1
self .tempo = new1
self .tempo. next = self .head
def find( self , key):
temp = self .head
f = 0
if self .head is None :
print ( "List is empty" )
else :
while True :
if temp.data = = key:
print (key, " Found" )
f = 1
break
temp = temp. next
if temp = = self .head:
break
if f = = 0 :
print (key, " Not Found" )
if __name__ = = "__main__" :
s = Search()
s.addNode( 5 )
s.addNode( 4 )
s.addNode( 3 )
s.addNode( 2 )
key = 2
s.find(key)
key = 6
s.find(key)
|
C#
using System;
namespace CircularLinkedListSearch
{
class Search
{
public class Node
{
public int data;
public Node next;
public Node( int data)
{
this .data = data;
}
}
public Node head = null ;
public Node tempo = null ;
public void AddNode( int data)
{
Node new1 = new Node(data);
if (head == null )
{
head = new1;
}
else
{
tempo.next = new1;
}
tempo = new1;
tempo.next = head;
}
public void Find( int key)
{
Node temp = head;
int f = 0;
if (head == null )
{
Console.WriteLine( "List is empty" );
}
else
{
do
{
if (temp.data == key)
{
Console.WriteLine(key + " Found" );
f = 1;
break ;
}
temp = temp.next;
} while (temp != head);
if (f == 0)
{
Console.WriteLine(key + " Not Found" );
}
}
}
static void Main( string [] args)
{
Search s = new Search();
s.AddNode(5);
s.AddNode(4);
s.AddNode(3);
s.AddNode(2);
int key = 2;
s.Find(key);
key = 6;
s.Find(key);
}
}
}
|
Javascript
class Search {
constructor() {
this .head = null ;
this .tempo = null ;
}
addNode(data) {
const newNode = { data, next: null };
if ( this .head === null ) {
this .head = newNode;
} else {
this .tempo.next = newNode;
}
this .tempo = newNode;
this .tempo.next = this .head;
}
find(key) {
let temp = this .head;
let found = false ;
if ( this .head === null ) {
console.log( "List is empty" );
} else {
do {
if (temp.data === key) {
console.log(key + " Found" );
found = true ;
break ;
}
temp = temp.next;
} while (temp !== this .head);
if (!found) {
console.log(key + " Not Found" );
}
}
}
}
const s = new Search();
s.addNode(5);
s.addNode(4);
s.addNode(3);
s.addNode(2);
let key = 2;
s.find(key);
key = 6;
s.find(key);
|
Output
2 Found
6 Not Found
Share your thoughts in the comments
Please Login to comment...