Reference

Data structures & costs

Compare operation costs, ordering, invariants and common algorithm notation.

Scope and conventions

Abstract data structures and conventional implementations. Costs use a unit-cost RAM model unless stated; n is item count, V vertices, E edges and k key/output length. Native containers can have different contracts. Expected, amortized and worst-case bounds are distinguished.

Linear structures

Fixed array / contiguous sequence

Operations & costs

Contiguous storage supports indexed access. Searching an unsorted sequence must inspect potentially every element.

Fragment

Operations & costs · Fixed array / contiguous sequence
Follow appearance
GitHub Light
GitHub Dark
Rosé Pine
Catppuccin Mocha
VS Code Dark+
Read/write by index       O(1)
Unsorted value search     O(n)
Insert/delete at index    O(n) shifts (if storage allows)
Scan all values           O(n)

Inserting into a fixed-size array may require allocating a different array.

Dynamic array / vector

Operations & costs

A resizable contiguous array trades occasional reallocation for cheap appends. Index access remains constant in the standard model.

Fragment

Operations & costs · Dynamic array / vector
Follow appearance
GitHub Light
GitHub Dark
Rosé Pine
Catppuccin Mocha
VS Code Dark+
Index                     O(1)
Append                    amortized O(1), worst O(n)
Insert/delete in middle   O(n)
Remove last               O(1) without shrink reallocation

Resizing may move elements and invalidate references/iterators according to the native container.

Linked list

Operations & costs

Links give local insertion/removal when the required nodes are already known. Finding a position by index still requires traversal.

Fragment

Operations & costs · Linked list
Follow appearance
GitHub Light
GitHub Dark
Rosé Pine
Catppuccin Mocha
VS Code Dark+
Index lookup              O(n)
Value search              O(n)
Insert after known node   O(1)
Doubly-linked delete
  of a known node         O(1)

A singly-linked delete often also needs its predecessor. Node allocation and poor locality are additional costs.

Stack: last in, first out

Operations & costs

Push/pop operate at one end. A linked stack has worst-case constant basic operations; a dynamic-array stack has amortized constant push.

Fragment

Operations & costs · Stack: last in, first out
Follow appearance
GitHub Light
GitHub Dark
Rosé Pine
Catppuccin Mocha
VS Code Dark+
push: add top
pop: remove top
peek: inspect top
LIFO: last in, first out

Searching or indexing arbitrary stack entries is outside the basic stack interface.

Queue: first in, first out

Operations & costs

A linked queue with head/tail or a properly implemented ring buffer supports constant-time enqueue/dequeue, apart from resizing.

Fragment

Operations & costs · Queue: first in, first out
Follow appearance
GitHub Light
GitHub Dark
Rosé Pine
Catppuccin Mocha
VS Code Dark+
enqueue: add at back
dequeue: remove at front
peek: inspect front
FIFO: first in, first out

Removing the first element of an ordinary contiguous array by shifting is O(n).

Deque / ring buffer

Operations & costs

A deque supports both ends. Ring buffers reuse a bounded region through modular indexing.

Fragment

Operations & costs · Deque / ring buffer
Follow appearance
GitHub Light
GitHub Dark
Rosé Pine
Catppuccin Mocha
VS Code Dark+
push/pop front            O(1) typical ring operation
push/pop back             O(1) typical ring operation
Resize                    O(n), if supported

Full-buffer behavior is a contract: reject, block, grow or overwrite. Do not assume one.