Queue in Python

Hey internet mates here we are going to make a python program to demonstrate working of queue in python.

Program code:-
def cls():
    print("\n" * 100)
def isEmpty(Qu):
    if Qu == [] :
        return True
    else:
     return False

def Enqueue (Qu, item):
    Qu.append(item)
    if len (Qu)==1:
        front = rear = 6
    else :
        rear=len(Qu)-1
def Dequeue(Qu):
    if isEmpty(Qu):
        return "Underflow"
    else:
        item=Qu.pop(0)
    if len(Qu)==0:
        front=rear=None

#if it was single-element queue
    return item

def Peek (Qu):
    if isEmpty(Qu):
        return Underflow
    else:
        front=0
    return Qu[front]
def Display (Qu):
    if isEmpty (Qu):
        print("Queue Empty!")
    elif len(Qu) == 1:
        print(Qu[0], "<==front, rear")
    else:
        front =0
        rear=len(Qu)-1
        print(Queue[front]," <-front")
        for a in range(1, rear ):
            print(Qu[a])
        print(Queue[rear], "<-rear")

#_main_program

queue = []
front=None
while True:
    cls()
    print("QUEUE OPERATIONS")
    print("1. Enqueue")
    print("2.Dequeue")
    print("3. Peek")
    print("4. Display queue")
    print("5. Exit")
    ch=int (input("Enter your choice ( 1-5): "))
    if ch==1:
        item= int(input("Enter item :"))
        Enqueue (queue, item)
        input("Press Enter to continue...")
    elif ch==2:
        item = Dequeue (queue)
        if item== ("Underflow"):
            print("Underflow Queue is empty!")
        else:
            print("Dequeue-ed item is", item)
        input("Press enter to continue...")
    elif ch==3:
        item=Peek (queue)
        if item=="Underflow":
            print("Queue is empty!")
        else :
            print("Frontmost item is", item)
        input("Press Enter to continue...")
    elif ch == 4:
        Display(queue)
        input("Press Enter to continue...")
    elif ch == 5:
        break
    else:
        print("Invalid choice!")
        input("Press Enter to continue...")

1 thought on “Queue in Python”

Leave a Comment

Your email address will not be published. Required fields are marked *