All pages 4 of 8

The queue

A line at a counter where people join at the back and leave from the front, each in two quick steps.

bakeryfirstlast
In life: First in line is first served.
012345672961837headtail
7 joins the back of the line.

Counted: 2 steps

Work the machine

The steps

  1. Write 7 on tile 7, where the tail hand points.
  2. Tile 7 is the last, so the tail hand wraps round to tile 0.
  3. 7 joins the back of the line.
The machine, at the end of the worked example. Press a button to make a move and watch it, one step at a time.
find: about the same, whatever the size1 stepadd: about the same, whatever the size2 stepsremove: about the same, whatever the size2 steps
The costs, with 8 things inside, counted by running the machine above: one box a step.

What it is

Think of a line at a bakery. New people join at the back. The person at the front is served and leaves. Nobody cuts in.

This is called a queue. Joining the back is often called enqueue, and leaving the front is called dequeue. The first thing in is the first thing out.

How a move works

Under this line is a ring of 8 tiles, numbered 0 to 7. A head hand points at the front of the line. A tail hand points at the first free tile, just behind the back.

To join, write the number on the tile under the tail hand, then move the hand on one tile. That is 2 steps. To leave, take the number under the head hand, then move that hand on. That is 2 steps too.

Join 5, 2 and 9, and let 5 leave. Then 6, 1, 8, 3 and 7 join. When 7 lands on tile 7, the tail hand wraps round to tile 0. The line reads 2, 9, 6, 1, 8, 3, 7.

Where you meet it

Printers keep their jobs in a queue. So do the songs on a playlist and the messages waiting to be sent. A computer keeps the keys you press in a queue, so the letters come out in the order you typed them.

Watch out for

If you kept the line in a plain row of tiles, it would walk off the end. People leave the front and join the back, so the line creeps along. The ring lets it wrap round and use the free tiles at the start again.