Circular Queue

A fixed-size ring buffer: head and tail indices wrap around the end of an array using modulo, so a bounded queue never needs to shift or reallocate.

A circular queue stores elements in a fixed array with two indices: head (next to serve) and tail (next free slot). Both advance with (i + 1) mod capacity, so when they reach the end of the array they wrap to slot 0 — the storage is a ring.

Enqueue writes at tail and advances it; dequeue reads at head and advances it. Full vs empty is the classic subtlety — both look like head == tail — solved by keeping a count or sacrificing one slot.

Because it never allocates after creation and both operations are O(1) with no shifting, the ring buffer is the standard structure for streaming data: audio buffers, network packet queues, keyboard input, and producer/consumer pipes between threads.

Operations

OperationComplexity
enqueueO(1)
dequeueO(1)
full/empty checkO(1)
capacityfixed at creation

Where it's used

Bounded buffers everywhere: audio/video streaming, interrupt queues, logging rings, lock-free producer/consumer channels.

Open the interactive Circular Queue workspace →