Quiz Entry - updated: 2026.09.29
Starting from an empty queue, what do enqueue(5), enqueue(3), dequeue(), enqueue(7), dequeue(), first(), dequeue(), dequeue() return?
dequeue() → 5, dequeue() → 3, first() → 7, dequeue() → 7, and the last dequeue() → null because the queue is empty.
* dequeue always takes the oldest element still waiting. *
| Operation | Returns | Queue (front … rear) |
|---|---|---|
enqueue(5) |
— | (5) |
enqueue(3) |
— | (5, 3) |
dequeue() |
5 | (3) |
enqueue(7) |
— | (3, 7) |
dequeue() |
3 | (7) |
first() |
7 | (7) |
dequeue() |
7 | () |
dequeue() |
null |
() |
Compare with the same kind of sequence on a stack: there, the first removal would have returned 3, the most recent element. Here it returns 5, the oldest. first() peeks without removing, so the following dequeue() returns the same 7. On an empty queue, this ADT returns null rather than throwing.