Solves the wasted space problem by treating the array as if it wraps around in a circle.
3 wasted slots. Rear at index 5 (max). Reports "FULL" with only 3/6 used!
Rear wraps to index 0. Freed slots reused. G fills the vacated slot.
Instead of simply incrementing pointers, we use modular arithmetic:
This makes the pointer "wrap around" to index 0 when it passes the end of the array.
In a circular queue, front can equal rear when full OR empty. Solutions:
The array is logically arranged in a circle. Pointers wrap using MOD.
Observe how the rear pointer wraps around when it reaches the end of the array.
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
| Feature | Linear Queue | Circular Queue |
|---|---|---|
| Space efficiency | Wasted space after dequeues | Reuses freed space |
| Pointer movement | Front++ and Rear++ | (ptr+1) MOD MaxSize |
| Full detection | Rear = MaxSize - 1 | Size = MaxSize |
| Empty detection | Front > Rear | Size = 0 |
| Complexity | Simpler to implement | Slightly more complex |
| Best for | Simple, short-lived queues | Long-running systems |