Queues: First In, First Out
A collection where values leave in the order they arrived: enqueue at the back, dequeue at the front. The circular queue keeps the front from drifting off the end of the storage.
What a learner can do afterwards
- Trace a queue through a run of enqueue and dequeue operations
- Give the order the same four values come back out of a queue and out of a stack
- Explain why a print spooler or a network buffer needs arrival order kept
1 · Read
A queue is a line you join at the back and leave from the front. Enqueue adds an item at the rear and dequeue removes the item at the front, so the first thing in is always the first thing out. That is the whole rule: first in, first out, no overtaking allowed.
To trace a queue, write the line left to right after every operation and cross off from the front only. Try it: enqueue 4, enqueue 7, dequeue, enqueue 9 leaves 7 at the front with 9 behind. Run the same four values through a stack and through a queue and the outputs come back in opposite orders. Dequeueing an empty queue is an error. An editor undo needs newest first, so it uses a stack, not a queue.
A print spooler is a queue: several people send documents at once but the printer prints one page at a time. Each document enqueues at the back and the printer dequeues from the front, so jobs print in arrival order and nobody jumps ahead. When a front job jams, everything behind it waits: cancel the stuck job or restart the spooler and the line flows again in order. Network buffers work the same way, holding bursts so nothing is lost or reordered.
In Python a list can act as a small queue. Removing from the front shifts everything else along, which is fine for learning but slow for huge lines, and that cost is why real programs use a dedicated double ended queue. For tracing by hand, the list version is fine.
First in, first out: join at the back, leave from the front.
2 · Watch
Take it off screen
Where it sits
8 questions wait behind this lesson, each with its answer explained. Every answer feeds the sky: stars light as they are learned, and dim when it is time to come back.