Priority Queue using List in DSA Python

Program 1

# Implementation of priority queue using list
pq=[]  #empty list

def qinsert():
    n=int(input("Enter an element"))
    pq.append(n)
 
def qdelete():
    if(len(pq)==0):
        print("Priority Queue is empty")  
    else:
         pq.sort()
         print("Deleted element is: ",(pq[0])) 
         del pq[0]  

def qdisplay():
     if(len(pq)==0):
        print("Priority Queue is empty")  
     else:
        print("Elements of queue: ")
        for element in pq:
            print(element) 

while(1):
    print("---------------------Priority Queue Menu---------------------")
    print(" 1.Insert \n 2.Delete \n 3.Display \n 4.Exit")
    print("--------------------------------------------------------")
    choice=int(input("Enter your choice"))
    if(choice==1):
        qinsert()
    elif(choice==2):
        qdelete()
    elif(choice==3):
        qdisplay()
    elif(choice==4):
        break    
    else:
        print("Invalid choice")

 

courses

DataFlair Team

DataFlair Team provides high-impact content on programming, Java, Python, C++, DSA, AI, ML, data Science, Android, Flutter, MERN, Web Development, and technology. We make complex concepts easy to grasp, helping learners of all levels succeed in their tech careers.

Leave a Reply

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