Question 5 of 9advanced✨ CreateLong Answer5 marks

Write a menu driven python program using queue, to implement movement of shuttlecock in it’s box.

Correct Answer

Menu-driven program to implement shuttlecock movement using queue

Create an empty queue for shuttlecock box

shuttlecockBox = list()

Function to check if queue is empty

def isEmpty(queue): if len(queue) == 0: return True else: return False

Function to add shuttlecock at rear (enqueue)

def enqueue(queue, element): queue.append(element)

Function to remove shuttlecock from front (dequeue)

def dequeue(queue): if isEmpty(queue): print("Box is empty! No shuttlecock to remove.") return None else: return queue.pop(0)

Function to view front shuttlecock (peek)

def peek(queue): if isEmpty(queue): print("Box is empty!") return None else: return queue[0]

Function to count shuttlecocks

def size(queue): return len(queue)

Menu-driven program

while True: print("\n--- Shuttlecock Box Menu ---") print("1. Add Shuttlecock") print("2. Remove Shuttlecock") print("3. View Front Shuttlecock") print("4. Check if Box is Empty") print("5. Count Shuttlecocks") print("6. Exit")

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

if choice == 1:
    item = input("Enter shuttlecock name: ")
    enqueue(shuttlecockBox, item)
    print("Shuttlecock added to box.")

elif choice == 2:
    removed = dequeue(shuttlecockBox)
    if removed != None:
        print("Removed shuttlecock:", removed)

elif choice == 3:
    front = peek(shuttlecockBox)
    if front != None:
        print("Front shuttlecock:", front)

elif choice == 4:
    if isEmpty(shuttlecockBox):
        print("Box is empty!")
    else:
        print("Box is not empty.")

elif choice == 5:
    print("Number of shuttlecocks:", size(shuttlecockBox))

elif choice == 6:
    print("Exiting program.")
    break

else:
    print("Invalid choice!")
Exercise: EXERCISE | Q: 4 | (Page 13)
For More Understanding

Explanation

This program implements a queue data structure using Python list to simulate shuttlecock movement in a box. The queue follows FIFO (First In First Out) principle - shuttlecocks are added at the rear and removed from the front. The program uses append() for enqueue operation and pop(0) for dequeue operation as specified in the textbook context. The menu provides options to add, remove, peek, check empty status, count items, and exit.

Solution Steps

  1. Step 1: Create empty queue using list()

  2. Step 2: Define isEmpty() function to check if queue is empty

  3. Step 3: Define enqueue() using append() to add at rear

  4. Step 4: Define dequeue() using pop(0) to remove from front

  5. Step 5: Define peek() to view front element without removing

  6. Step 6: Define size() using len() to count elements

  7. Step 7: Create while loop with menu options for user interaction