Download as pdf or txt
Download as pdf or txt
You are on page 1of 2

class node:

def __init__(self,data):
self.data=data
self.next=None
self.prev=None

class cll:
def __init__(self):
self.head=None
self.tail=None

def display(self):
temp=self.head
if temp==None:
print("linkly list is empty ")
return
while temp:
print(temp.data)
if temp==self.tail:
break
else:
temp=temp.next

def search(self,key):
temp=self.head
if temp==None:
print("Linked list is empty ")
return
while temp:
if temp.data==key:
print("Key found")
break
elif temp==self.tail:
print("Key not found ")
break
else:
temp=temp.next

def append(self,newnode):
if self.head==None:
self.head=newnode
self.tail=newnode
else:
temp=self.tail
temp.next=newnode
newnode.prev=temp
self.tail=newnode
self.tail.next=self.head

def remove(self,key):
temp=self.head
if temp==None:
print("Linked list is empty")
return
while temp:
if temp.data==key:
if self.head==self.tail:
self.head=None
self.tail=None
break
elif temp==self.head:
self.head=temp.next
break
elif temp==self.tail:
self.tail=temp.prev
break
temp=temp.next
if temp==self.head:
print("Key not found")
break

cLL=cll()
while 1:
print("\n 1.Append \t 2.Display \t 3.Search \t 4.Delete
\n")
choice=int(input("Enter your choice : "))
if choice==1:
data=int(input("Enter an element to insert :"))
cLL.append(node(data))
elif choice==2:
cLL.display()
elif choice==3:
key=int(input("Enter key to search : "))
cLL.search(key)
elif choice==4:
key=int(input("Enter key to delete :"))
cLL.remove(key)
else:
break

You might also like