Edexcel International Advanced Level Computer Science, point 4.3.2
Definition
A queue is an abstract data type in which items are added at one end (the rear or back) and removed from the other end (the front). The first item added is the first removed: FIFO (first in, first out), like people waiting in a line.
Function and characteristics
- It keeps items in the order they arrived, so they are served fairly.
- Two pointers track the ends: front (where items leave) and rear (where items join). The book calls them the head pointer and the tail pointer, and some questions say push and pop for a queue too.
- It may have a fixed maximum size, so it can become full.
- It can be built from an array or list (8.2.6), a linked list, or as a circular queue (14.1.5).
- Insert and delete are fast (constant time).
a. Structure
With an array:
| Index | 0 | 1 | 2 | 3 | 4 |
|---|---|---|---|---|---|
| Item | 4 | 9 | 2 | 7 | |
| front = 0 | rear = 3 |
b. Operations
| Operation | What it does | Typical name |
|---|---|---|
| Insert | Add an item to the rear. If the queue is full, it's an overflow | enqueue(item) |
| Delete | Remove and return the front item. If the queue is empty, it's an underflow | dequeue() |
| Search | Look at the front item without removing it. There's no way to search beyond the front: to reach an item further back, the items in front of it would have to be dequeued first | peek() |
| (helpers) | Is it empty? Is it full? | isEmpty(), isFull() |
Enqueue (insert an item):
- Check whether the queue is full. If it is, report an error and stop.
- Add 1 to the rear pointer, so it points to the next free position.
- Store the item at the position the rear pointer now gives.
Dequeue (delete the front item):
- Check whether the queue is empty. If it is, report an error and stop.
- Output the item at the position the front pointer gives.
- Add 1 to the front pointer, so it points to the next item in the queue.
Peek reads the item at the front pointer without changing either pointer.
With an array, a linear queue is empty when head > tail, and full when tail = size − 1. That is why it can seem full although there is room: once the tail reaches the last slot, the queue counts as full even if items have left the front.
Problem with a simple array queue: as items are removed, front moves along, and the space at the start is never reused, so the queue seems full although there is room. A circular queue fixes this by wrapping the pointers round (14.1.5).
Searching (looking for an item behind the front) would mean dequeuing items until it is found, which is slow and not what a queue is for, so a queue has no search operation beyond peek().
Uses
- Print queues (jobs printed in the order sent).
- Keyboard and I/O buffers.
- Process scheduling (first-come first-served, 1.2.3).
- Waiting lists and customer service queues.
- Breadth-first search of a graph or tree.
- Network packets waiting to be sent.
Benefits
- Fair: first come, first served.
- Simple, and enqueue and dequeue are fast.
- Natural for buffering between a fast producer and a slow consumer.
Drawbacks
- Only the front and rear are reachable, so searching is slow.
- A fixed-size queue can overflow, and a simple array version wastes space (use a circular queue).
- Not suitable when the most recent item is needed first (use a stack), or when some items are more urgent (a priority queue is needed).
Stack and queue compared
| Stack | Queue | |
|---|---|---|
| Order | LIFO | FIFO |
| Add at | Top | Rear |
| Remove from | Top | Front |
| Pointers | One (top) | Two (front, rear) |
| Used for | Undo, call stack, backtracking | Buffers, scheduling, breadth-first search |
Hand tracing
Starting with head (front) = 0 and tail (rear) = −1 (empty):
| Operation | Queue after (front ... rear) | Head | Tail | Output |
|---|---|---|---|---|
enqueue(4) |
4 | 0 | 0 | |
enqueue(9) |
4, 9 | 0 | 1 | |
enqueue(2) |
4, 9, 2 | 0 | 2 | |
dequeue() |
9, 2 | 1 | 2 | 4 |
enqueue(7) |
9, 2, 7 | 1 | 3 | |
dequeue() |
2, 7 | 2 | 3 | 9 |
peek() |
2, 7 | 2 | 3 | 2 |
Worked example
Task: a circular queue of size 4 (so space freed at the front is reused) holds the print jobs A, B, C (front to rear). Show the queue after dequeue(), enqueue("D"), enqueue("E"), enqueue("F"), and say what happens at the last step.
| Operation | Queue (front ... rear) | Notes |
|---|---|---|
| Start | A, B, C | 3 of 4 used |
dequeue() |
B, C | returns A |
enqueue("D") |
B, C, D | 3 items |
enqueue("E") |
B, C, D, E | 4 items, now full |
enqueue("F") |
B, C, D, E | Overflow: no room, so F is rejected (an error) |
The program should check isFull() first, and wait or report an error instead of losing the job.
Exam tip: say "first in, first out", and name front and rear. When tracing, redraw the queue after each operation and show what dequeue returns. Be able to contrast with a stack. Mention overflow and underflow. In Unit 2 you must implement a queue with a fixed-length list (8.2.6).
Check yourself
What does FIFO mean?
First in, first out: the item that was added earliest is the first removed.
At which end are items added to and removed from a queue?
Added at the rear, removed from the front.
Why is a simple array queue wasteful, and what fixes it?
As items are removed the front moves along and the space at the start isn't reused, so it seems full. A circular queue wraps the pointers round to reuse it.
Give two uses of a queue
Any two of: print queues; keyboard buffers; process scheduling; waiting lists; breadth-first search; network packets.
After enqueue(1), enqueue(2), dequeue(), enqueue(3), what is in the queue, front to rear?
2, 3.