Python program to search an element in a Circular Linked List

PythonServer Side ProgrammingProgramming

<p>When it is required to search for an element in a circular linked list, a &lsquo;Node&rsquo; class needs to be created. In this class, there are two attributes, the data that is present in the node, and the access to the next node of the linked list.</p><p>In a circular linked list, the head and the rear are adjacent to each other. They are connected to form a circle, and don&rsquo;t have &lsquo;NULL&rsquo; value in the last node. Another class needs to be created that would have an initialization function, and the head of the node would be initialized to &lsquo;None&rsquo;.</p><p>Multiple methods are defined by the user to add node to the linked list, to search for a specific node in the linked list and to print the node values.</p><p>Below is a demonstration for the same &minus;</p><h2>Example</h2><p><a class="demo" href="http://tpcg.io/vQamqkhQ" rel="nofollow" target="_blank">&nbsp;Live Demo</a></p><pre class="prettyprint notranslate">class Node: &nbsp; &nbsp;def __init__(self,data): &nbsp; &nbsp; &nbsp; self.data = data &nbsp; &nbsp; &nbsp; self.next = None class list_creation: &nbsp; &nbsp;def __init__(self): &nbsp; &nbsp; &nbsp; self.head = Node(None) &nbsp; &nbsp; &nbsp; self.tail = Node(None) &nbsp; &nbsp; &nbsp; self.head.next = self.tail &nbsp; &nbsp; &nbsp; self.tail.next = self.head &nbsp; &nbsp;def add_data(self,my_data): &nbsp; &nbsp; &nbsp; new_node = Node(my_data) &nbsp; &nbsp; &nbsp; if self.head.data is None: &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;self.head = new_node &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;self.tail = new_node &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;new_node.next = self.head &nbsp; &nbsp; &nbsp; else: &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;self.tail.next = new_node &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;self.tail = new_node &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;self.tail.next = self.head &nbsp; &nbsp;def search_value(self,elem_to_search): &nbsp; &nbsp; &nbsp; curr = self.head; &nbsp; &nbsp; &nbsp; i = 1; &nbsp; &nbsp; &nbsp; flag_val = False; &nbsp; &nbsp; &nbsp; if(self.head == None): &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;print(&quot;The list is empty&quot;); &nbsp; &nbsp; &nbsp; else: &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;while(True): &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; if(curr.data == elem_to_search): &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;flag_val = True; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;break; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; curr = curr.next; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; i = i + 1; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; if(curr == self.head): &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;break; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;if(flag_val): &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; print(&quot;The element is present in list at position : &quot; + str(i)); &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;else: &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; print(&quot;The element is not present in list&quot;); &nbsp; &nbsp;def print_it(self): &nbsp; &nbsp; &nbsp; curr = self.head &nbsp; &nbsp; &nbsp; if self.head is None: &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;print(&quot;The list is empty&quot;); &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;return; &nbsp; &nbsp; &nbsp; else: &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;print(curr.data) &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;while(curr.next != self.head): &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; curr = curr.next &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; print(curr.data) &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;print(&quot; &quot;) class circular_linked_list: &nbsp; &nbsp;my_cl = list_creation() &nbsp; &nbsp;print(&quot;Nodes are being added to the list&quot;) &nbsp; &nbsp;my_cl.add_data(21) &nbsp; &nbsp;my_cl.add_data(54) &nbsp; &nbsp;my_cl.add_data(78) &nbsp; &nbsp;my_cl.add_data(99) &nbsp; &nbsp;my_cl.add_data(27) &nbsp; &nbsp;print(&quot;The list is :&quot;) &nbsp; &nbsp;my_cl.print_it() &nbsp; &nbsp;print(&quot;Value 99 is being searched&quot;) &nbsp; &nbsp;my_cl.search_value(99) &nbsp; &nbsp;print(&quot;Value 0 is being searched&quot;) &nbsp; &nbsp;my_cl.search_value(0)</pre><h2>Output</h2><pre class="result notranslate">Nodes are being added to the list The list is : 21 54 78 99 27 Value 99 is being searched The element is present in list at position : 4 Value 0 is being searched The element is not present in list</pre><h2>Explanation</h2><ul class="list"><li>The &lsquo;Node&rsquo; class is created.</li><li>Another class with required attributes is created.</li><li>Another method named &lsquo;search_value&rsquo; is defined, that is used to search for a specific element in the linked list.</li><li>Another method named &lsquo;print_it&rsquo; is defined, that displays the nodes of the circular linked list.</li><li>An object of the &lsquo;list_creation&rsquo; class is created, and the methods are called on it to add data.</li><li>An &lsquo;init&rsquo; method is defined, that the first and last nodes of the circular linked list to None.</li><li>The &lsquo;search_value&rsquo; method is called.</li><li>It iterates through the list, and checks to see if the element that needs to be searched is found.</li><li>If it is found, its index is displayed.</li><li>This is displayed on the console using the &lsquo;print_it&rsquo; method.</li></ul>
raja
Updated on 11-Mar-2021 11:27:48

Advertisements