Circular Linked List Implementation
Circular Linked List Implementation
A circular linked list is a variation where the last node points back to the first node.
There are two types:
1. Circular Singly Linked List
2. Circular Doubly Linked List
---
class Node:
def __init__(self, data):
self.data = data
self.next = None
class CircularSinglyLinkedList:
def __init__(self):
self.head = None
def display(self):
if not self.head:
return
temp = self.head
while True:
print(temp.data, end=" ")
temp = temp.next
if temp == self.head:
break
---
class DNode:
def __init__(self, data):
self.data = data
self.next = None
self.prev = None
class CircularDoublyLinkedList:
def __init__(self):
self.head = None
def display(self):
if not self.head:
return
temp = self.head
while True:
print(temp.data, end=" ")
temp = temp.next
if temp == self.head:
break
---
Key Features:
- Circular nature means last node connects to the first.
- Useful in circular queues, round-robin scheduling.