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

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: "))
            else:
                newnode=node(input(f"Enter {i} Data: "))
                cur=self.head
                while cur.next!=None:
                    cur=cur.next
                cur.next=newnode
                newnode.prev=cur
    //To display elements in Linked List
     def display(self):
        cur=self.head
        while cur.next != None:
            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
        c=1
        while cur.next!=None:
            c+=1
            cur=cur.next

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

        else:
            cur=self.head
            mur=cur.next
            cn=1
            while cur.next!=None:
                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
        c = 1
        while cur.next != None:
            c += 1
            cur = cur.next

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

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

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

        else:
            cur=self.head
            mur=cur.next
            cn=1
            while cur.next!=None:
                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