3.2.7 · D1Linear Data Structures

Foundations — Queue — FIFO semantics, enqueue - dequeue, circular array implementation

1,777 words8 min readBack to topic

Before you can read the parent note comfortably, you need to genuinely see eight small things. This page builds each one from nothing, in an order where every idea leans on the one before it.


1. A slot, an array, and an index

Figure — Queue — FIFO semantics, enqueue - dequeue, circular array implementation

The parent note writes arr[N] — that means "an array of slots." The valid indices are then . There is no slot with index ; that is one past the end. Remembering this single fact prevents half of all queue bugs.


2. The symbol — capacity


3. front — where the oldest element sits

The picture to hold: front is a little arrow parked under one chair, saying "the next person to be served is sitting here."

Figure — Queue — FIFO semantics, enqueue - dequeue, circular array implementation

When we dequeue (remove the front), we don't erase and shift everyone. We just slide the front arrow one chair to the right. The old occupant is simply forgotten, not moved.


4. count — how many are currently seated


5. "Rear" — the next free seat, derived not stored

Why can we compute it? Because the occupied chairs are always consecutive starting at front (allowing for wrap, coming next). If front is where the oldest sits and there are count people, then the first free chair is count steps past front:

Figure — Queue — FIFO semantics, enqueue - dequeue, circular array implementation

Read the figure: front = 1, count = 3, so occupied chairs are and the next free one is . That is exactly front + count. So far, no modulo — because nothing has fallen off the end yet. That is section 7.


6. The % symbol — modulo (remainder)

This is the single most important new symbol on the whole topic, and the parent note leans on it constantly. We build it from the most basic thing: division with remainder.

Figure — Queue — FIFO semantics, enqueue - dequeue, circular array implementation

Worked feel for the numbers (all with , so answers live in ):

See Modular Arithmetic for the same idea developed on its own.


7. Putting it together — the wrap-around formula

Now sections 5 and 6 combine. The raw rear front + count might point at a chair number , which doesn't exist. We fold it back onto the ring with modulo:

Both are the same modulo trick applied to two different arrows. Once modulo clicks, the whole circular-queue idea is just these two lines.


8. FIFO — the rule all this serves

Everything above (front, count, modulo) exists purely so that a fixed array can obey this time-order rule forever without shifting elements or running out of room prematurely. Contrast this with a stack, whose rule is the exact opposite (newest out first).


Prerequisite map

Slot and Array

Index counts from 0

Capacity N and last index N minus 1

front arrow points at oldest

count of occupied slots

rear equals front plus count

Modulo wraps a straight row into a ring

rear mod N and front mod N

FIFO queue on a circular array

Read it top to bottom: the plain array and 0-based indexing come first; front and count are built on top; rear = front + count needs both; modulo (built separately) folds the result onto a ring; and only then does the full circular FIFO queue stand up.


Equipment checklist

If you can answer every line below without peeking, you are ready for the parent note. Reveal each to self-test.

If an array has slots, what is the largest valid index?
(indices run to ).
What does the front index point at — an element or a position?
A position (the address of the slot holding the oldest element), not the element itself.
What does count measure, and what are its minimum and maximum values?
The number of elements currently in the queue; minimum (empty), maximum (full).
Why is rear computed as front + count before any wrap?
Because occupied slots are consecutive starting at front, so the first free slot is count steps past it.
What does a % N (modulo) give you, in plain words?
The remainder after dividing by — where you land on an -mark clock, wrapping past the last mark back to 0.
Compute and .
and .
Why does the circular queue need modulo at all?
To turn a straight, fixed-length row of slots into a ring so indices reuse freed slots at the start instead of walking off the end.
State the FIFO rule in five words.
First in, first out (oldest served first).

Recall One-sentence summary

A queue obeys FIFO; to run it inside a fixed array of slots we track front (where the oldest sits) and count (how many are seated), derive the next free slot as , and use modulo everywhere so the row of slots behaves like a ring.