Queue Implementation using Python
Code is given below.
#QUEUE
class queue:
def __init__(self):
self.l1=[]
def add(self,data):
if data not in self.l1:
self.l1.append(data)
else:
print("Data is alreday there in queue....")
def remove(self):
self.l1.pop(0)
def display(self):
for i in self.l1:
print(i,end=' ')
print()
A=queue()
while True:
print("\nPress\n1.ADD ITEM\n2.REMOVE\n3.DISPLAY\n4.EXIT\n")
ch=int(input("Enter your choice : "))
if ch==1:
number=int(input("Enter the number : "))
A.add(number)
elif ch==2:
A.remove()
elif ch==3:
print("Queue Elements are....")
A.display()
elif ch==4:
break
#Doble Ended Queue
class doubleEndedQueue:
def __init__(self):
self.l1=[]
def addAtFront(self,data):
if data not in self.l1:
self.l1.insert(0,data)
else:
print("Data is alreday there in queue....")
def addAtEnd(self,data):
if data not in self.l1:
self.l1.append(data)
else:
print("Data is already there in queue....")
def deleteAtFront(self):
self.l1.pop(0)
def deleteAtEnd(self):
self.l1.pop(len(self.l1)-1)
def display(self):
for i in self.l1:
print(i,end=' ')
print()
A=doubleEndedQueue()
while True:
print("\nPress\n1.Add at front\n2.Add at end\n3.Remove at front\n4.Remove at end\n5.DISPLAY\n6.EXIT\n")
ch=int(input("Enter your choice : "))
if ch==1:
number=int(input("Enter the number : "))
A.addAtFront(number)
elif ch==2:
number=int(input("Enter the number : "))
A.addAtEnd(number)
elif ch==3:
A.deleteAtFront()
elif ch==4:
A.deleteAtEnd()
elif ch==5:
A.display()
elif ch==6:
break
else:
pass
Comments
Post a Comment