Skip to content

Arrays, Linked Lists, Stacks & Queues

A contiguous block and a chain of pointers buy different operations at different costs

1. Two memory layouts

An array stores its elements next to each other in one block. A linked list stores each element in its own node, plus a pointer to the next node. That single difference decides which operations are cheap. The array wins at random access; the linked list wins at splicing a node into the middle when you already hold it.

Array address arithmetic

The address formula is why a[i] is O(1): no traversal, just arithmetic. A linked list has no such formula. To reach node i, the machine must follow i pointers from the head.

2. Array costs

Arrays are compact and cache-friendly. The CPU reads a cache line at a time, so a linear scan over contiguous memory is often much faster in practice than a linked-list scan, even though both are O(n) in the abstract model.

  • Index — O(1).
  • Search by value — O(n) unless the array is sorted and you binary search.
  • Insert/delete in the middle — O(n), because every later element shifts.
  • Append to a dynamic array — O(1) amortized; the occasional resize copies all n elements, but the cost spread over n appends is constant.

3. Linked-list costs

A singly linked list node holds a value and a next pointer. The head is the only entry point unless you also store a tail. Inserting after a node you already hold is O(1): allocate the node, point it at the old next, point the old node at it. Finding that node is the expensive part.

  • Index i — O(i), because you must walk from the head.
  • Search — O(n).
  • Insert/delete given a node — O(1) for a singly linked list if you have the previous node; a doubly linked list removes that caveat.
  • Memory — one extra pointer per node, plus allocator overhead and poor locality.

The trade in one sentence

Arrays make the position cheap and the splice expensive; linked lists make the splice cheap and the position expensive. Neither is “better” without an operation mix.

4. Stacks & queues

A stack and a queue are restrictions on a list, not new memory layouts. A stack pushes and pops at one end: last in, first out. A queue enqueues at the back and dequeues at the front: first in, first out. Both can be implemented with a dynamic array or a linked list, and both have O(1) core operations.

  • Stack — function calls, undo, depth-first search, expression parsing.
  • Queue — breadth-first search, scheduling, buffering, printer jobs.
  • Deque — a double-ended queue; both ends are O(1).

Example: inserting into an array vs a linked list

An array has n = 1,000,000 elements. Insert at index 0: every element shifts, so about 1,000,000 moves. Insert at index 500,000: about 500,000 moves. Insert at the end: one write (amortized).

A linked list with a pointer to the node before the insertion point: two pointer updates, independent of n. But finding that node by index still costs O(n), so the advantage disappears unless the traversal was already needed.

5. Practice

Try it

Why is array indexing O(1) while linked-list indexing is O(n)?

Try it

A dynamic array doubles when full. Why is append O(1) amortized?

Try it

Which structure would you choose for a FIFO job queue, and why?

Try it

What does a stack have to do with recursion?

6. Resources

Next, the hash table asks a different question: can we avoid searching at all by computing the location from the key? The answer is yes, with a collision strategy.

Next: hash tables →

Continue the track

Site soundtrack