Linear Data Structures
Arrays, strings, linked lists, stacks, queues and hash tables: the building blocks every other structure uses.
Arrays and strings
Contiguous storage: indexing, shifting, growing arrays, matrices and strings.
- Arrays: indexing, traversal, insertion and deletion costs Arrays give instant access by index, but inserting or deleting in the middle shifts every later element. Watch elements shift one by one.
- Dynamic arrays: capacity, growth, amortized append A dynamic array grows by copying into a bigger block when it fills. Doubling the capacity keeps appends O(1) on average.
Linked lists
Nodes joined by pointers: singly, doubly and circular lists and the classic list techniques.
- Singly linked lists: traverse, insert, delete A singly linked list is a chain of nodes, each pointing to the next. Walk the chain with a cursor and rewire two arrows to insert or delete.
- Linked list techniques: reverse, middle, merge, detect a cycle Four classic list moves: reverse by rewiring pointers, find the middle and detect cycles with slow and fast runners, and merge two sorted…
Stacks and queues
Last-in-first-out and first-in-first-out structures and the problems they solve.
- Stacks: push, pop, peek; array and linked implementations A stack adds and removes at the top only: last in, first out. Watch push, pop and peek on array and linked implementations.
- Queues: enqueue, dequeue; circular buffer A queue adds at the back and removes at the front: first in, first out. A circular buffer reuses its slots as head and tail wrap around.
Hashing
Turn keys into array positions for near-constant-time lookup, and handle collisions.
- Hash functions: from key to bucket index A hash function turns a key into a bucket index. See keys map to buckets and how a good function spreads them evenly.
- Hash tables with separate chaining With separate chaining, each bucket holds a small list of the keys that hash there. Insert, search and delete walk one short chain.
- Open addressing: linear, quadratic probing, double hashing Open addressing stores every key in the bucket array itself and probes for the next free slot. Compare linear, quadratic and double-hash…
- Load factor and resizing The load factor is how full a hash table is. When it passes a threshold the table doubles and every key moves to a new bucket.
- Hash sets and maps in practice: counting, lookup, deduplication Hash sets and maps answer "have I seen this?" in O(1). Use them to count, look up, group and deduplicate, as in Two Sum.
- Collisions, worst cases and mutable keys Bad keys can pile into one bucket and turn a hash table into a slow list. Mutable keys can get lost after they change.