In this section, we will delve into Circular Linked Lists, a variant of linked lists where the last node points back to the first node instead of pointing to null. This unique structure offers several advantages and is used in various applications such as implementing circular buffers, round-robin scheduling algorithms, and more.
A Circular Linked List is a data structure consisting of nodes where each node contains two parts: the data part and the reference (or link) to the next node. Unlike a singly linked list or a doubly linked list, in a circular linked list, the last node points back to the first node, forming a circle.
A typical node in a circular linked list consists of:
For a circular doubly linked list, each node would also have a Previous Pointer pointing to the previous node.
Below is a basic implementation of a circular singly linked list in Python:
class Node:
def __init__(self, data):
self.data = data
self.next = None
class CircularLinkedList:
def __init__(self):
self.head = None
def append(self, data):
new_node = Node(data)
if not self.head:
self.head = new_node
self.head.next = self.head # Point to itself in a circular manner
else:
temp = self.head
while temp.next != self.head:
temp = temp.next
temp.next = new_node
new_node.next = self.head
def display(self):
if not self.head:
print("List is empty")
return
temp = self.head
while True:
print(temp.data, end=" -> ")
temp = temp.next
if temp == self.head:
break
print()
# Example usage
cll = CircularLinkedList()
cll.append(10)
cll.append(20)
cll.append(30)
cll.display() # Output: 10 -> 20 -> 30 ->
data and next.Below is an implementation of a circular doubly linked list in Python:
class Node:
def __init__(self, data):
self.data = data
self.next = None
self.prev = None
class CircularDoublyLinkedList:
def __init__(self):
self.head = None
def append(self, data):
new_node = Node(data)
if not self.head:
self.head = new_node
self.head.next = self.head
self.head.prev = self.head
else:
temp = self.head
while temp.next != self.head:
temp = temp.next
temp.next = new_node
new_node.prev = temp
new_node.next = self.head
self.head.prev = new_node
def display_forward(self):
if not self.head:
print("List is empty")
return
temp = self.head
while True:
print(temp.data, end=" <-> ")
temp = temp.next
if temp == self.head:
break
print()
def display_backward(self):
if not self.head:
print("List is empty")
return
temp = self.head.prev
while True:
print(temp.data, end=" <-> ")
temp = temp.prev
if temp == self.head.prev:
break
print()
# Example usage
cdll = CircularDoublyLinkedList()
cdll.append(10)
cdll.append(20)
cdll.append(30)
cdll.display_forward() # Output: 10 <-> 20 <-> 30 <->
cdll.display_backward() # Output: 30 <-> 20 <-> 10 <->
data, next, and prev.next and prev pointers to itself.Circular linked lists provide a unique way to handle data where cyclic traversal is required. They offer efficient traversal but come with additional complexity in terms of insertion, deletion, and maintaining the circular nature. Understanding and implementing circular linked lists can be beneficial for solving specific problems in computer science and software engineering.
By mastering circular linked lists, you enhance your ability to design and implement complex data structures that meet specific requirements efficiently.