Data Structures

A data structure is a particular way of arranging the addresses and pointers from the previous topic so that specific operations can be done efficiently, and most of the structures in this topic are really just different tradeoffs built from those same two ingredients. An array is the simplest structure: a single contiguous block of memory holding same-sized elements back to back, so any element can be reached instantly by computing its address directly from its index, though inserting into the middle means physically shifting every element after it over by one. A string is, underneath the name, just an array of characters using the text encoding covered earlier in this series, which is why string operations inherit an array's exact same tradeoffs. A linked list trades that instant indexing away for cheap insertion: each node holds a value plus a pointer to the next node, exactly the pointer mechanism the previous topic just introduced, so inserting a new node anywhere just means rewriting two pointers, but finding the 500th element means walking all 500 pointer hops one at a time, since there's no way to compute a middle node's address directly the way an array allows. A stack restricts access to one end only, last in first out, which is the exact same logical shape as the call stack the previous topic already covered automatically managing every function call; a queue restricts access the other way, first in first out, modeling a real waiting line, used anywhere work needs to be processed in the order it arrived. A hash table solves the linked list's slow-search problem by running each key through a hash function that converts it into an array index, giving average-case instant lookup, insertion, and deletion all at once — the catch is that two different keys can hash to the same index, a collision, which every real hash table implementation has to handle explicitly, usually by chaining a small linked list at each occupied slot. A tree generalizes the linked list from one chain into a branching hierarchy, and the filesystem tree covered earlier in this series is already exactly this: each node can point to multiple children, with a binary search tree further keeping every left child smaller and every right child larger than its parent, which turns a search into a series of smaller-or-larger decisions that discards half the remaining tree at every step. A graph, already introduced formally in discrete mathematics as nodes and edges, is the most general structure of all — a tree is simply a graph with no cycles and exactly one path between any two nodes, and a graph models anything with arbitrary many-to-many connections rather than a strict hierarchy. Choosing the right structure for a given problem is the single decision that determines which operations stay fast and which ones become painfully slow, which is exactly the lens the next topic, algorithms, applies directly to the structures introduced here.

What operation matters most?
fast lookup by position, fast insert, or fast search by value
Array / string
instant index access, slow middle insert
Linked list
cheap insert anywhere, slow search
Hash table
fast average lookup by key, not ordered
Tree / graph
models hierarchy or arbitrary connections
Pick the structure whose strength matches the bottleneck
algorithms (next) depend directly on this choice
Array vs. Linked List: Opposite Tradeoffs Same data, two completely different memory layouts Array: contiguous, indexed A B C reach C instantly: one address calculation insert before B: shift C over Linked list: scattered, chained A B C insert before B: rewrite 2 pointers reach C: walk A then B then C a stack and a queue are both built from one of these two layouts, just restricting where you're allowed to add or remove stack: one end only, last in first out; queue: both ends, first in first out neither layout is better, they're better at different things
Hash Tables, Trees, and Graphs A key converted to an index, and two shapes of connection Hash table key: report.pdf index 2 average case: instant lookup, insert, and delete collisions chain a small list at that slot Tree vs. graph a tree: no cycles, exactly one path root to any node a graph: arbitrary many-to-many connections the filesystem tree is already an example a hash table is the array's instant indexing, reached via a key instead of a number a tree is a linked list that branches instead of staying a single chain every structure in this topic is one of the earlier ones, specialized for a purpose

Take data structures further

Ask the copilot about this term, or have our engineers assess it against your process.