Insertion and Deletion in Circular Linked List in DSA Python
by DataFlair Team
Program 1
# Implemenation of Circular linked list
class Node:
def __init__(self):
self.data=None
self.add=None
class CircularLinkedList:
def __init__(self):
self.start=None
self.count=0
# create list
def createList(self):
n=int(input("Enter an element"))
self.start=Node()
self.start.data=n
self.start.add=self.start
temp=self.start
self.count=self.count+1
choice=input("Want to continue")
while(choice=="Y" or choice=="y"):
n=int(input("Enter next element"))
newnode=Node()
newnode.data=n
newnode.add=self.start
temp.add=newnode
temp=temp.add
self.count=self.count+1
choice=input("Want to continue")
# Display list
def displayList(self):
if(self.start==None):
print("List not found")
else:
temp=self.start
while(True):
print(temp.data,end=" ")
temp=temp.add
if(temp==self.start):
break
print("\nTotal Node is : ",self.count)
# insert first node
def insertFirst(self):
if(self.start==None):
print("List not found")
else:
n=int(input("Enter an element"))
newnode=Node()
newnode.data=n
newnode.add=None
last=self.start
while(last.add!=self.start):
last=last.add
newnode.add=self.start
self.start=newnode
last.add=self.start
self.count=self.count+1
# insert last node
def insertLast(self):
if(self.start==None):
print("List not found")
else:
n=input("Enter an element for insert")
newnode=Node()
newnode.data=n
newnode.add=None
last=self.start
while(last.add!=self.start):
last=last.add
last.add=newnode
newnode.add=self.start
self.count=self.count+1
# insert middle node
def insertMiddle(self):
if(self.start==None):
print("List is empty")
else:
n=int(input("Enter an element for insert"))
newnode=Node()
newnode.data=n
newnode.add=None
pos=int(input("Enter the position"))
if(pos>self.count):
print("Invalid position")
else:
i=1
next=self.start
while(i<pos):
prev=next
next=next.add
i=i+1
prev.add=newnode
newnode.add=next
self.count=self.count+1
# delete first node
def deleteFirst(self):
if(self.start==None):
print("List not found")
else:
last=self.start
while(last.add!=self.start):
last=last.add
temp=self.start
self.start=self.start.add
last.add=self.start
print("Deleted node is: ",temp.data)
temp.add=None
temp=None
self.count=self.count-1
# Delete node from Middle
def deleteMiddle(self):
if(self.start==None):
print("List is empty")
else:
pos=int(input("Enter the position for delete"))
if(pos>self.count):
print("Invalid position")
else:
i=1
next=self.start
while(i<pos):
prev=next
next=next.add
i=i+1
temp=next
next=next.add
prev.add=next
print("Deleted node is: ",temp.data)
temp=None
self.count=self.count-1
# delete last node
def deleteLast(self):
if(self.start==None):
print("List not found")
else:
last=self.start
while(last.add!=self.start):
prev=last
last=last.add
prev.add=self.start
print("Deleted Node is :",last.data)
last.add=None
last=None
self.count=self.count-1
# Main Method
mylist=CircularLinkedList()
while(1):
print("\n----------------------Circular Linked List Menu--------------\n")
print("1.Create")
print("2.Display")
print("3.Insert First")
print("4.Insert Middle")
print("5.Insert Last")
print("6.Delete First")
print("7.Delete Middle")
print("8.Delete Last")
print("9.Exit")
print("\n------------------------------------------------------------\n")
choice=int(input("Enter your choice"))
if(choice==1):
mylist.createList()
elif(choice==2):
mylist.displayList()
elif(choice==3):
mylist.insertFirst()
elif(choice==4):
mylist.insertMiddle()
elif(choice==5):
mylist.insertLast()
elif(choice==6):
mylist.deleteFirst()
elif(choice==7):
mylist.deleteMiddle()
elif(choice==8):
mylist.deleteLast()
else:
break
Tags: circular linked list in dsa pythondsa pythondsa python practicaldsa python programdsa python program on circular linked listdsa python program on insertion and deletion in circular linked listdsa using pythoninsert and delete in circular linked listinsertion and deletion in circular linked list in dsa python
DataFlair Team
DataFlair Team provides high-impact content on programming, Java, Python, C++, DSA, AI, ML, data Science, Android, Flutter, MERN, Web Development, and technology. We make complex concepts easy to grasp, helping learners of all levels succeed in their tech careers.