Queues
Computer Science · Data structures
Question 1 of 10
0A 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.
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.
- Enqueue A, B and C: the queue is [A, B, C].
- Dequeue returns A, leaving [B, C].
- Enqueue D gives [B, C, D].
- Dequeue returns B, leaving [C, D]. Dequeue returns C, leaving [D].
- 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 , and the back item is at .
- 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 . Operations: enqueue 12, 7, 9, 4, 15; dequeue three times; enqueue 8, 20, 3, 11; then enqueue 5.
- Five enqueues fill slots 0 to 4 with 12, 7, 9, 4, 15. Count is 5.
- Three dequeues remove 12, 7 and 9. Front moves to 3 and count is 2. The queue is 4, 15.
- Enqueue 8 goes to slot . Count is 3.
- Enqueue 20 goes to slot . The pointer has wrapped. Count is 4.
- Enqueue 3 goes to slot 1, and 11 goes to slot 2. Count is 6, so the queue is full.
- Enqueue 5 fails: this is overflow. Nothing changes.
Final state: front is 3, count is 6, the back item is at slot . 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.
- Queue A, B, C. A runs 0 to 3, 4 ms left, and goes to the back: B, C, A.
- B runs 3 to 5 and finishes (it needed only 2 ms). Queue: C, A.
- C runs 5 to 8, 2 ms left. Queue: A, C.
- A runs 8 to 11, 1 ms left. Queue: C, A.
- 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 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.
- Queue: A. Dequeue A, enqueue its neighbours B and C. Queue: B, C.
- Dequeue B. Its unseen neighbour is D. Queue: C, D.
- Dequeue C. Its unseen neighbour is E (D is already seen). Queue: D, E.
- Dequeue D. Its unseen neighbour is F. Queue: E, F.
- 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.