Question 7 of 9intermediate🔧 ApplyShort Answer4 marks

Show the status of queue after each operation enqueue(34) enqueue(54) dequeue() enqueue(12) dequeue() enqueue(61) peek() dequeue() dequeue() dequeue() enqueue(1)

Correct Answer

Initial Status: Queue is Empty

Step 1: enqueue(34) → Queue: [34] 34 is inserted at the rear end.

Step 2: enqueue(54) → Queue: [34, 54] 54 is inserted at the rear end.

Step 3: dequeue() → Queue: [54] Element 34 is removed from the front.

Step 4: enqueue(12) → Queue: [54, 12] 12 is inserted at the rear end.

Step 5: dequeue() → Queue: [12] Element 54 is removed from the front.

Step 6: enqueue(61) → Queue: [12, 61] 61 is inserted at the rear end.

Step 7: peek() → Queue: [12, 61] Returns element at front (12) without removing it.

Step 8: dequeue() → Queue: [61] Element 12 is removed from the front.

Step 9: dequeue() → Queue: [] (Empty) Element 61 is removed from the front.

Step 10: dequeue() → Underflow Exception Cannot delete from an empty queue.

Step 11: enqueue(1) → Queue: [1] 1 is inserted at the rear end.

Exercise: EXERCISE | Q: 6 | (Page 13)
For More Understanding

Explanation

This question tests understanding of queue operations following FIFO (First In First Out) principle. Enqueue adds elements at rear, dequeue removes from front. The peek operation only views the front element without removal. The critical point is Step 10 where dequeue on empty queue causes Underflow exception, as mentioned in the textbook context.

Solution Steps

  1. Step 1: Apply enqueue(34) - Insert 34 at rear, Queue: [34]

  2. Step 2: Apply enqueue(54) - Insert 54 at rear, Queue: [34, 54]

  3. Step 3: Apply dequeue() - Remove 34 from front, Queue: [54]

  4. Step 4: Apply enqueue(12) - Insert 12 at rear, Queue: [54, 12]

  5. Step 5: Apply dequeue() - Remove 54 from front, Queue: [12]

  6. Step 6: Apply enqueue(61) - Insert 61 at rear, Queue: [12, 61]

  7. Step 7: Apply peek() - View front (12), Queue unchanged: [12, 61]

  8. Step 8: Apply dequeue() - Remove 12 from front, Queue: [61]

  9. Step 9: Apply dequeue() - Remove 61 from front, Queue: [] (Empty)

  10. Step 10: Apply dequeue() - Underflow! Cannot delete from empty queue

  11. Step 11: Apply enqueue(1) - Insert 1 at rear, Queue: [1]