liveBook · Manning

Circular queue

As explained in Grokking Data Structures

A circular queue is a queue implemented over storage in which the front and rear positions wrap around from the end to the beginning.

Picture an array bent into a ring: after its last cell, the next position is the first cell again. A circular queue uses this wraparound to reuse cells freed by dequeues, without shifting the remaining elements. Enqueue adds at the rear; dequeue removes from the front. section 9.7 section 9.18

Wraparound in an eight-cell array

With cells indexed 0 through 7, the virtual position after 7 is 8, which maps back to cell 0: 8 % 8 = 0. The same pattern maps virtual index 9 to cell 1. section 9.7

A static array has a fixed capacity, so enqueueing when it is full must be handled. A dynamic array can grow, but resizing requires rearranging elements so the front and rear positions still work. Array implementations can offer performance advantages when capacity is known, at the cost of more complicated code. section 9.7 section 9.13 section 9.9

How Grokking Data Structures covers it

How deep it goes
Explains virtual indexes and modulo mapping, and notes fixed-capacity limits; dynamic-array resizing is described as complicated.
Examples
Uses an eight-cell array, showing virtual indexes 8 and 9 mapping to cells 0 and 1.
What you can do after
Apply modulo arithmetic to map virtual queue positions onto array cells and reuse space after dequeues.
How it connects
Connects queues with arrays, linked lists, dynamic arrays, and modular arithmetic.

Related concepts

This explainer was generated by Manning's knowledge graph from the text of Grokking Data Structures. The section links point to the parts of the book it draws on.