Priority Queue using Module in DSA Python

Program 1

# Implementation of Priority Queue using Module
import queue
pq=queue.PriorityQueue() # Object

# insert function
def qinsert():
    n=int(input("Enter an element for insert"))
    pq.put(n)

# delete function
def qdelete():
      if(pq.empty()):
          print("Priority Queue is empty")
      else:   
          print("Deleted element is: ",pq.get())

# display function
def qdisplay():
       if(pq.empty()):
          print("Priority Queue is empty")
       else:   
        print(pq.queue)

# Main menu
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 *