Site icon DataFlair

Deletion in Circular Doubly Linked List in DSA Python

Program 1

# implementation of doubly Circular Linked list
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
          # method for create list
     def createList(self):
         n=int(input("Enter an 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: ")
         while(choice=="Y" or choice=="y"):
              n=int(input("Enter next element"))
              newnode=Node()
              newnode.ladd=None
              newnode.data=n
              newnode.radd=None
              temp.radd=newnode
              newnode.ladd=temp
              self.start.ladd=newnode
              newnode.radd=self.start
              temp=temp.radd
              self.count=self.count+1
              choice=input("Want to continue: ")
      
      # method for display    
     def displayList(self):
         if(self.start==None):
             print("List not found")
         else:
             temp=self.start
             print()
             while(1):
                 print(temp.data,end=" ") 
                 temp=temp.radd
                 if(temp==self.start):
                     break   
             print("\nTotal Node is : " ,self.count)         

       # method for reverse display    
     def reversedisplayList(self):
          if(self.start==None):
             print("List not found")
          else: 
           temp=self.start.ladd 
           temp1=self.start.ladd 
           while(1):
               print(temp.data,end=" ")
               temp=temp.ladd
               if(temp==temp1):
                   break

      # method for insert first         
     def insertFirst(self):
         if(self.start==None):
             print("List not found")
         else:
             last=self.start
             last=last.ladd
             n=int(input("Enter an element for insert"))    
             newnode=Node()
             newnode.ladd=None
             newnode.data=n
             newnode.radd=None
             newnode.radd=self.start
             self.start=ladd=newnode
             self.start=newnode
             self.start.ladd=last
             last.radd=self.start
             self.count=self.count+1
     # method for insert last      
     def insertLast(self):
         if(self.start==None):
             print("List not found")
         else:
             last=self.start.ladd 
             n=int(input("Enter an element"))   
             newnode=Node()
             newnode.ladd=None
             newnode.data=n
             newnode.radd=None
             last.radd=newnode
             newnode.ladd=last
             self.start.ladd=newnode
             newnode.radd=self.start
             self.count=self.count+1

     # Method for delete last
     def deleteLast(self):
             if(self.start==None):
                 print("List not found")
             else:
                 temp=self.start.ladd
                 prev=temp.ladd
                 self.start.ladd=prev
                 prev.radd=self.start
                 print("Delete node is: ",temp.data)
                 temp.ladd=None
                 temp.radd=None
                 temp=None
                 self.count=self.count-1


       # Method for delete middle node
     def deleteMiddle(self):
           if(self.start==None):
                print("List not found")
           else:
                pos=int(input("Enter middle position for delete"))
                if(pos>self.count):
                     print("Invalid postion total node is: ",self.count)
                else:
                     i=1
                     temp=self.start
                     while(i<pos):
                        temp=temp.radd     
                        i=i+1
                     prev=temp.ladd
                     next=temp.radd   
                     prev.radd=next
                     next.ladd=prev     
                     print("Deleted node is:",temp.data)
                     temp.radd=None
                     temp.ladd=None
                     temp=None
                     self.count=self.count-1

     # Method for delete First
     def deleteFirst(self):
         if(self.start==None):
             print("List not found")
         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)
             temp.ladd=None
             temp.radd=None
             temp=None    
             self.count=self.count-1

       #Method for insert middle position
     def insertMiddle(self):
           if(self.start==None):
                print("List not found")
           else:
                n=int(input("Enter an element for insert at middle"))     
                newnode=Node()
                newnode.ladd=None
                newnode.data=n
                newnode.radd=None
                pos=int(input("Enter middle position"))
                if(pos>self.count):
                     print("Invalid position total node only: ",self.count)
                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
                     self.count=self.count+1

# Main Menu
mylist=CircularDoubleList()
while(1):
    print("------------------Circular Doubly Linked List-----------------")
    print("1.Create")
    print("2.Display")
    print("3.Reverse Display")
    print("4.Insert First")
    print("5.Insert Last")
    print("6.Insert Middle")
    print("7.Delete First")
    print("8.Delete Last")
    print("9.Delete Middle")
    print("10.Exit")
    print("----------------------------------------------------------------")
    choice=int(input("Enter your choice"))
    if(choice==1):
        mylist.createList()
    elif(choice==2):
        mylist.displayList()
    elif(choice==3):
        mylist.reversedisplayList()
    elif(choice==4):
        mylist.insertFirst()
    elif(choice==5):
        mylist.insertLast()
    elif(choice==6):
        mylist.insertMiddle()
    elif(choice==7):
        mylist.deleteFirst()
    elif(choice==8):
        mylist.deleteLast()
    elif(choice==9):
        mylist.deleteMiddle()
    else:
        break

 

Exit mobile version