Edexcel International Advanced Level Computer Science, point 18.2.1
The circular queue (14.1.5) and the list-based queue (8.2.6) are ADTs you must now write in code. In Unit 4 you may be asked to amend and write one. Check the Programming Language Subset (PLS) for the exact method names allowed.
The design
- A fixed-length list of
capacityitems holds the data. frontis the index of the next item to leave.countis the number of items held.- The index where the next item joins is .
- Wrap-around uses
%.
a. Create
class CircularQueue:
def __init__(self, capacity):
self.items = [None] * capacity # fixed-length list
self.capacity = capacity
self.front = 0
self.count = 0
Choose capacity at creation: it can't change afterwards. Reject a capacity below 1.
d. IsEmpty and e. IsFull
def is_empty(self):
return self.count == 0
def is_full(self):
return self.count == self.capacity
Using count avoids the ambiguity of front == rear (which could mean empty or full).
b. Enqueue
def enqueue(self, item):
if self.is_full():
return False # or raise OverflowError
rear = (self.front + self.count) % self.capacity
self.items[rear] = item
self.count += 1
return True
Steps: check full, find the rear slot, store, add 1 to the count.
c. Dequeue
def dequeue(self):
if self.is_empty():
return None # or raise IndexError
item = self.items[self.front]
self.items[self.front] = None # optional: clear the slot
self.front = (self.front + 1) % self.capacity
self.count -= 1
return item
Steps: check empty, take the front item, move front on (with wrap-around), subtract 1 from the count.
Useful extras
def peek(self):
return None if self.is_empty() else self.items[self.front]
def size(self):
return self.count
def to_list(self): # items in queue order
return [self.items[(self.front + i) % self.capacity]
for i in range(self.count)]
Test plan and trace
| Test | Expected |
|---|---|
| New queue | is_empty() is True, is_full() is False |
| Enqueue until full | The last enqueue works; the next returns False |
| Dequeue from an empty queue | Returns None |
| First in, first out | Items come out in the order they went in |
| Wrap-around | After dequeues, new items reuse the freed slots |
| Boundary | Capacity 1: enqueue, dequeue, enqueue |
q = CircularQueue(4)
for x in "ABC":
q.enqueue(x)
print(q.dequeue(), q.to_list()) # A ['B', 'C']
q.enqueue("D")
q.enqueue("E") # wraps round to index 0
print(q.items, q.front, q.count) # ['E', 'B', 'C', 'D'] 1 4
print(q.enqueue("F")) # False: full
print([q.dequeue() for _ in range(4)]) # ['B', 'C', 'D', 'E']
print(q.dequeue(), q.is_empty()) # None True
Trace (capacity 4):
| Operation | front |
count |
items |
|---|---|---|---|
| (start) | 0 | 0 | [-, -, -, -] |
| enqueue A, B, C | 0 | 3 | [A, B, C, -] |
| dequeue (A) | 1 | 2 | [-, B, C, -] |
| enqueue D | 1 | 3 | [-, B, C, D] |
| enqueue E | 1 | 4 | [E, B, C, D] (index ) |
| enqueue F | 1 | 4 | rejected: full |
Using rear instead of count
An alternative keeps front and rear, and leaves one slot empty so that full and empty can be told apart:
- Empty:
front == rear. - Full:
(rear + 1) % capacity == front. - Capacity of stores at most items.
The count method is simpler and uses every slot.
Worked example
Task: a print queue holds at most 3 jobs. Write the code to add jobs, reject extras, and print them in order.
jobs = CircularQueue(3)
for name in ["report", "photo", "notes", "essay"]:
if jobs.enqueue(name):
print("Queued", name)
else:
print("Queue full, rejected", name)
while not jobs.is_empty():
print("Printing", jobs.dequeue())
Output: Queued report, Queued photo, Queued notes, Queue full, rejected essay, then Printing report, Printing photo, Printing notes (first in, first out).
Exam tip: write is_empty and is_full first, then check them at the start of enqueue and dequeue. Use % capacity for the wrap-around. Test the wrap-around and the full and empty cases. Show the pointers when tracing.
Check yourself
How do you work out the index where a new item is stored?
(front + count) % capacity.
Why is a count used?
It makes it easy to tell a full queue from an empty one when the front and rear pointers are equal.
What should enqueue do if the queue is full?
Reject the item (return False or raise an overflow error) without changing the queue.
Why is the % operator needed?
It wraps an index that reaches the end of the list back to the start, so freed slots are reused.