liveBook · Manning

Queue

As explained in High Performance Unity Game Development

A data structure that stores elements for first-in, first-out retrieval.

A queue retrieves elements in first-in, first-out (FIFO) order: the earliest added element is removed first. Queues suit tasks where processing order matters, such as processing positions in the order a pathfinding algorithm discovers them. Enemy spawning does not need FIFO if any available enemy index will do. section 6.4

An array-backed circular queue avoids shifting elements on every removal. Track the array, the index to remove from (start), the next index to add to (end), and the number of stored elements (count). Enqueue writes at end; dequeue reads at start. Advance the relevant index after each operation, wrapping it to zero at the array’s end. section 6.4

Figure 6.12 To implement a queue using a circular array, we use two indices: start and end. If either index reaches the array limit, we reset it to the beginning. Start points to the first element added, which is 10. End points to the next empty index, the one after 40. When we dequeue, we output the first element (10), then increment start, so it now points to 20. When we enqueue 50, we write it at the current end index. Then we increment end. Since end has now reached the array limit, it wraps back to 0.

The diagram traces dequeueing 10, then enqueueing 50 as the end index wraps to the start of the array. section 6.4

A circular array keeps its elements in place, avoiding the copies required by shifting every remaining item left. This makes its behavior visible and preserves contiguous storage, but a queue maintains more state and wraparound checks than a stack. Use FIFO only when the problem requires it; otherwise, a simpler choice may suffice. section 6.4 section 6.8

How High Performance Unity Game Development covers it

How deep it goes
Explains circular-array state and enqueue/dequeue index updates, including wraparound, without detailing capacity or empty-queue handling.
Examples
Demonstrates circular wraparound with values 10, 20, 40, and 50, and cites pathfinding as a FIFO use.
What it assumes
Builds on arrays and integer indices, explaining queue state and operations through those structures.
What you can do after
Build an array-backed circular queue by tracking start, end, and count while wrapping indices at the array boundary.
How it connects
Connects queues to arrays, stacks, lists, data-oriented design, and pathfinding.

Also explained in

Compare how 30 books explain queue →

Related concepts

This explainer was generated by Manning's knowledge graph from the text of High Performance Unity Game Development. The section links point to the parts of the book it draws on.