Edexcel International Advanced Level Computer Science, point 8.2.6
A queue (4.3.2) follows first in, first out. Here you implement it in code using a fixed-length list, with pointers for the front and the rear. Check the PLS for the exact conventions the exam uses.
The problem with a simple version
With a plain list and a front that moves along as items are removed, the space at the start is never reused, so the queue appears full while it has room. The fix is a circular queue: the pointers wrap round to the start when they reach the end, using MOD.
a. Create
CAPACITY = 5
queue = [None] * CAPACITY # fixed-length list
front = 0 # index of the front item
count = 0 # number of items held
Keeping a count makes empty and full easy to test, and avoids ambiguity when front equals rear. The rear (where the next item goes) is worked out as (front + count) % CAPACITY.
d. IsEmpty, e. IsFull, f. Size
def is_empty(count):
return count == 0
def is_full(count):
return count == CAPACITY
def size(count):
return count
b. Enqueue
def enqueue(queue, front, count, item):
if is_full(count):
print("Queue full")
return count
rear = (front + count) % CAPACITY # next free slot (wraps round)
queue[rear] = item
return count + 1
c. Dequeue
def dequeue(queue, front, count):
if is_empty(count):
print("Queue empty")
return None, front, count
item = queue[front]
front = (front + 1) % CAPACITY # move front on (wraps round)
return item, front, count - 1
The MOD operator (%) wraps the index: (4 + 1) % 5 is 0.
A class version
class Queue:
def __init__(self, capacity):
self.items = [None] * capacity
self.front = 0
self.count = 0
def is_empty(self):
return self.count == 0
def is_full(self):
return self.count == len(self.items)
def size(self):
return self.count
def enqueue(self, item):
if self.is_full():
raise OverflowError("queue is full")
rear = (self.front + self.count) % len(self.items)
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.front = (self.front + 1) % len(self.items)
self.count -= 1
return item
Hand trace (capacity 4)
| Operation | front |
count |
List | Notes |
|---|---|---|---|---|
| Create | 0 | 0 | [-, -, -, -] |
empty |
| enqueue(A) | 0 | 1 | [A, -, -, -] |
rear = 0 |
| enqueue(B) | 0 | 2 | [A, B, -, -] |
rear = 1 |
| enqueue(C) | 0 | 3 | [A, B, C, -] |
rear = 2 |
| dequeue() | 1 | 2 | [A, B, C, -] |
returns A |
| enqueue(D) | 1 | 3 | [A, B, C, D] |
rear = 3 |
| enqueue(E) | 1 | 4 | [E, B, C, D] |
rear = : wraps round |
| enqueue(F) | 1 | 4 | [E, B, C, D] |
full: rejected |
| dequeue() | 2 | 3 | [E, B, C, D] |
returns B |
The queue's order (front to rear) is C, D, E at the end.
Other ways to make a queue in Python
# 1. An ordinary list
line = []
line.append("a") # enqueue at the rear
line.append("b")
print(line.pop(0)) # dequeue from the front -> a
# 2. Queue from the queue module
from queue import Queue as LibraryQueue
q = LibraryQueue(maxsize=3) # a queue with room for 3
q.put("x") # enqueue
q.put("y")
print(q.get()) # dequeue -> x
print(q.empty(), q.full()) # False False
pop(0)is slow on long lists, because every other item has to shift along one place.queue.Queuedoes the full and empty checks for you. The specification asks for the fixed-length list version because it makes you move the pointers and wrap them round yourself.
Worked example: a print queue
jobs = Queue(3)
jobs.enqueue("report.pdf")
jobs.enqueue("photo.png")
jobs.enqueue("notes.txt")
print(jobs.is_full()) # True
print(jobs.dequeue()) # report.pdf (first in, first out)
jobs.enqueue("essay.doc") # reuses the freed slot (wrap-around)
print(jobs.size()) # 3
while not jobs.is_empty():
print(jobs.dequeue()) # photo.png, notes.txt, essay.doc
The fourth job fits because the first slot was freed and the rear pointer wrapped round to use it. A simple (non-circular) list would have reported the queue as full.
Exam tip: know how the pointers move (front + 1 on dequeue, wrapping with MOD) and how to test full and empty. When tracing, show front, count (or rear) and the list after each step, and check the wrap-around at the end of the list.
Check yourself
Why is a circular queue better than a simple list-based queue?
It reuses the space freed at the front by wrapping the pointers round, so the queue isn't wrongly reported as full.
What does the MOD operator do in a circular queue?
It wraps an index that reaches the end of the list back round to 0.
How can you tell whether a queue with a count variable is full or empty?
It is full when count equals the capacity and empty when count is 0.
In a queue of capacity 5 with front = 3 and count = 4, which index does the next enqueue use?
(3 + 4) % 5 = 2.