Memory Layout & Arrays
The two fundamental sequence structures differ in memory layout, and everything about their performance follows from it.
An array stores elements in a single contiguous block of memory. Because elements are adjacent and equally sized, the address of element is computed directly as , giving random access to jump to any index instantly.
The cost is rigidity: inserting or removing in the middle requires shifting all following elements, which takes time, and a fixed array cannot grow beyond its allocated size.
Linked Lists & Trade-offs
A linked list stores each element in a separate node holding a pointer to the next node. Nodes sit anywhere in memory; the chain is held together by pointers.
| Operation | Array | Linked list |
|---|---|---|
| Access element i | ||
| Insert/remove at known position | ||
| Grow beyond capacity | Costly / fixed | Easy |
Common pitfall: assuming linked lists are faster because insertion is . That insertion assumes you already hold the position; finding it takes , and arrays are far more cache-friendly.