- Python 3 Basic Tutorial
- Python 3 - Home
- What is New in Python 3
- Python 3 - Overview
- Python 3 - Environment Setup
- Python 3 - Basic Syntax
- Python 3 - Variable Types
- Python 3 - Basic Operators
- Python 3 - Decision Making
- Python 3 - Loops
- Python 3 - Numbers
- Python 3 - Strings
- Python 3 - Lists
- Python 3 - Tuples
- Python 3 - Dictionary
- Python 3 - Date & Time
- Python 3 - Functions
- Python 3 - Modules
- Python 3 - Files I/O
- Python 3 - Exceptions
- Python 3 Advanced Tutorial
- Python 3 - Classes/Objects
- Python 3 - Reg Expressions
- Python 3 - CGI Programming
- Python 3 - Database Access
- Python 3 - Networking
- Python 3 - Sending Email
- Python 3 - Multithreading
- Python 3 - XML Processing
- Python 3 - GUI Programming
- Python 3 - Further Extensions
Python program to search an element in a Circular Linked List
When it is required to search for an element in a circular linked list, a ‘Node’ 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.
In a circular linked list, the head and the rear are adjacent to each other. They are connected to form a circle, and don’t have ‘NULL’ 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 ‘None’.
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.
Below is a demonstration for the same −
class Node: def __init__(self,data): self.data = data self.next = None class list_creation: def __init__(self): self.head = Node(None) self.tail = Node(None) self.head.next = self.tail self.tail.next = self.head def add_data(self,my_data): new_node = Node(my_data) if self.head.data is None: self.head = new_node self.tail = new_node new_node.next = self.head else: self.tail.next = new_node self.tail = new_node self.tail.next = self.head def search_value(self,elem_to_search): curr = self.head; i = 1; flag_val = False; if(self.head == None): print("The list is empty"); else: while(True): if(curr.data == elem_to_search): flag_val = True; break; curr = curr.next; i = i + 1; if(curr == self.head): break; if(flag_val): print("The element is present in list at position : " + str(i)); else: print("The element is not present in list"); def print_it(self): curr = self.head if self.head is None: print("The list is empty"); return; else: print(curr.data) while(curr.next != self.head): curr = curr.next print(curr.data) print("\n") class circular_linked_list: my_cl = list_creation() print("Nodes are being added to the list") my_cl.add_data(21) my_cl.add_data(54) my_cl.add_data(78) my_cl.add_data(99) my_cl.add_data(27) print("The list is :") my_cl.print_it() print("Value 99 is being searched") my_cl.search_value(99) print("Value 0 is being searched") my_cl.search_value(0)
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
- The ‘Node’ class is created.
- Another class with required attributes is created.
- Another method named ‘search_value’ is defined, that is used to search for a specific element in the linked list.
- Another method named ‘print_it’ is defined, that displays the nodes of the circular linked list.
- An object of the ‘list_creation’ class is created, and the methods are called on it to add data.
- An ‘init’ method is defined, that the first and last nodes of the circular linked list to None.
- The ‘search_value’ method is called.
- It iterates through the list, and checks to see if the element that needs to be searched is found.
- If it is found, its index is displayed.
- This is displayed on the console using the ‘print_it’ method.
- Related Articles
- Python program to search an element in a doubly linked list
- Python Program to Search for an Element in the Linked List without using Recursion
- Python Circular Linked List Program
- Python program to create and display a Circular Linked List
- Python Program to Convert a given Singly Linked List to a Circular List
- Searching an Element in Doubly Circular Linked List using C++
- Python program to remove duplicate elements from a Circular Linked List
- Program to create linked list to binary search tree in Python
- Search an element in the given singly Linked List using C++
- Python Program to search an element in a tuple
- Python program to sort the elements of the Circular Linked List
- C++ Program to Implement Circular Singly Linked List
- C++ Program to Implement Circular Doubly Linked List
- Python Program to Find the Largest Element in a Doubly Linked List
- Convert an Array to a Circular Doubly Linked List in C++