AQA 7517, section 4.2.2.1
A queue is first in, first out (FIFO), like people waiting in a line. Items join at the rear (the back) and leave from the front, so the item that has waited the longest is served first.
Operations
You need to be able to describe and apply these operations on linear, circular and priority queues.
| Operation | What it does |
|---|---|
enqueue(item) (add) |
Add an item to the rear |
dequeue() (remove) |
Remove and return the front item |
is_empty() |
True if there are no items |
is_full() |
True if there is no room for another item |
Trying to dequeue from an empty queue is underflow, and trying to add to a full queue is overflow. Test first.
Linear queues
A linear queue can be stored in an array, using two pointers: one for the front and one for the rear.
- Enqueue: if the queue isn't full, move the rear pointer on by one and store the item there.
- Dequeue: if the queue isn't empty, take the item at the front and move the front pointer on by one.
The problem with a simple linear queue in a fixed array is that, as items are removed from the front, the space at the start is never reused. The rear can reach the end of the array and the queue looks full even though there are empty slots at the start.
Circular queues
A circular queue fixes this. When a pointer reaches the end of the array it wraps around to the start, using the remainder after division (%) to move the pointers. This lets the queue reuse space freed by removed items.
class CircularQueue:
def __init__(self, size):
self.items = [None] * size
self.size = size
self.front = 0
self.count = 0
def is_empty(self):
return self.count == 0
def is_full(self):
return self.count == self.size
def enqueue(self, item):
if self.is_full():
raise OverflowError("Queue is full")
rear = (self.front + self.count) % self.size
self.items[rear] = item
self.count += 1
def dequeue(self):
if self.is_empty():
raise IndexError("Queue is empty")
item = self.items[self.front]
self.items[self.front] = None
self.front = (self.front + 1) % self.size
self.count -= 1
return item
This version tracks the front and a count. Many exam questions use separate front and rear pointers instead, where the rear is updated as rear = (rear + 1) % size. The idea is the same: both pointers wrap round using % size.
Here is a circular queue of size 5 as items are added and removed:
The last step puts F at index , wrapping round to the space freed at the start. The queue is now full.
Priority queues
In a priority queue each item has a priority, and the item with the highest priority leaves first, no matter when it arrived. Items with equal priority leave in the order they joined (first in, first out).
| Item | Priority |
|---|---|
| Print report | 2 |
| Print urgent invoice | 1 |
| Print notes | 3 |
If 1 is the highest priority, "Print urgent invoice" is served first, even though it joined after "Print report". Two ways of building one are to keep the queue sorted by priority as items are added, or to search for the highest priority item each time something is removed.
Uses of queues
- Print queues: jobs are printed in the order they were sent.
- Keyboard buffers: keystrokes are processed in the order typed.
- Scheduling: the processor handles tasks in order (a priority queue if some tasks are more important).
- Breadth-first search of graphs (see 4.3.1.1).
Worked example
Task: a circular queue of size 4 starts empty. Show the contents and pointers after: enqueue 10, enqueue 20, enqueue 30, dequeue, enqueue 40, enqueue 50.
Using front and count:
- Enqueue 10, 20, 30: array
10 20 30 _, front 0, count 3. - Dequeue returns 10: array
_ 20 30 _, front 1, count 2. - Enqueue 40: rear is , so array
_ 20 30 40, count 3. - Enqueue 50: rear is , so array
50 20 30 40, count 4.
The queue is now full, and the 50 wrapped round into the freed slot at index 0. The next dequeue returns 20, because it is at the front.
Exam tip: show the calculation each time a pointer moves, such as "". Check whether the question counts empty and full using the pointers or a count, and follow its method.
Check yourself
A circular queue has size 5, front at index 3 and 3 items stored. At which index does the next item go?
, so the next item goes at index 1.
Why is a circular queue better than a linear queue in a fixed array?
It reuses the space at the start of the array that is freed when items are removed, by wrapping the pointers round, so the queue only becomes full when every slot is used.
What is the difference between a queue and a priority queue?
In a queue, items leave in the order they joined. In a priority queue the item with the highest priority leaves first, and items with equal priority leave in the order they joined.
What are the two operations used to add and remove items from a queue?
Enqueue adds an item to the rear, and dequeue removes the item from the front.