Circular Doubly Linked List in DSA Python
by DataFlair Team
Program 1
# Implementation of circular double linked list
import os
class Node:
def __init__(self):
self.ladd=None
self.data=None
self.radd=None
class CircularDoubleList:
def __init__(self):
self.start=None
self.count=0
def create(self):
n=int(input("Enter first element: "))
self.start=Node()
self.start.ladd=None
self.start.data=n
self.start.radd=None
temp=self.start
self.count=self.count+1
choice=input("Want to continue(y/Y): ")
choice=choice.lower()
while(choice=='y'):
self.count=self.count+1
n=int(input("Enter next element: "))
newnode=Node()
newnode.ladd=None
newnode.data=n
newnode.radd=None
temp.radd=newnode
newnode.ladd=temp
temp=temp.radd
self.start.ladd=newnode
newnode.radd=self.start
choice=input("Want to continue(y/Y): ")
choice=choice.lower()
def display(self):
if(self.start==None):
print("List is empty")
else:
temp=self.start
while(True):
print(temp.data,end=" ")
temp=temp.radd
if(temp==self.start):
break
print("\n Total Node in list: ",self.count)
def insertFirst(self):
if(self.start==None):
print("List is empty")
else:
self.count=self.count+1
n=int(input("Enter next element: "))
newnode=Node()
newnode.ladd=None
newnode.data=n
newnode.radd=None
last=self.start.ladd
self.start.ladd=newnode
newnode.radd=self.start
self.start=newnode
self.start.ladd=last
last.radd=self.start
def insertMiddle(self):
if(self.start==None):
print("List is empty")
else:
self.count=self.count+1
n=int(input("Enter an element: "))
newnode=Node()
newnode.ladd=None
newnode.data=n
newnode.radd=None
pos=int(input("Enter poistion of node: "))
if(pos>self.count):
print("Poistion is grater than total node: ")
else:
i=1
next=self.start
while(i<pos):
prev=next
next=next.radd
i=i+1
prev.radd=newnode
newnode.ladd=prev
newnode.radd=next
next.ladd=newnode
def insertLast(self):
if(self.start==None):
print("List is empty")
else:
self.count=self.count+1
n=int(input("Enter an element: "))
newnode=Node()
newnode.ladd=None
newnode.data=n
newnode.radd=None
last=self.start.ladd
last.radd=newnode
newnode.ladd=last
self.start.ladd=newnode
newnode.radd=self.start
def deleteFirst(self):
if(self.start==None):
print("List is empty")
else:
temp=self.start
last=self.start.ladd
self.start=self.start.radd
self.start.ladd=last
last.radd=self.start
print(" Deleted node is ",temp.data)
del temp
temp=None
self.count=self.count-1
def deleteLast(self):
if(self.start==None):
print("List is empty")
else:
last=self.start.ladd
prev=last.ladd
self.start.ladd=prev
prev.radd=self.start
print("Deleted node is : ",last.data)
del last
last=None
self.count=self.count-1
def deleteMiddle(self):
if(self.start==None):
print("List is empty")
else:
pos=int(input("Enter poistion of node for delete: "))
if(pos>self.count):
print("Poistion is grater than total node: ")
else:
self.count=self.count-1
i=1
next=self.start
while(i<pos):
prev=next
next=next.radd
i=i+1
temp=next
next=next.radd
next.ladd=prev
prev.radd=next
print("Deleted node is : ",temp.data)
del temp
temp=None
# Main
os.system('cls')
cdll=CircularDoubleList()
while(1):
print("\n --------------------DoubleLinked List------------------------")
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("---------------------------------------------------------")
choice=int(input("Enter your choice: "))
if(choice==1):
cdll.create()
elif(choice==2):
cdll.display()
elif(choice==3):
cdll.insertFirst()
elif(choice==4):
cdll.insertMiddle()
elif(choice==5):
cdll.insertLast()
elif(choice==6):
cdll.deleteFirst()
elif(choice==7):
cdll.deleteMiddle()
elif(choice==8):
cdll.deleteLast()
else:
break
Tags: circular doubly linked listcircular doubly linked list in dsa pythoncircular doubly linked list in pythondsa pythondsa python circular doubly linked listdsa python practicaldsa python programdsa python program on circular doubly linked listdsa python tutorial
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.