Programmin-in-Python

Queue Implementation

Dec 22nd, 2020
116
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 1.46 KB | None | 0 0
  1. def enqueue(queue, item):
  2.     queue.append(item)
  3.  
  4. def dequeue(queue):
  5.     if any(queue):
  6.         print("Item De-queued : ", queue.pop(0))
  7.     else : print("[WARNING] Underflow!!!")
  8.  
  9. def peek(queue):
  10.     if any(queue):
  11.         for i in range(len(queue)):
  12.             if i == 0:
  13.                 print(queue[i], " <== FRONT")
  14.             elif i == len(queue)-1 :
  15.                 print(queue[i], " <== REAR")
  16.             else: print(queue[i], end="\n")
  17.     else: print("[WARNING] Queue is EMPTY!!!")
  18.  
  19. def main():
  20.     queue = []
  21.  
  22.     print("The Queue is Empty now..\nPlease Enter 5 items into it...")
  23.  
  24.     for i in range(5):
  25.         inp = input("Enter an Item : ")
  26.         queue.append(inp)
  27.  
  28.     print("[SUCCESS] 5 Items Successfully added to it.")
  29.     print("\nPlease Enter the Operation that you want to do on it...")
  30.  
  31.     while True:
  32.         print("\n1. Enqueue\n2. Dequeue\n3. Peek\n4. Display Queue\n5. Exit")
  33.  
  34.         choice = int(input("Enter the Choice (Number only) : "))
  35.  
  36.         if choice == 1:
  37.             item = input("Enter the Item to be pushed : ")
  38.             enqueue(queue, item)
  39.             print("[SUCCESS] Item Successfully En-queued into the queue.")
  40.         elif choice == 2 : dequeue(queue)
  41.         elif choice == 3 : peek(queue)
  42.         elif choice == 4 :
  43.             for i in queue : print(i)
  44.         elif choice == 5 :
  45.             print("Exiting...")
  46.             break
  47.         else : print("[ERROR] Invalid Choice")
  48. main()
Advertisement
Add Comment
Please, Sign In to add comment