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

Circular Singly Linked List


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

 class linking:
    //To create Linked List 
    def __init__(self):
        n=int(input('Enter how many nodes:'))
        for i in range(1,n+1):
            if i==1:
                self.head = node(input(f"Enter {i} data: "))
                mur = self.head
                mur.next=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

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

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

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

        if pos == c + 1:
            newnode = node(data)
            cur = self.head
            mur = self.head
            while cur.next != mur:
                cur = cur.next
            cur.next = newnode
            newnode.next = mur

        elif pos == 1:
            cur = self.head
            mur=cur
            newnode = node(data)
            newnode.next = cur
            self.head = newnode
            while cur.next!=mur:
                cur=cur.next
            cur.next=newnode

        else:
            cn = 1
            cur = self.head
            pt=cur
            mur = cur.next
            newnode = node(data)
            while cur.next != pt:
                if pos - 1 == cn:
                    cur.next = newnode
                    newnode.next = mur
                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 = self.head
        c = 1
        while cur.next != mur:
            cur = cur.next
            c = c + 1

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

        elif pos == c:
            cur = self.head
            pt=cur
            mur = cur.next
            while mur.next != pt:
                cur = mur
                mur = cur.next
            cur.next = pt

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

//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==2:
        mylist.add(input("Enter Data:"))
    if choose==3:
        mylist.display()
    if choose==4:
        mylist.remove(int(input("Enter Position:")))
    if choose==5:
        data=int(input("Enter Data: "))
        mylist.find(data)
    if choose==6:
        break


0 Comments