Skip to main content

Data Structures

A data structure is a representation plus invariants that make a set of operations efficient. Choose one from the workload—not from a generic ranking of “advantages” and “disadvantages.”

Start with operations

NeedTypical representationStrength
Random positional accessarray / dynamic arrayO(1)O(1) indexing, locality
Local insertion with a known nodelinked listpointer rewiring
Last-in, first-out accessstack ADTone-ended discipline
First-in, first-out accessqueue ADTordered processing
Exact key lookuphash tableexpected constant-time lookup
Ordered keys and rangesbalanced search treelogarithmic ordered operations
Repeated minimum/maximumheaplogarithmic updates, constant root access
Arbitrary relationshipsgraph representationtraversal and path algorithms

Representation versus interface

A stack or queue is an abstract data type: its contract restricts which element may be removed. It can be implemented by an array, linked nodes, or another container. An array and linked list instead describe storage organization.

Questions to ask

  1. Which operations dominate, and what are their worst or amortized costs?
  2. Must references, indexes, or iteration order remain stable after mutation?
  3. Is capacity bounded, and is allocation allowed during operation?
  4. How important are cache locality and per-element overhead?
  5. Are concurrency, persistence, or ownership part of the contract?

Asymptotic costs are necessary but incomplete: two O(1)O(1) operations can differ substantially in allocation, indirection, and locality.

Source