0% found this document useful (0 votes)
6 views

DS Lab Assignment3

Uploaded by

bchgpdqk57
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
6 views

DS Lab Assignment3

Uploaded by

bchgpdqk57
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 4

3.

Write a program to implement circular linked list as an ADT which


supports the following operations:
i. Insert an element x in the list
ii. Remove an element from the list
iii. Search for an element x in the list and return its pointer

CODE
class Node:
"""A class to represent a node in a circular linked list."""
def __init__(self, data):
self.data = data
self.next = None

class CircularLinkedList:
"""A class to represent a circular linked list."""
def __init__(self):
self.tail = None # Tail points to the last node in the
circular list.

def is_empty(self):
"""Check if the list is empty."""
return self.tail is None

def insert(self, x):


"""Insert an element x into the circular linked list."""
new_node = Node(x)
if self.is_empty():
# If the list is empty, point the new node to itself.
self.tail = new_node
self.tail.next = new_node
else:
# Insert the new node after the current tail and update the
tail.
new_node.next = self.tail.next
self.tail.next = new_node
self.tail = new_node
print(f"Inserted {x} into the list.")

def remove(self, x):


"""Remove an element x from the circular linked list."""
if self.is_empty():
print("The list is empty! Nothing to remove.")
return

current = self.tail.next # Start at the head of the list.


prev = self.tail
while True:
if current.data == x:
if current == self.tail and current.next == self.tail:
# The list has only one node.
self.tail = None
else:
# Remove the current node by updating pointers.
prev.next = current.next
if current == self.tail:
self.tail = prev # Update tail if the last
node is removed.
print(f"Removed {x} from the list.")
return
prev = current
current = current.next
if current == self.tail.next:
# Completed one full cycle, element not found.
print(f"Element {x} not found in the list.")
return

def search(self, x):


"""Search for an element x in the circular linked list and
return its pointer."""
if self.is_empty():
print("The list is empty! Cannot search.")
return None

current = self.tail.next # Start at the head of the list.


while True:
if current.data == x:
print(f"Element {x} found in the list.")
return current
current = current.next
if current == self.tail.next:
# Completed one full cycle, element not found.
print(f"Element {x} not found in the list.")
return None

def display(self):
"""Display all elements in the circular linked list."""
if self.is_empty():
print("The list is empty.")
return

current = self.tail.next # Start at the head of the list.


elements = []
while True:
elements.append(current.data)
current = current.next
if current == self.tail.next:
break
print("List:", " -> ".join(map(str, elements)))

# Example usage of the circular linked list


if __name__ == "__main__":
cll = CircularLinkedList()
cll.insert(10)
cll.insert(20)
cll.insert(30)
cll.display()
cll.search(20)
cll.remove(20)
cll.display()
cll.remove(40) # Element not in the list
cll.search(40)
OUTPUT

You might also like