Chapter 18 · Queues

Circular Queues

A circular queue (ring buffer) uses a fixed-size array and two pointers — head and tail — that wrap around using modulo arithmetic. When the tail reaches the end of the array it wraps back to index 0, reusing the space freed by earlier dequeues. Result: O(1) enqueue and dequeue with zero memory allocation.

What is a circular queue?

A circular queue (ring buffer) is a fixed-size queue stored in an array where the head and tail indices wrap around to the start using modulo ((i + 1) % capacity). It reuses freed slots, so enqueue and dequeue are O(1) with no shifting and no growing. It's used in audio and network buffers, keyboard input and logging the last N events.

class RingBuffer {
  constructor(capacity) { this.buf = new Array(capacity); this.head = 0; this.size = 0; }
  enqueue(x) {
    if (this.size === this.buf.length) throw new Error("full");
    this.buf[(this.head + this.size++) % this.buf.length] = x;
  }
  dequeue() {
    const x = this.buf[this.head];
    this.head = (this.head + 1) % this.buf.length; this.size--;
    return x;
  }
}

Ring visualization

0—
1—
2AHEAD
3B
4C
5DTAIL
6—
7—

Capacity = 8. head = 2, tail = 5. Elements A,B,C,D (4 items). Free slots: 0,1,6,7. When we enqueue E: tail becomes (5+1) mod 8 = 6.

Live demo — FIFO queue

Enqueue and dequeue items to see the FIFO order in action. In a circular queue the same slots are reused using modulo index arithmetic.

Queue — First In, First Out (FIFO)

size: 4
← FRONT (dequeue)BACK (enqueue) →
3[0]front
7[1]
1[2]
5[3]

Implementation

circular-queue.pseudo
1CircularQueue(capacity):
2 data <- array of size capacity
3 head <- 0 // points to front element
4 tail <- 0 // points to next empty slot
5 size <- 0
6 capacity <- capacity
7 
8enqueue(value):
9 if size == capacity: error 'Queue is full'
10 data[tail] <- value
11 tail <- (tail + 1) mod capacity // wrap around with modulo
12 size <- size + 1
13 
14dequeue() -> value:
15 if size == 0: error 'Queue is empty'
16 value <- data[head]
17 head <- (head + 1) mod capacity // wrap around with modulo
18 size <- size - 1
19 return value
20 
21peek() -> value:
22 if size == 0: error 'Queue is empty'
23 return data[head]
24 
25isFull() -> bool: return size == capacity
26isEmpty() -> bool: return size == 0

Why modulo works

Capacity = 8. Tail starts at 6. After 3 enqueues:

enqueue(E)(6+1) mod 8 = 7tail moves right
enqueue(F)(7+1) mod 8 = 0wraps to start!
enqueue(G)(0+1) mod 8 = 1continues from 0

Linear queue vs Circular queue

Linear Queue (array)

  • − After n dequeues, front n slots are wasted
  • − Need to shift elements left: O(n) per dequeue
  • − Or accept growing memory waste

Circular Queue

  • + Reuses dequeued slots via wrap-around
  • + O(1) enqueue and dequeue, always
  • + Fixed memory footprint
  • + Cache-friendly contiguous array

Real-world uses

OS I/O buffers

Keyboard/disk input buffered in a ring so the OS can process at its own pace

Audio streaming

Producer fills buffer, consumer reads — ring prevents gaps without blocking

Network packet queues

Fixed-size queues drop oldest packets when full (drop-tail policy)

CPU scheduling

Round-robin schedulers cycle through tasks using a circular buffer of process IDs

Time complexity

enqueue — write to tail and advance with moduloO(1)

Constant time

dequeue — read from head and advance with moduloO(1)

Constant time

peek, isFull, isEmpty — constant time checksO(1)

Constant time

space — fixed array of size capacityO(n)

Linear time

🎯 Quick Check

A circular queue has capacity 5. head = 3, tail = 3, size = 0. After enqueue(X) and enqueue(Y), what is the tail index?

⚠️Full vs empty ambiguity
When head == tail, it could mean empty (size=0) or full (size=capacity). Implementations handle this by: (1) keeping an explicit size counter, (2) keeping one slot always empty (so full means (tail+1)%cap == head), or (3) using a boolean flag. The pseudocode above uses an explicit size counter which is the clearest approach.

Circular Queues: summary

Key points

  • Track size (or keep one slot empty) to tell "full" from "empty".
  • Some ring buffers overwrite the oldest item when full, which suits "last N events".

Frequently asked questions

›Why use a circular queue instead of a normal queue?

It has a fixed memory footprint and O(1) operations without ever shifting or reallocating, which is ideal for streaming data.