All subjects · Computer Science · WCP03 Advanced Principles of Computer Science
Circular queues
Edexcel IAL Computer Science · WCP03 Advanced Principles of Computer Science
A queue is first in, first out. Built on a fixed-length list, a simple queue wastes the space left behind when items leave. A circular queue wraps the rear pointer back to the start so the space is reused.
Structure
A circular queue is a fixed-length array with two pointers and usually a counter:
- front: index of the item that will leave next;
- rear: index of the most recently added item;
- count (size): number of items, used to test for empty (0) and full (equal to the capacity).
Pointers move forward and wrap around with mod: next = (pointer + 1) mod capacity.
Operations
class CircularQueue:
def __init__(self, capacity):
self.items = [None] * capacity
self.capacity = capacity
self.front = 0
self.rear = -1
self.count = 0
def enqueue(self, item): # insert
if self.count == self.capacity:
return False # full
self.rear = (self.rear + 1) % self.capacity
self.items[self.rear] = item
self.count += 1
return True
def dequeue(self): # delete
if self.count == 0:
return None # empty
item = self.items[self.front]
self.front = (self.front + 1) % self.capacity
self.count -= 1
return itemSearch: start at front and step (index + 1) mod capacity, count times, comparing each item.
Tracing
Capacity 5, front = 0, rear = -1. Enqueue P, Q, R, S; dequeue twice; enqueue T, U, V.
| After | Array (index 0 to 4) | front | rear | count |
|---|---|---|---|---|
| Enqueue P, Q, R, S | P, Q, R, S, - | 0 | 3 | 4 |
| Dequeue twice (P, Q leave) | -, -, R, S, - | 2 | 3 | 2 |
| Enqueue T | -, -, R, S, T | 2 | 4 | 3 |
| Enqueue U (wraps) | U, -, R, S, T | 2 | 0 | 4 |
| Enqueue V | U, V, R, S, T | 2 | 1 | 5 |
Benefits and drawbacks
Benefits: reuses freed space, so a fixed amount of memory serves an endless stream; enqueue and dequeue are O(1) with no shifting. Uses: keyboard and printer buffers, streaming audio, CPU scheduling (round robin).
Drawbacks: fixed capacity, so it can become full; slightly more complex pointer logic; searching is still linear.
Worked example
A circular queue has capacity 6, front = 4, rear = 1 and count = 4. Where is the next item enqueued, and which index is dequeued next?
- Next rear = (1 + 1) mod 6 = 2, so the item goes in index 2.
- The next item to leave is at front = 4.
Answer: Enqueue at index 2; dequeue from index 4.
Check yourself
- A circular queue of capacity 8 has rear = 7. At which index will the next item be enqueued (if not full)?
Show answer
0. (7 + 1) mod 8 = 0: it wraps to the start.
- A circular queue of capacity 5 has front = 3. After two dequeues, what is front?
Show answer
0. 3 -> 4 -> 0.
- Starting empty with capacity 5, the operations are: enqueue P, Q, R, S; dequeue; dequeue; enqueue T, U, V. How many items are in the queue at the end?
Show answer
5. 4 added, 2 removed, 3 added: 5 items.
- For the same operations, what is the final value of rear?
Show answer
1. Rear goes 0, 1, 2, 3, then 4 (T), 0 (U), 1 (V).
- For the same operations, which item will the next dequeue return?
Show answer
R. front = 2, which holds R.
- How does the queue code above detect that the queue is full?
- rear equals 0
- count equals the capacity
- front equals rear
- The array contains None
Show answer
count equals the capacity. With a counter, full means count == capacity.
- Why is a circular queue better than a simple linear queue on a fixed-length list?
- It allows items to leave from the middle
- It sorts the items automatically
- It has unlimited capacity
- Space freed at the front by dequeues can be reused when the rear wraps around
Show answer
Space freed at the front by dequeues can be reused when the rear wraps around. A linear queue's rear reaches the end even when the front has empty slots.
- Which is a typical use of a circular queue?
- Storing a family tree
- Finding the shortest route between towns
- A keyboard buffer holding key presses until the program reads them
- Looking up words in a dictionary by key
Show answer
A keyboard buffer holding key presses until the program reads them. Buffers are classic circular queues.
Sources
- Pearson Edexcel International Advanced Subsidiary and Advanced Level in Computer Science specification (2026), Issue 1
- Pearson Edexcel IAL Computer Science sample assessment materials (Unit 3 paper, resource booklet and mark scheme)
Last updated 8 November 2026. Spotted a mistake? Tell us.