liveBook · Manning

Rear

As explained in Grokking Data Structures

The rear is the end of a queue at which new elements are added.

The rear is the end of a queue where new elements enter. Enqueue adds at the rear, while dequeue removes the least recently added element from the front. section 9.3

In an array-based queue, the rear pointer moves through several practical situations:

  • It starts at index 0 and advances as values are enqueued. section 9.11
  • When it reaches the last array cell, it wraps to the beginning using virtual, circular indexes. section 9.11
  • After wrapping, rear may point to a lower index than front; this does not necessarily mean the queue is full. section 9.11

A line at an ice-cream cart

Imagine people joining a line: each newcomer walks to the rear and stands behind the last person. The person at the front leaves first, preserving arrival order. section 9.3

def enqueue(self, value):
    if self.is_full():
        raise ValueError('The queue is already full!')
    self._data[self._rear] = value
    self._rear = (self._rear + 1) % self._max_size
    self._size += 1

This compact Python implementation stores the new value at rear, advances the pointer with circular indexing, and updates the queue size. section 9.11

How Grokking Data Structures covers it

How deep it goes
Explains rear as an endpoint, then traces initialization, advancement, circular wrapping, and pointer ordering.
Examples
Uses an ice-cream line, boxes and numbers, circular arrays, and a Python enqueue implementation.
What it assumes
Assumes only queue ordering, teaching pointer movement and circular indexing from the beginning.
What you can do after
Apply it via enqueue by placing values at rear and advancing a circular array pointer.
How it connects
Connects rear to queues through enqueue and contrasts it with front and the stack top.

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.