Learn how a circular queue uses a fixed-size array with wrapped index calculations.
Suppose you fill a three-slot queue, then remove its first item. There is space for one more item at index 0. A circular queue can reuse that slot while the two older items stay in place.
Number the slots 0, 1, 2. From slot 2, advancing one step takes you back to slot 0. The array stays in place. Only the index wraps.
In the previous queue, enqueue always appended to the underlying array. In this lesson, the queue has a fixed positive capacity k and rejects an enqueue when all k slots are occupied. Other implementations can grow a circular buffer by allocating a larger array and copying the items in queue order.
A queue’s state can be described with three values:
| Piece of state | Role |
|---|---|
slots | The physical array positions that hold queue items. |
front | The physical position of the queue’s first logical item. |
size | The number of items currently in the queue. |
To read the four-slot queue in the diagram, begin at front = 1 and follow the three stored items, B, C, D. Enqueuing E fills slot 0. Read from the same front for four steps and the order becomes B, C, D, E.
The item at logical offset j occupies slot (front + j) % k. Substitute j = 0 for the front item. For a nonempty queue, substitute j = size - 1 for the rear, the most recently enqueued item still present. If the queue has room, j = size gives the slot for the next insertion.
Take a capacity-five queue whose first item, A, occupies slot 4. Follow one enqueue and one dequeue.
| State | Slots 0 through 4 | front | size | FIFO order |
|---|---|---|---|---|
| Start | [B, ·, ·, ·, A] | 4 | 2 | A, B |
Enqueue C at (4 + 2) % 5 = 1 | [B, C, ·, ·, A] | 4 | 3 | A, B, C |
Dequeue A and advance to (4 + 1) % 5 = 0 | [B, C, ·, ·, ·] | 0 | 2 | B, C |
The dots denote unused slots. Check the last row by starting at index 0 and reading two values.
On an empty queue there is no front or rear item to read. On a nonempty queue, front identifies the oldest item and (front + size - 1) % k identifies the newest. The next insertion index is one step beyond that newest item.
In this diagram, the arrow labeled rear marks the next insertion position. Some implementations use that name for the write index. The peekRear method below returns the last stored item, one slot before that write index.
Compare front with the next insertion index. If size = 0, then (front + 0) % k equals front. If size = k, then (front + k) % k also equals front. Attempting a write in the second state would overwrite the oldest item.
Our implementation uses size to distinguish the two states while making all k slots available. Allow a dequeue when size > 0 and an enqueue when size < k. A different design could instead keep a separate full flag or reserve one unused slot.
The class below stores the slots, the capacity, the front index, and the size. It calculates both the next write position and the rear item’s position when needed.
Enqueue and dequeue return a boolean to indicate whether they succeeded. Reading an empty queue returns undefined in JavaScript or None in Python. Assume the capacity is a positive integer.
class CircularQueue {
constructor(capacity) {
this.slots = Array(capacity);
this.capacity = capacity;
this.front = 0;
this.size = 0;
}
enqueue(value) {
if (this.size === this.capacity) return false;
const writeIndex = (this.front + this.size) % this.capacity;
this.slots[writeIndex] = value;
this.size++;
return true;
}
dequeue() {
if (this.size === 0) return false;
this.slots[this.front] = undefined;
this.front = (this.front + 1) % this.capacity;
this.size--;
return true;
}
peekFront() {
return this.size === 0 ? undefined : this.slots[this.front];
}
peekRear() {
if (this.size === 0) return undefined;
const rearIndex = (this.front + this.size - 1) % this.capacity;
return this.slots[rearIndex];
}
}| Operation | Before the update | After the update |
|---|---|---|
| Enqueue | Check for space. Compute (front + size) % k. | Write the value at that slot and increase size by one. |
| Dequeue | Check for an item. Clear the slot at front. | Set front = (front + 1) % k and decrease size by one. |
| Read front or rear | Check for an item. Compute its index. | Read the slot. Both front and size keep their values. |
Each operation uses O(1) time. Reserving the buffer uses O(k) space.
A correct circular queue keeps all four operations constant-time while handling capacity-one, full, empty, and wrapped states.
front identifies the first logical item, while size identifies how many physical slots belong to the queue.(front + size) % k to find the next insertion position within a capacity of k slots.front in both the empty and full states. Use size to distinguish them.O(1) time when their indexes and queue state are updated directly.