Queues

Computer Science · Data structures

Question 1 of 10

0

    A system that handles tasks in the order they arrived, first come first served, is using:

    Stuck? Read the explanation

    Unlocks at a streak of 5 correct answers in a row.

    Print a free worksheet (PDF)

    Embed this practice on your site or class page

    Paste this HTML into your page. Students practise right there; it is free and needs no account.

    Queues explained

    A queue is a data structure that holds items in first-in, first-out (FIFO) order. The item that has waited longest is the first one removed, just like a line of people at a checkout.

    A queue has two main operations:

    • Enqueue: add an item at the back (the rear).
    • Dequeue: remove and return the item at the front.

    Queues are used wherever things must be handled in arrival order. A printer keeps a queue of print jobs, and a call centre may answer callers in the order they rang. Breadth-first search, a method for exploring a graph, also uses a queue.

    Some courses also cover circular queues, which reuse the space freed at the front of an array, and priority queues, where items leave by priority rather than by arrival time. A priority queue is a different structure from a basic queue.

    A common mistake is to confuse queues with stacks. A queue removes the item that was added first, while a stack removes the item that was added last.

    Worked example

    Start with an empty queue and carry out these operations in order: enqueue A, enqueue B, enqueue C, dequeue, enqueue D, dequeue, dequeue. The front of the queue is on the left.

    1. Enqueue A, B and C: the queue is [A, B, C].
    2. Dequeue returns A, leaving [B, C].
    3. Enqueue D gives [B, C, D].
    4. Dequeue returns B, leaving [C, D]. Dequeue returns C, leaving [D].
    5. The removed items, in order, are A, B and C, and D is still waiting.

    Key facts

    • An array queue stores items in a fixed-size array and keeps a front pointer (next item to remove) and a count of the items stored.
    • In a circular queue the indices wrap round. The slot for the next enqueue is (front+count) mod capacity(\text{front} + \text{count}) \bmod \text{capacity}, and the back item is at (front+count−1) mod capacity(\text{front} + \text{count} - 1) \bmod \text{capacity}.
    • The queue is empty when the count is 0 and full when the count equals the capacity. Adding to a full queue is overflow. Removing from an empty queue is underflow.
    • Without wrap-around, a queue whose pointers only move forward reports "full" even though the front slots are free. Wrap-around fixes this.

    Worked example 2

    A circular queue has capacity 6. It starts empty with front =0= 0. Operations: enqueue 12, 7, 9, 4, 15; dequeue three times; enqueue 8, 20, 3, 11; then enqueue 5.

    1. Five enqueues fill slots 0 to 4 with 12, 7, 9, 4, 15. Count is 5.
    2. Three dequeues remove 12, 7 and 9. Front moves to 3 and count is 2. The queue is 4, 15.
    3. Enqueue 8 goes to slot (3+2) mod 6=5(3 + 2) \bmod 6 = 5. Count is 3.
    4. Enqueue 20 goes to slot (3+3) mod 6=0(3 + 3) \bmod 6 = 0. The pointer has wrapped. Count is 4.
    5. Enqueue 3 goes to slot 1, and 11 goes to slot 2. Count is 6, so the queue is full.
    6. Enqueue 5 fails: this is overflow. Nothing changes.

    Final state: front is 3, count is 6, the back item is at slot (3+6−1) mod 6=2(3 + 6 - 1) \bmod 6 = 2. Reading from front to back: 4, 15, 8, 20, 3, 11.

    Worked example 3

    Round-robin scheduling gives each process a time slice (quantum) of 3 ms. A process that is not finished goes to the back of the queue. Processes A (7 ms), B (2 ms) and C (5 ms) arrive together in that order.

    1. Queue A, B, C. A runs 0 to 3, 4 ms left, and goes to the back: B, C, A.
    2. B runs 3 to 5 and finishes (it needed only 2 ms). Queue: C, A.
    3. C runs 5 to 8, 2 ms left. Queue: A, C.
    4. A runs 8 to 11, 1 ms left. Queue: C, A.
    5. C runs 11 to 13 and finishes. A runs 13 to 14 and finishes.

    Finishing order: B at 5 ms, C at 13 ms, A at 14 ms. Check: the total work is 7+2+5=147 + 2 + 5 = 14 ms, and the CPU was never idle.

    Worked example 4

    A breadth-first search (BFS) uses a queue. A node is marked as seen when it is enqueued. Edges: A–B, A–C, B–D, C–D, C–E, D–F, E–F. Neighbours are taken in alphabetical order, starting at A.

    1. Queue: A. Dequeue A, enqueue its neighbours B and C. Queue: B, C.
    2. Dequeue B. Its unseen neighbour is D. Queue: C, D.
    3. Dequeue C. Its unseen neighbour is E (D is already seen). Queue: D, E.
    4. Dequeue D. Its unseen neighbour is F. Queue: E, F.
    5. Dequeue E, then F. Nothing new is found.

    Visit order: A, B, C, D, E, F. Because the queue handles nodes level by level, the first time a node is found is by a path with the fewest edges. F is first found through D, so the shortest path A to F has 3 edges.

    Common mistakes

    • Writing the queue back to front. Always state which end is the front.
    • Dequeuing from the back, which is stack behaviour.
    • Forgetting to wrap with mod, or giving the next free slot when the question asks for the back item. They differ by one place.
    • Treating "front equals rear" (where rear is the next free slot) as meaning empty. Without a count, it can also mean full.
    • In a queue built from two stacks, moving items across while the out-stack still holds items. Only move when the out-stack is empty, or the order breaks.

    Exam tips

    • Draw the array and write the pointer values after each operation. A table of states in your working is quicker than doing it in your head.
    • Check the final count: enqueues minus successful dequeues.
    • For round-robin, keep a running clock and list the queue after every time slice.
    • When a question asks for "overflow" or "underflow", name the cause: adding to a full queue or removing from an empty one.
    • A linked-list queue needs a tail pointer for enqueue to take constant time.

    What you will practise

    101 multiple-choice questions (Easy, Medium, Hard). Each answer is followed by an explanation.

    Example questions

    • A queue follows:
    • Adding to a queue is called:
    • Removing from a queue is called:
    • Printers use queues because jobs are:
    • A priority queue serves:
    • The operation that reads the front item without removing it is:
    • A deque allows insertion and removal at:
    • Which of these is an everyday example of a queue?

    …and 93 more.