0% found this document useful (0 votes)
10 views2 pages

Circular Linked List Implementation

The document describes the implementation of circular linked lists in Python, specifically focusing on Circular Singly Linked Lists and Circular Doubly Linked Lists. It includes class definitions for nodes and methods for appending and displaying elements in both types of lists. Key features highlight the circular nature of these lists, which is beneficial for applications like circular queues and round-robin scheduling.

Uploaded by

Pavani Ankala
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
10 views2 pages

Circular Linked List Implementation

The document describes the implementation of circular linked lists in Python, specifically focusing on Circular Singly Linked Lists and Circular Doubly Linked Lists. It includes class definitions for nodes and methods for appending and displaying elements in both types of lists. Key features highlight the circular nature of these lists, which is beneficial for applications like circular queues and round-robin scheduling.

Uploaded by

Pavani Ankala
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 2

Circular Linked List Implementation (Singly and Doubly) in Python

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

---

Circular Singly Linked List in Python:

class Node:
def __init__(self, data):
self.data = data
self.next = None

class CircularSinglyLinkedList:
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
return
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:
return
temp = self.head
while True:
print(temp.data, end=" ")
temp = temp.next
if temp == self.head:
break

---

Circular Doubly Linked List in Python:

class DNode:
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 = DNode(data)
if not self.head:
self.head = new_node
new_node.next = new_node
new_node.prev = new_node
return
tail = self.head.prev
tail.next = new_node
new_node.prev = tail
new_node.next = self.head
self.head.prev = new_node

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.

You might also like