# Program to Search an Element in a Circular Linked List

Program to Search an Element in a Circular Linked List on fibonacci, factorial, prime, armstrong, swap, reverse, search, sort, stack, queue, array, linkedlist, tree, graph etc.

## Q. Program to search an element in a circular linked list.

### Explanation

In this program, we create a circular linked list and search a node in the list.

9->5->2->7->3

Consider, above example. Suppose we need to search for node 5. To solve this problem, we will iterate through the list and compare each node with 5. If match is found, we will set the flag to true and prints out the position of the node 5. In this example, node 5 is present at the position 2.

### Algorithm

1. Define a Node class which represents a node in the list. It has two properties data and next which will point to the next node.
2. Define another class for creating circular linked list and it has two nodes: head and tail.
3. search() will search for a node in the list:
1. Variable i will keep track of the position of the searched node.
2. Variable flag will store boolean value false.
3. Current will point to head node.
4. Iterate through the loop by incrementing current to current.next and i to i + 1.
5. Compare each node's data with searched node. If match is found, set flag to true.
6. If flag is true, prints the position of searched node.
7. Else, print the message "Element is not present in the list".

## Solution

### Python

```#Represents the node of list.
class Node:
def __init__(self,data):
self.data = data;
self.next = None;

class CreateList:
#Declaring head and tail pointer as null.
def __init__(self):
self.tail = Node(None);

#This function will add the new node at the end of the list.
newNode = Node(data);
#Checks if the list is empty.
#If list is empty, both head and tail would point to new node.
self.tail = newNode;
else:
#tail will point to new node.
self.tail.next = newNode;
#New node will become new tail.
self.tail = newNode;

#Searches for a node in the list
def search(self,element):
i = 1;
flag = False;
#Checks whether list is empty
print("List is empty");
else:
while(True):
#Compares element to be found with each node present in the list
if(current.data ==  element):
flag = True;
break;
current = current.next;
i = i + 1;
break;
if(flag):
print("Element is present in the list at the position :  " + str(i));
else:
print("Element is not present in the list");

cl = CreateList();
#Search for node 2 in the list
cl.search(2);
#Search for node in the list
cl.search(7);
```

Output:

```Element is present in the list at the position : 2
Element is not present in the list
```

### C

```#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include <stdbool.h>

//Represents the node of list.
struct node{
int data;
struct node *next;
};

//Declaring head and tail pointer as null.
struct node *tail = NULL;

//This function will add the new node at the end of the list.
//Create new node
struct node *newNode = (struct node*)malloc(sizeof(struct node));
newNode->data = data;
//Checks if the list is empty.
//If list is empty, both head and tail would point to new node.
tail = newNode;
}else {
//tail will point to new node.
tail->next = newNode;
//New node will become new tail.
tail = newNode;
}
}

//Searches for a node in the list
void search(int element) {
int i = 1;
bool flag = false;
//Checks whether list is empty
printf("List is empty");
}
else {
do{
//Compares element to be found with each node present in the list
if(current->data ==  element) {
flag = true;
break;
}
current = current->next;
i++;
if(flag)
printf("Element is present in the list at the position : %d", i);
else
printf("\nElement is not present in the list");
}
}

int main()
{
//Search for node 2 in the list
search(2);
//Search for node in the list
search(7);

return 0;
}
```

Output:

```Element is present in the list at the position : 2
Element is not present in the list
```

### JAVA

```public class SearchNode {
//Represents the node of list.
public class Node{
int data;
Node next;
public Node(int data) {
this.data = data;
}
}

//Declaring head and tail pointer as null.
public Node tail = null;

//This function will add the new node at the end of the list.
//Create new node
Node newNode = new Node(data);
//Checks if the list is empty.
//If list is empty, both head and tail would point to new node.
tail = newNode;
}
else {
//tail will point to new node.
tail.next = newNode;
//New node will become new tail.
tail = newNode;
}
}

//Searches for a node in the list
public void search(int element) {
int i = 1;
boolean flag = false;
//Checks whether list is empty
System.out.println("List is empty");
}
else {
do{
//Compares element to be found with each node present in the list
if(current.data ==  element) {
flag = true;
break;
}
current = current.next;
i++;
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();
//Search for node 2 in the list
cl.search(2);
//Search for node in the list
cl.search(7);
}
}
```

Output:

```Element is present in the list at the position : 2
Element is not present in the list
```

### C#

``` using System;
{
public class Program
{
//Represents the node of list.
public class Node<T>{
public T data;
public Node<T> next;
public Node(T value) {
data = value;
next = null;
}
}

public class CreateList<T>{
protected Node<T> tail = null;

//This function will add the new node at the end of the list.
//Create new node
Node<T> newNode = new Node<T>(data);
//Checks if the list is empty.
tail = newNode;
}else{
//tail will point to new node.
tail.next = newNode;
//New node will become new tail.
tail = newNode;
}
}

//Searches for a node in the list
public void search(T element) {
int i = 1;
bool flag = false;
//Checks whether list is empty
Console.WriteLine("List is empty");
}
else {
do{
//Compares element to be found with each node present in the list
if(current.data.Equals(element)) {
flag = true;
break;
}
current = current.next;
i++;
if(flag)
Console.WriteLine("Element is present in the list at the position : " + i);
else
Console.WriteLine("Element is not present in the list");
}
}
}

public static void Main()
{

CreateList<int> cl = new CreateList<int>();
//Search for node 2 in the list
cl.search(2);
//Search for node in the list
cl.search(7);
}
}
}
```

Output:

```Element is present in the list at the position : 2
Element is not present in the list
```

### PHP

```<!DOCTYPE html>
<html>
<body>
<?php
//Represents the node of list.
class Node{
public \$data;
public \$next;
function __construct(\$data){
\$this->data = \$data;
\$this->next = NULL;
}
}
class CreateList{
//Declaring head and tail pointer as null.
private \$tail;
function __construct(){
\$this->tail = NULL;
}
//This function will add the new node at the end of the list.
//Create new node
\$newNode = new Node(\$data);
//Checks if the list is empty.
//If list is empty, both head and tail would point to new node.
\$this->tail = \$newNode;
}
else{
//tail will point to new node.
\$this->tail->next = \$newNode;
//New node will become new tail.
\$this->tail = \$newNode;
}
}

//Searches for a node in the list
function search(\$element) {
\$i = 1;
\$flag = false;
//Checks whether list is empty
echo "List is empty";
}
else {
do{
//Compares element to be found with each node present in the list
if(\$current->data == \$element) {
\$flag = true;
break;
}
\$current = \$current->next;
\$i++;
if(\$flag)
echo "Element is present in the list at the position : \$i <br>";
else
echo "Element is not present in the list<br>";
}
}
}

\$cl = new CreateList();
//Search for node 2 in the list
\$cl->search(2);
//Search for node  in the list
\$cl->search(7);
?>
</body>
</html>
```

Output:

```Element is present in the list at the position : 2
Element is not present in the list
```

Next Topic#