Article Categories
- All Categories
-
Data Structure
-
Networking
-
RDBMS
-
Operating System
-
Java
-
MS Excel
-
iOS
-
HTML
-
CSS
-
Android
-
Python
-
C Programming
-
C++
-
C#
-
MongoDB
-
MySQL
-
Javascript
-
PHP
-
Economics & Finance
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 nodes to the linked list, to search for a specific node in the linked list and to print the node values.
Implementation
Below is a demonstration for searching an element in a circular linked list ?
class Node:
def __init__(self, data):
self.data = data
self.next = None
class CircularLinkedList:
def __init__(self):
self.head = None
def add_data(self, my_data):
new_node = Node(my_data)
if self.head is None:
self.head = new_node
new_node.next = self.head
else:
current = self.head
while current.next != self.head:
current = current.next
current.next = new_node
new_node.next = self.head
def search_value(self, elem_to_search):
if self.head is None:
print("The list is empty")
return
current = self.head
position = 1
while True:
if current.data == elem_to_search:
print(f"The element is present in list at position: {position}")
return
current = current.next
position += 1
if current == self.head:
break
print("The element is not present in list")
def print_list(self):
if self.head is None:
print("The list is empty")
return
current = self.head
while True:
print(current.data)
current = current.next
if current == self.head:
break
# Create circular linked list and test search
my_list = CircularLinkedList()
print("Nodes are being added to the list")
my_list.add_data(21)
my_list.add_data(54)
my_list.add_data(78)
my_list.add_data(99)
my_list.add_data(27)
print("The list is:")
my_list.print_list()
print("Value 99 is being searched")
my_list.search_value(99)
print("Value 0 is being searched")
my_list.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
How the Search Works
The search algorithm follows these steps ?
- Start from the head node of the circular linked list
- Compare the current node's data with the target element
- If found, return the position; otherwise, move to the next node
- Continue until we reach the head node again (completing the circle)
- If the element is not found after traversing the entire list, return "not found"
Key Features
- Circular Structure: The last node points back to the first node
- Position Tracking: The search method tracks the position of each element
- Complete Traversal: The algorithm stops when it returns to the starting node
- Empty List Handling: Checks for empty list before searching
Conclusion
Searching in a circular linked list requires careful handling of the circular nature to avoid infinite loops. The algorithm tracks position and stops when it completes a full circle, ensuring efficient element lookup.
---