Data structure
A way of organizing data for efficient access and modification. Arrays, linked lists, hash maps, trees, stacks, queues, and graphs are common. The right data structure makes operations fast; the wrong one makes them slow. Choosing is a core programming skill.
- Always — every program uses data structures.
- Never.
Definition
A data structure organizes data in memory for specific operations. Arrays give O(1) indexed access. Linked lists give O(1) insert/delete. Hash maps give O(1) average lookup. Trees give O(log n) search. Stacks/queues give LIFO/FIFO access. Graphs model relationships.
Why it exists
Different operations need different organizations. A hash map is fast for lookups but bad for ordered iteration. An array is fast for indexing but slow for insert. The right structure makes your code efficient.
How it works
Choose based on operations: frequent lookups → hash map, ordered data → tree/sorted array, FIFO → queue, LIFO → stack, relationships → graph. The structure determines the Big-O of each operation. Most languages provide built-in structures (arrays, maps, sets).
Scaling limits
Each structure has tradeoffs. Hash maps use more memory. Arrays are slow to resize. Trees need balancing. Graphs can be memory-heavy.
Cost shape
Free.
Alternatives
- Wrong structure — Works but slow.
Failure modes
- Using a list where a hash map is needed (O(n) vs O(1) lookup).
- Not resizing arrays.
- Unbalanced trees degrading to O(n).