Circular Queue

Solves the wasted space problem by treating the array as if it wraps around in a circle.

Why Circular Queues?

Linear Queue Problem

D
E
F

3 wasted slots. Rear at index 5 (max). Reports "FULL" with only 3/6 used!

becomes

Circular Queue Solution

G
·
·
D
E
F

Rear wraps to index 0. Freed slots reused. G fills the vacated slot.

How a Circular Queue Works

The Key Idea: MOD

Instead of simply incrementing pointers, we use modular arithmetic:

Rear ← (Rear + 1) MOD MaxSize
Front ← (Front + 1) MOD MaxSize

This makes the pointer "wrap around" to index 0 when it passes the end of the array.

Enqueue

  1. Check if the queue is full (size = MaxSize)
  2. Rear ← (Rear + 1) MOD MaxSize
  3. Place item at Queue[Rear]
  4. Increment size counter

Dequeue

  1. Check if the queue is empty (size = 0)
  2. Retrieve item at Queue[Front]
  3. Front ← (Front + 1) MOD MaxSize
  4. Decrement size counter

Detecting Full vs Empty

In a circular queue, front can equal rear when full OR empty. Solutions:

  • Size counter (simplest — used here)
  • Keep one slot always empty
  • Boolean flag

Circular Visualisation

The array is logically arranged in a circle. Pointers wrap using MOD.

Trace Example

Click a step above to trace through circular queue operations.

Interactive Circular Queue Simulator

Observe how the rear pointer wraps around when it reaches the end of the array.

Front: 0 Rear: -1 Size: 0
Circular queue initialised. Front = 0, Rear = -1, Size = 0.

Circular Queue — Pseudocode (9618 Style)

PROCEDURE Enqueue(Queue, Front, Rear, Size, MaxSize, Item)
    IF Size >= MaxSize THEN
        OUTPUT "Queue is full"
    ELSE
        Rear ← (Rear + 1) MOD MaxSize
        Queue[Rear] ← Item
        Size ← Size + 1
    ENDIF
ENDPROCEDURE

Linear vs Circular — Comparison

FeatureLinear QueueCircular Queue
Space efficiencyWasted space after dequeuesReuses freed space
Pointer movementFront++ and Rear++(ptr+1) MOD MaxSize
Full detectionRear = MaxSize - 1Size = MaxSize
Empty detectionFront > RearSize = 0
ComplexitySimpler to implementSlightly more complex
Best forSimple, short-lived queuesLong-running systems
Linear Queue Back to Hub