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
