Write a program in python to perfrom following function on Circular Doubly Linked List 1) Create Circular Doubly Linked List 2) Add node 3)Display 4)Delete node 5)Find data in Circular Doubly Linked List

Circular Doubly Linked List

 
class node:
    //To initialize values
    def __init__(self,data=None):
        self.data=data
        self.next=None
        self.prev=None

class linking:
    //To create Linked List 
    def __init__(self):
        n=int(input("Enter How many Nodes You want: "))
        for i in range(1,n+1):
            if i==1:
                self.head = node(input(f"Enter {i} Data: "))
                mur = self.head
                mur.next = mur
                mur.prev = mur
            else:
                newnode = node(input(f"Enter {i} data: "))
                cur = self.head
                mur = self.head
                while cur.next != mur:
                    cur = cur.next
                cur.next = newnode
                newnode.next = mur
                newnode.prev = cur

    //To display elements in Linked List
    def display(self):
        cur=self.head
        mur=cur
        while cur.next != mur:
            print(cur.data,end=" ")
            cur = cur.next
        print(cur.data)

    //To add node at specific position in Linked List
    def add(self,data):
        pos=int(input("Enter Position: "))
        newnode=node(data)
        cur=self.head
        mur=cur
        c=1
        while cur.next!=mur:
            c+=1
            cur=cur.next
        print(c)
        if pos==1:
            cur=self.head
            mur=cur
            self.head=newnode
            #newnode.prev=None

            newnode.next=cur
            cur.prev=newnode
            while cur.next!=mur:
                cur=cur.next
            cur.next=newnode
            newnode.prev=cur

        if pos==c+1:
            cur=self.head
            mur=self.head
            while cur.next!=mur:
                cur=cur.next
            cur.next=newnode
            newnode.prev=cur
            newnode.next=mur
            mur.prev=newnode

        else:
            cur=self.head
            mur=cur.next
            pt=cur
            cn=1
            while cur.next!=pt:
                if pos-1==cn:
                    cur.next=newnode
                    newnode.next=mur
                    mur.prev=newnode
                    newnode.prev=cur
                cur=cur.next
                mur=cur.next
                cn += 1

    //To remove node from specific position in Linked List
    def remove(self,pos):
        cur = self.head
        mur=cur
        c = 1
        while cur.next != mur:
            c += 1
            cur = cur.next

        if pos==1:
            cur=self.head
            mur=cur.next
            pt=mur
            self.head=mur
            while mur.next!=cur:
                mur=mur.next
            mur.next=pt
            pt.prev=mur
            cur.next=None
            cur.prev=None

        if pos==c:
            cur=self.head
            pt=cur
            mur=cur.next

            while mur.next!=pt:
                mur=mur.next
                cur=cur.next
            cur.next=pt
            pt.prev=cur
            mur.prev=None
            mur.next=None

        else:
            cur=self.head
            pt=cur
            mur=cur.next
            cn=1
            while cur.next!=pt:
                if pos-1==cn:
                    cur.next=mur.next
                    mur=mur.next
                    mur.prev=cur
                cur=cur.next
                mur=cur.next
                cn+=1

    //To Find data in Linked List
    def find(self,data):
        cur=self.head
        i=0
        f=0
        while cur.next != None:
            if int(cur.data)==data:
                f=1
                break
            cur = cur.next
            i+=1
        if int(cur.data) == data:
            f = 1
        if f==1:
            print(f"Your Data Found at {i+1}")
        else:
            print("Your Data Not Found......")


//To create menu
while True:
    choose = (int(input("\n1)create\n2)Add node\n3)Display\n4)Delete node\n5)Find\n6)Exit.\nEnter your choice:")))
    if choose == 1:
        mylist = linking()
    if choose == 3:
        mylist.display()
    if choose==6:
        break
    if choose == 2:
        mylist.add(input("Enter Data:"))
    if choose == 4:
        mylist.remove(int(input("Enter Position:")))
    if choose == 5:
        data = int(input("Enter Data: "))
        mylist.find(data)

0 Comments