Skip to content

Queues

Implement a queue as a circular array with a front index and a size, with rear = (front + size) % capacity, and as a linked list with a head and a tail, so that enqueue and dequeue are O(1) in both.

It opens on

A line of 3, 8 and 5 fills cells 0 to 2 of a five-cell array, and the 3 is served. Must the 8 and 5 slide left, or can they stay?

Why it matters

Print jobs, network packets, the order in which a search visits a graph and every line of customers are queues, and a queue that costs O(n) per dequeue slows to a crawl when thousands of items wait.

Good to know first

  • Stacks: items go in and out at one end in last in, first out order, and pop(0) on a list shifts every item
  • Linked list insertion and deletion: with head and tail, append at the tail and removal at the head are O(1)
  • Values, types and expressions: a % b is the remainder of a divided by b

Step by step

  1. First in, first out

    Use enqueue and dequeue and say the order in which items leave, show that list.pop(0) makes dequeue O(n), and keep a front index so that dequeue moves no item.

    A dequeue gives the 3, because it joined first, and the 8 is then next in line.

    A common mix-up

    The items must slide to the front of the array after a dequeue.

    In fact: Only front changes, so no item moves and dequeue is O(1).

  2. Wrapping round the array

    Write the circular array queue with data, front and size, compute rear = (front + size) % capacity, and trace it through a wrap.

    With front 1 and size 2, rear is (1 + 2) % 5 = 3, so an enqueue of 7 goes in cell 3. With front 1 and size 3, rear is 4, so an enqueue of 2 goes in cell 4.

    the two methods

    A common mix-up

    When the line reaches the last cell the queue is full.

    In fact: It is full only when size equals capacity, and free cells at the left are reached by the % capacity wrap.

  3. A queue on a linked list

    Implement a queue on a singly linked list with a head and a tail, enqueue at the tail and dequeue at the head in O(1), and compare it with the circular array.

    enqueue(7) on 3, 8, 5 makes the next of the node 5 the new node and moves tail onto the node 7, so the line is 3, 8, 5, 7.

    A common mix-up

    After the last dequeue the tail pointer can be left alone.

    In fact: Tail would still name a removed node, so it must be set to None when head becomes None.

Try it

A queue gets enqueue(p), enqueue(q), dequeue(), enqueue(r), dequeue(). What does the second dequeue return, and what is left in the queue?Answer

q; the queue holds only r

Hint: p leaves first, then q

A queue of capacity 7 has front 5 and size 4. Which cell does the next enqueue write, and is the queue full?Answer

Cell 2, and it is not full since size 4 is less than 7

Hint: (5 + 4) % 7

A new linked queue gets enqueue(2), enqueue(9), dequeue(), dequeue(). What are head and tail, and what does the second dequeue return?Answer

Both None, and 9

Hint: The last dequeue empties the queue

From there, an enqueue of 7 and then two dequeues. Which items come out, and which one is left?Answer

The 8 and then the 5 come out, and the 7 is left.

Now front is 1 and size is 4. Which cell does the enqueue of 4 write into?Answer

(1 + 4) % 5 = 0, so cell 0, the one at the start of the array.

Queues

30 minutes every month. No card.