Queues: first in, first out
After this: Say what a queue is, and why the front of an array makes a slow queue.
On this page · 7 sections · 1 check
A stack gives back the newest value first. That is right for undo, but it would be unfair to people waiting for their turn.
Three people send a document to the same printer. The person who sent first should get their pages first, and nobody should jump ahead. This lesson is about the structure that keeps things in that order.
What a queue is
Picture people waiting in a line at a shop. New people join at the back. The person at the front is served and leaves.
- Ana joins. The line is Ana.
- Ben joins at the back. The line is Ana, Ben.
- Cho joins at the back. The line is Ana, Ben, Cho.
- The shop serves the front of the line. Ana leaves. The line is Ben, Cho.
- Dev joins at the back. The line is Ben, Cho, Dev.
- The shop serves the front again. Ben leaves.
A is a line like this, made of values instead of people. You can do two things with it.
- adds a value at the back.
- takes the value at the front and gives it back to you.
First in, first out
A stack and a queue differ only in which value comes out. Put in 1, then 2, then 3, and take all three out.
| Structure | Values come out as | Which goes first |
|---|---|---|
| Stack | 3, 2, 1 | the newest |
| Queue | 1, 2, 3 | the oldest |
The queue's rule is called first in, first out, or for short.
Queues appear wherever work has to wait its turn: pages waiting for a printer, or requests waiting for a web server. Later tutorials also use a queue to go through trees and graphs layer by layer.
The slow way: a plain array
Let us try the first idea that comes to mind. Keep the queue in an array, with the front of the line at index 0.
Enqueue works well. It is an append: write the value at the end of the row.
Dequeue is the problem. Take the array [1, 2, 3, 4, 5] and dequeue once.
- Read 1 from index 0. That leaves a gap at the front.
- Move 2 to index 0.
- Move 3 to index 1.
- Move 4 to index 2.
- Move 5 to index 3.
The array is now [2, 3, 4, 5]. Removing one value took four moves. This is the same shifting you saw in Inserting and deleting in the middle.
With n values in the queue, one dequeue makes n − 1 moves. That is O(n), which means the work grows with the size of the queue. The lesson Counting steps explains O(n).
The moves add up
To empty a queue of five values, the dequeues make 4 + 3 + 2 + 1 = 10 moves.
| Values in the queue | Moves to empty it |
|---|---|
| 5 | 10 |
| 1,000 | 499,500 |
| 1,000,000 | 499,999,500,000 |
The total is written O(n²), which means that when the queue doubles in size, the work becomes about four times as big.
Watch it happen
Empty the queue from the front, one dequeue at a time. Each dequeue here is a remove at index 0, so add up the moves as you go.
- Remove index 0Dequeue. The 1 leaves, and the other four values each move one cell left.
- Remove index 0
- Remove index 0
- Remove index 0
- Remove index 0
- 01
- 12
- 23
- 34
- 45
Run an operation to begin.
The moves were 4, 3, 2, 1 and 0, which makes the 10 from the table.
Check yourself
A queue is built on a plain array. Enqueue appends at the end, and dequeue removes index 0. What does each cost, and why?
Connect
- Say what a queue is: a line of values where the first value in is the first value out.
- Say why a queue on a plain array is slow: taking from the front moves every other value.
- Add up the moves to empty a slow queue, and say why the total is O(n²).