C-Sharp | Java | Python | Swift | GO | WPF | Ruby | Scala | F# | JavaScript | SQL | PHP | Angular | HTML
Q. Program to search an element in a circular linked list.ExplanationIn 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
SolutionPython#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.head = Node(None); self.tail = Node(None); self.head.next = self.tail; self.tail.next = self.head; #This function will add the new node at the end of the list. def add(self,data): newNode = Node(data); #Checks if the list is empty. if self.head.data is None: #If list is empty, both head and tail would point to new node. self.head = newNode; self.tail = newNode; newNode.next = self.head; else: #tail will point to new node. self.tail.next = newNode; #New node will become new tail. self.tail = newNode; #Since, it is circular linked list tail will point to head. self.tail.next = self.head; #Searches for a node in the list def search(self,element): current = self.head; i = 1; flag = False; #Checks whether list is empty if(self.head == None): 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; if(current == self.head): break; if(flag): print("Element is present in the list at the position : " + str(i)); else: print("Element is not present in the list"); class CircularLinkedList: cl = CreateList(); #Adds data to the list cl.add(1); cl.add(2); cl.add(3); cl.add(4); #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 *head = NULL; struct node *tail = NULL; //This function will add the new node at the end of the list. void add(int data){ //Create new node struct node *newNode = (struct node*)malloc(sizeof(struct node)); newNode->data = data; //Checks if the list is empty. if(head == NULL){ //If list is empty, both head and tail would point to new node. head = newNode; tail = newNode; newNode->next = head; }else { //tail will point to new node. tail->next = newNode; //New node will become new tail. tail = newNode; //Since, it is circular linked list tail will point to head. tail->next = head; } } //Searches for a node in the list void search(int element) { struct node *current = head; int i = 1; bool flag = false; //Checks whether list is empty if(head == NULL) { 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++; }while(current != head); 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() { //Adds data to the list add(1); add(2); add(3); add(4); //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 JAVApublic 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 head = null; public Node tail = null; //This function will add the new node at the end of the list. public void add(int data){ //Create new node Node newNode = new Node(data); //Checks if the list is empty. if(head == null) { //If list is empty, both head and tail would point to new node. head = newNode; tail = newNode; newNode.next = head; } else { //tail will point to new node. tail.next = newNode; //New node will become new tail. tail = newNode; //Since, it is circular linked list tail will point to head. tail.next = head; } } //Searches for a node in the list public void search(int element) { Node current = head; int i = 1; boolean flag = false; //Checks whether list is empty if(head == null) { 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++; }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(); //Adds data to the list cl.add(1); cl.add(2); cl.add(3); cl.add(4); //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; namespace CircularLinkedList { 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> head = null; protected Node<T> tail = null; //This function will add the new node at the end of the list. public void add(T data){ //Create new node Node<T> newNode = new Node<T>(data); //Checks if the list is empty. if(head == null){ head = newNode; tail = newNode; newNode.next = head; }else{ //tail will point to new node. tail.next = newNode; //New node will become new tail. tail = newNode; //Since, it is circular linked list tail will point to head. tail.next = head; } } //Searches for a node in the list public void search(T element) { Node<T> current = head; int i = 1; bool flag = false; //Checks whether list is empty if(head == null) { 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++; }while(current != head); 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>(); //Adds data to the list cl.add(1); cl.add(2); cl.add(3); cl.add(4); //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 $head; private $tail; function __construct(){ $this->head = NULL; $this->tail = NULL; } //This function will add the new node at the end of the list. function add($data){ //Create new node $newNode = new Node($data); //Checks if the list is empty. if($this->head == NULL){ //If list is empty, both head and tail would point to new node. $this->head = $newNode; $this->tail = $newNode; $newNode->next = $this->head; } else{ //tail will point to new node. $this->tail->next = $newNode; //New node will become new tail. $this->tail = $newNode; //Since, it is circular linked list tail will point to head. $this->tail->next = $this->head; } } //Searches for a node in the list function search($element) { $current = $this->head; $i = 1; $flag = false; //Checks whether list is empty if($this->head == NULL) { 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++; }while($current != $this->head); 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(); //Adds data to the list $cl->add(1); $cl->add(2); $cl->add(3); $cl->add(4); //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#
|