3.2.8 · D1Linear Data Structures

Foundations — Deque (double-ended queue) — operations, use cases

2,035 words9 min readBack to topic

Before you can read the parent note Deque, you must own every symbol it throws at you. Below, each idea is built from nothing, drawn as a picture, and justified — why does the topic need this?


1. A "line" of items — front and back

The most basic object here is a sequence: items sitting in a row, left to right.

Figure — Deque (double-ended queue) — operations, use cases

2. The symbol — "how many items"

Everywhere in this topic you see the letter .

Why a single letter? Because we want to talk about cost for any size at once. Saying "if there are things" covers 3 things, 3 million things, and everything between in one breath.


3. Big-O: what "" and "" actually mean

This is the symbol the whole topic is bragging about (" at both ends!"), so we build it carefully from zero.

Figure — Deque (double-ended queue) — operations, use cases

4. Amortized — averaging out the rare expensive move

The parent says push is "amortized ". That word is a symbol too.

Figure — Deque (double-ended queue) — operations, use cases

5. Array, index, and capacity

The fast implementation is an array, so we nail its three symbols.

Figure — Deque (double-ended queue) — operations, use cases

6. The modulo operator — the ring-maker

This is the single most important piece of arithmetic in the array deque, so we earn it slowly.

Why does this build a ring? Watch what does to the counting numbers:

The moment you reach the capacity , the result wraps back to . So counting "past the end" of the array silently lands you at the beginning — exactly a circle.


7. Pointer and node — the linked-list vocabulary

The other implementation uses nodes and pointers.


8. LIFO and FIFO — the two disciplines a deque contains


Prerequisite map

Sequence with front and back

n means item count

Big-O flat vs slanted

Amortized average cost

Array index capacity C

Modulo wraps to a ring

Node and prev pointer

LIFO and FIFO

Deque topic


Equipment checklist

What do "front" and "back" name — items or positions?
Positions: the left edge and right edge of the line; the item sitting there can change.
What does the symbol stand for?
The current number of items in the structure.
In one phrase, what does look like on a size-vs-time graph?
A flat horizontal line — work does not grow with .
In one phrase, what does look like?
A straight slanted line rising in proportion to .
Does mean "exactly one step"?
No — it means bounded by a constant that doesn't grow with (could be 3 or 300 steps).
What does "amortized " mean?
Total cost over operations is , so the average per operation is , even if a few are expensive.
For an array of capacity , what are the valid indices?
through (0-based counting).
Compute .
(the remainder of ).
Why add before taking when moving front left?
can be ; adding first keeps the result non-negative before the remainder.
Move front left once from with : what index?
.
Why must the linked-list deque be doubly linked?
pop_back needs the node before the last instantly; the prev pointer gives it in instead of walking .
What do LIFO and FIFO stand for?
Last-In-First-Out (stack) and First-In-First-Out (queue).

Connections

  • Parent: Deque — the topic these foundations serve
  • Stack — the LIFO restriction of a deque
  • Queue — the FIFO restriction of a deque
  • Circular Buffer / Ring Buffer — where the ring lives
  • Doubly Linked List — where prev/next pointers are built
  • Dynamic Array Amortized Analysis — the full amortized- argument