Sunday, 23 August 2020

Write a Python program to implement a queue using a list data-structure.

 PROGRAM:


OUTPUT:

    

 

  

   


#Queue Operation in a list

#Prepared by RKS

def isEmpty(Qu):

    if Qu==[]:

        return True

    else:

        return False


def Enqueue(Qu, item):

    Qu.append(item)

    if len(Qu)==1:

        front=rear=0

    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

        return item

def Peek(Qu):

    if isEmpty(Qu):

        return "Underflow"

    else:

        front=0

        return Qu[front]


def disp(Qu):

    if isEmpty(Qu):

        print("queue Empty")

    elif len(Qu)==1:

        print(Qu[0],"<==Front, Rear")

    else:

        front=0

        rear=len(Qu)-1

        print(Qu[front],"<-Front")

        for a in range(1,rear):

            print(Qu[a])

        print(Qu[rear],"<-Rear")

queue=[]

front=None

while True:

    print("\n\nQueue Operations:")

    print("1. Insert element into the queue")

    print("2. Delete element from the queue")

    print("3. Peek(Display the front element)")

    print("4. Display Queue")

    print("5. Exit")

    ch=int(input("Enter your choice : "))

    if ch==1:

        item=int(input("Enter Item:"))

        Enqueue(queue,item)

    elif ch==2:

        item=Dequeue(queue)

        if item=="Underflow":

            print("queue is empty")

        else:

            print("Dequeued item is : ",item)

    elif ch==3:

        item=Peek(queue)

        if item=="Underflow":

            print("queue is empty")

        else:

            print("Topmost item is : ",item)

    elif ch==4:

        disp(queue)

    elif ch==5:

        break

    else:

        print("Invalid Choice ")

            

 



No comments:

Post a Comment