Dynamic Arrays and Linked Lists

Array vs. Linked List Implementation: What's the Difference?

An array stores elements in one block and reaches them by index. A linked list chains separate nodes together. Compare access, insertion and memory use.

On this page
  1. What is the difference between an array and a linked list?
  2. What each one looks like in code
  3. How the two compare
  4. Speed and memory layout
  5. Which one to use

What is the difference between an array and a linked list?

An array stores its elements in one contiguous block of memory. You reach any element directly by its index. A linked list stores each element in its own node. Each node holds a value and a pointer to the next node, so the nodes can sit anywhere in memory. To reach the third element of a linked list, you start at the head and follow two pointers.

What each one looks like in code

An array of three integers:

int numbers[3] = {10, 20, 30};   // one block of memory
int third = numbers[2];          // direct access by index

A linked list node:

struct Node {
    int value;
    struct Node *next;   // points to the next node, or NULL at the end
};

The two layouts look like this:

Array:        | 10 | 20 | 30 |            one block, index 2 is found directly

Linked list:  [10 | *] -> [20 | *] -> [30 | NULL]    nodes can sit anywhere

How the two compare

Task Array Linked list
Read the element at index i Direct, the position is calculated from i Walk from the head through i nodes
Insert or remove at the front Shift every element over Change the head pointer
Insert or remove in the middle, once you know the spot Shift the elements after it Change a couple of pointers
Find a value you don’t know the position of Check elements one at a time Check nodes one at a time
Memory used per element The value only The value plus a pointer
Changing the size Fixed when declared, unless you allocate a new block and copy Grows one node at a time

The costs above are often written in big O notation. O(1) means the time does not grow with the number of elements. O(n) means it grows in step with that number.

Speed and memory layout

Arrays are often faster when you scan through many elements. Their elements sit next to each other in memory, so the processor can load them efficiently, and nearby values tend to stay in the processor’s cache. A linked list’s nodes may be spread across memory, so each step to the next node can be slower. Linked lists also need extra memory for each pointer.

The table compares how the number of steps grows, not how long each step takes. A linked list insert in the middle is quick once you have the node, but finding that node first usually means walking the list.

Which one to use

  • For reading by index, or for a list whose size changes little, use an array. In C++, a growable array is usually std::vector, which the guide to arraylist in c explains.
  • For frequent insertions and removals at places you already have a pointer to, a linked list can fit. In C++, the standard version is covered in c std::list.

The complete guide to dynamic arrays and linked lists

ArrayList in C++: Using std::vector
Read the guide

Keep exploring

Guides from other topics

All topics →

The const Keyword in C++

Learn what the const keyword does in C++, how it applies to variables, pointers, references and member functions, and how the compiler enforces it.

const 5 min read

std::function in C++

std::function is a C++ wrapper that holds any callable with a matching signature, such as a function, lambda or functor, and calls it the same way.

std::function 5 min read