Edexcel International Advanced Level Computer Science, point 14.1.5
Definition
A circular queue is a queue (4.3.2) stored in a fixed-size array, in which the front and rear pointers wrap round to the start of the array when they reach the end, so that the array is treated as a ring.
Function and why needed
A simple array queue wastes space: as items are dequeued, the front moves along, and the empty slots at the start can never be reused, so the queue seems full when it is not. A circular queue reuses those slots, giving a fixed-size queue with no wasted space and no shifting of items.
a. Structure
A circular queue needs:
- A fixed-size array (capacity ).
- A front pointer: where the next item will be removed from.
- A rear pointer (or a count): where the next item will be added.
- Wrap-around using
MOD(the remainder operator): .
Empty or full? When front and rear are equal, the queue may be empty or full. Fix this with a count of items (or by leaving one slot unused).
class CircularQueue:
def __init__(self, capacity):
self.items = [None] * capacity
self.capacity = capacity
self.front = 0 # index of the first item
self.count = 0 # number of items held
def is_empty(self):
return self.count == 0
def is_full(self):
return self.count == self.capacity
b. Operations
Insert (enqueue)
- If the queue is full: overflow (reject).
- Work out the rear position: .
- Store the item there, and add 1 to the count.
def enqueue(self, item):
if self.is_full():
raise OverflowError("queue is full")
rear = (self.front + self.count) % self.capacity
self.items[rear] = item
self.count += 1
Delete (dequeue)
- If the queue is empty: underflow.
- Take the item at the front.
- Move the front on: , and subtract 1 from the count.
def dequeue(self):
if self.is_empty():
raise IndexError("queue is empty")
item = self.items[self.front]
self.front = (self.front + 1) % self.capacity
self.count -= 1
return item
Search
Look through the items from the front, for count positions, wrapping round:
def search(self, target):
for i in range(self.count):
index = (self.front + i) % self.capacity
if self.items[index] == target:
return index
return -1
Time: . Insert and delete are both .
Hand trace (capacity 5)
| Operation | front |
count |
Array (index 0 to 4) | Note |
|---|---|---|---|---|
| (start) | 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 |
| dequeue() | 2 | 1 | A B C - - |
returns B |
| enqueue(D) | 2 | 2 | A B C D - |
rear = 3 |
| enqueue(E) | 2 | 3 | A B C D E |
rear = 4 |
| enqueue(F) | 2 | 4 | F B C D E |
rear = : wraps round |
| enqueue(G) | 2 | 5 | F G C D E |
rear = 1: now full |
| enqueue(H) | 2 | 5 | F G C D E |
overflow: rejected |
The queue contents, front to rear, are C, D, E, F, G.
Benefits
- No wasted space: freed slots are reused.
- Fixed memory: predictable, no resizing.
- Fast operations: insert and delete are , no shifting of items.
- Ideal for buffers (keyboard, network, audio) and fixed-size streams.
Drawbacks
- Fixed capacity: it can fill up (overflow) and can't grow (unlike a linked-list queue).
- Slightly more complex than a simple queue (modulo arithmetic, empty/full ambiguity).
- Searching is and must handle wrap-around.
Circular queue compared with a simple array queue
| Simple array queue | Circular queue | |
|---|---|---|
| Reuse of freed space | No | Yes |
| Seems full when not? | Yes | No |
| Shifting items to compensate | Needed (slow) | Never |
| Complexity | Simpler | Wrap-around logic |
Worked example
Task: a circular queue of capacity 4 has front = 3 and holds 3 items (X, Y, Z in order). Which indices do they occupy, where does the next item go, and what happens after one dequeue?
- Items: X at index 3, Y at , Z at index 1.
- Next item goes at .
- After one dequeue: X is returned, front becomes , and count is 2 (Y at 0, Z at 1).
q = CircularQueue(4)
q.front = 3 # set up the example state
for x in "XYZ":
q.enqueue(x)
print(q.items) # ['Y', 'Z', None, 'X']
print(q.dequeue(), q.front) # X 0
Exam tip: show the formula and use MOD for wrap-around. When tracing, record front, count (or rear) and the array after each operation. State how you tell full from empty (a count). Explain the advantage over a simple queue: reuse of freed space.
Check yourself
What problem with a simple array queue does a circular queue solve?
Freed slots at the start of the array are never reused in a simple queue, so it seems full when it isn't. A circular queue wraps round and reuses them.
How is wrap-around achieved?
By using the MOD operator: next = (current + 1) mod capacity.
How can you tell whether a circular queue is full or empty?
Keep a count of items: empty when the count is 0, full when it equals the capacity.
In a circular queue of capacity 6 with front = 4 and 3 items, where is the next item stored?
(4 + 3) mod 6 = 1.
Give one drawback of a circular queue
It has a fixed capacity so it can overflow, and the wrap-around logic makes it slightly more complex.