liveBook · Manning

Linear queue

As explained in Grokking Data Structures

A linear queue is a queue implementation whose front and rear positions advance linearly through its underlying array without wrapping around.

Imagine a line that moves only forward. A linear queue stores items in an array, adds new items toward larger indexes, and removes items from the front. Once the rear reaches the array’s end, the queue is treated as full—even if earlier positions became empty. section 9.7 section 9.18

A linear queue follows these practical rules:

  • Enqueue adds an element to the back, while dequeue removes and returns the element at the front. section 9.18
  • Dequeuing leaves holes at the beginning of the array, so usable space shrinks as the rear advances. section 9.7
  • An array allocated for n elements supports only n insertions before the rear reaches its end, even after removals. section 9.7

Why the space disappears

Suppose a queue uses an array with eight positions. After several dequeues, the first positions are empty, but newly enqueued elements continue toward larger indexes. When the rear reaches position seven, the linear queue is full; reclaiming the holes would require moving the remaining elements. section 9.7

This design is simple but usually impractical. Moving elements toward the start reclaims space but costs O(n) each time; a circular queue instead wraps its indexes around the array and reuses freed positions. section 9.7 section 9.9

How Grokking Data Structures covers it

How deep it goes
Explains array layout, shrinking capacity, and the movement workaround before stopping short of a detailed implementation.
Examples
Illustrates holes in an eight-position array after dequeues and the resulting rear-at-end condition.
What you can do after
Evaluate when a fixed-array queue is acceptable and recognize when circular or linked-list storage is more practical.
How it connects
Contrasts linear queues with circular queues and links both to static arrays, linked lists, enqueue, and dequeue.

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.