Queue
A queue is a linear data structure that adds elements at the back and removes them from the front, so the first value in is the first value out (FIFO). enqueue appends to one end and dequeue removes from the other, preserving arrival order as service order.
A coffee line works the same way: new customers join at the back, and the person at the front is served next. Because items leave in arrival order, schedulers, message buffers, print spoolers, and breadth-first search rely on queues so earlier work is never bypassed by later work.
How a Queue Works
A queue exposes two operations at opposite ends. enqueue(val) appends val at the back; dequeue() removes and returns the element at the front. The animated code implements a bounded queue: calling enqueue when the queue already holds max elements raises Queue Full (overflow), and calling dequeue on an empty queue raises Queue Empty (underflow). Both guards appear directly in the displayed class, and a rejected enqueue leaves the queue unchanged.
Compare this with a stack, which pushes and pops at the same end. Moving removal to the same end as insertion turns a queue into a stack; separating insertion and removal onto opposite ends is what distinguishes FIFO from LIFO.
The displayed Python class stores elements in a plain list and dequeues with pop(0), which shifts every remaining element left by one in O(n) time rather than O(1). The FIFO contract matches a standard queue, while the list keeps the teaching implementation short. Production queues use a circular buffer (head and tail indices wrapping a fixed array) or collections.deque, serving both ends in O(1) time.
Queue Operations Step by Step
The lesson starts from the default state: an empty queue with capacity 8. Run this sequence from the Inputs panel.
enqueue(3): 3 joins the back. The queue is[3]; front and back point to the same element.enqueue(7): 7 lines up behind 3. The queue is[3, 7].enqueue(1): the queue is[3, 7, 1].dequeue(): the front leaves first, returning 3 (the value that arrived first). The queue is[7, 1].dequeue(): returns 7. The queue is[1].dequeue(): returns 1. The queue is[]again.
Once those six operations finish, the queue is empty. Running one more dequeue() raises Queue Empty and changes nothing (the underflow guard, which the lesson handles even though the seeded demo stops before it).
The departure sequence is 3, 7, 1, matching the exact arrival order guaranteed by FIFO.
On the canvas, the same trace runs with a FRONT arrow pinned under the first cell and a BACK arrow tracking the last. Each dequeue slides the front cell off to the left while the remaining cells shift one slot over, and the header's size, front, and back readouts update on every beat to match the states above.
Time and Space Complexity
The abstract queue supports O(1) operations at both ends; the runtime you get depends on the storage underneath.
| Operation | Displayed list version | Deque / circular buffer |
|---|---|---|
enqueue | O(1) amortized | O(1) |
dequeue | O(n) (pop(0) shifts remaining elements) | O(1) |
| Read front | O(1) | O(1) |
The list and a deque use O(n) space for the n elements currently waiting. A fixed-capacity circular buffer allocates all C slots up front, so it uses O(C) space regardless of how many elements are waiting.
Edge Cases
- Enqueue when full: raises overflow. The bound is enforced in the displayed code (
maxis checked beforeQueue Fullis raised), and bounded queues serve as a back-pressure mechanism. The capacity slider stopping at 10 is only a canvas-width limit of the visualization, not a property of queues. - Dequeue when empty: raises underflow. Guard with an emptiness check or catch the exception; some APIs return a sentinel instead, depending on whether an empty queue is expected control flow or an error.
- Single element: front and back point at the same cell; one dequeue returns it and empties the queue.
- Drain without refill: repeated dequeues replay arrival order until underflow.
Common Mistakes
pop(0)in production. Easy to write, O(n) to run. Usecollections.dequeonce the queue grows beyond a small demo.- Mixing the ends. Dequeuing from the back silently turns the queue into a stack, breaking FIFO without raising an error.
- Assuming a priority queue is a queue. A priority queue serves the highest-priority item next rather than the earliest arrival; it is usually backed by a heap and does not guarantee FIFO order.
- Two-stacks queue, refilled too eagerly. The classic two-stack construction is amortized O(1) only if you refill the out-stack when it is empty; refilling on every pop degrades to O(n).
Keep exploring
A few lessons that share the same technique or lead into the next idea.
Embed this lesson in a blog, docs, or course?
Found a mistake or something unclear?