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 sort the elements of the Circular Linked List
A Circular Linked List is a data structure where the last node points back to the first node, forming a circle. To sort elements in a circular linked list, we need to create a Node class and implement sorting algorithms.
In a circular linked list, unlike regular linked lists, there's no NULL value at the end. Instead, the tail node connects back to the head, creating a continuous loop.
Node Class Structure
First, we define a Node class to represent individual elements ?
class Node:
def __init__(self, data):
self.data = data
self.next = None
Circular Linked List Implementation
Next, we create a class to manage the circular linked list with methods to add data, sort, and display ?
class CircularLinkedList:
def __init__(self):
self.head = None
self.tail = None
def add_data(self, data):
new_node = Node(data)
if self.head 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 sort_list(self):
if self.head is None:
print("The list is empty")
return
current = self.head
while True:
index = current.next
while index != self.head:
if current.data > index.data:
# Swap data
current.data, index.data = index.data, current.data
index = index.next
current = current.next
if current.next == self.head:
break
def print_list(self):
if self.head is None:
print("The list is empty")
return
current = self.head
print(current.data, end=" ")
current = current.next
while current != self.head:
print(current.data, end=" ")
current = current.next
print()
# Create and test the circular linked list
cll = CircularLinkedList()
print("Adding nodes to the circular linked list...")
cll.add_data(21)
cll.add_data(54)
cll.add_data(78)
cll.add_data(99)
cll.add_data(27)
print("Original list:")
cll.print_list()
print("Sorting the list...")
cll.sort_list()
print("Sorted list:")
cll.print_list()
Adding nodes to the circular linked list... Original list: 21 54 78 99 27 Sorting the list... Sorted list: 21 27 54 78 99
How the Sorting Works
The sorting algorithm uses a bubble sort approach ?
- Outer loop: Iterates through each node starting from head
- Inner loop: Compares current node with all subsequent nodes
- Swapping: If current node data is greater than compared node, swap their values
- Termination: Process continues until we complete one full cycle
Key Points
- The
tail.nextalways points toheadto maintain the circular structure - Sorting is done by swapping data values rather than rearranging node pointers
- Loop termination is checked by comparing with the head node
- Time complexity is O(n²) due to nested loops
Conclusion
Sorting a circular linked list involves maintaining the circular structure while applying sorting algorithms. The bubble sort approach swaps data values to arrange elements in ascending order, making it suitable for small to medium-sized lists.
