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.

Practise this in the app: instant marking and a fix for every mistake. Free, no email needed to start.

Practise this lesson

Structure

A circular queue is a fixed-length array with two pointers and usually a counter:

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 item

Search: 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.

AfterArray (index 0 to 4)frontrearcount
Enqueue P, Q, R, SP, Q, R, S, -034
Dequeue twice (P, Q leave)-, -, R, S, -232
Enqueue T-, -, R, S, T243
Enqueue U (wraps)U, -, R, S, T204
Enqueue VU, V, R, S, T215

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?

  1. Next rear = (1 + 1) mod 6 = 2, so the item goes in index 2.
  2. The next item to leave is at front = 4.

Answer: Enqueue at index 2; dequeue from index 4.

Key idea: Circular queue: fixed array, front and rear pointers that wrap with (p + 1) mod capacity, plus a count for empty and full tests. Reuses space; O(1) enqueue and dequeue.

Check yourself

  1. 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.

  2. A circular queue of capacity 5 has front = 3. After two dequeues, what is front?
    Show answer

    0. 3 -> 4 -> 0.

  3. 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.

  4. 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).

  5. For the same operations, which item will the next dequeue return?
    Show answer

    R. front = 2, which holds R.

  6. 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.

  7. 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.

  8. 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.

Practise with instant feedback

Sources

Last updated 8 November 2026. Spotted a mistake? Tell us.