4.2Stack with a Linked List
Array or linked list?
You have coded a stack in two ways: with an array and with a linked list. Both offer the same operations and all of them take O(1). This lesson shows where the differences are: memory, capacity and real-world speed. By the end you will know which one to pick and when.
- Why both implementations are the same abstract data type and why the calling code does not change
- How they differ in time, memory per element, capacity and cache behaviour
- When an array fits better and when a linked list does
1One abstract data type, two structures
A stack is an abstract data type: it says which operations it offers, not how they are coded. Both versions use the same function names and signatures. Only the inside of struct Stack changes.
// With an array (4.1)struct Stack { int items[CAPACITY]; int top; // index of the top, -1 when empty}; // With a linked list (4.2)struct Stack { struct Node* top; // the top node, NULL when empty};The code that uses the stack does not change at all. The snippet below compiles with either version and gives the same result:
struct Stack s;int x;create(&s);push(&s, 10);push(&s, 20);if (peek(&s, &x)) printf("%d\n", x); // 20while (pop(&s, &x)) printf("%d ", x); // 20 10The only difference is isFull: the array version has it, the linked-list version does not need it. So as long as the calling code does not rely on a fixed capacity, switching implementations means changing a single file.
2Time
All four operations are O(1) in both versions. The array finds the top with an index, the linked list with the top pointer. No operation shifts elements or has to traverse the list.
There is one detail. If you use a dynamic array that moves to a bigger array when it fills up, the push that triggers the move copies every element and takes O(n). If the capacity doubles each time, these expensive pushes are rare and the average cost stays O(1). This is called amortized O(1). In a linked list every push really is O(1), but every push calls malloc, and malloc is slower than writing into an array cell.
3Memory
In an array each element takes only its own size: 4 bytes for an int. In a linked list each node carries a next pointer next to the value. On a 64-bit system a pointer is 8 bytes, so a node is at least 12 bytes and, with alignment, usually 16. On top of that comes the record malloc keeps for each allocation (8 to 16 bytes on most systems).
On the other hand, a fixed array reserves its full capacity whether it is used or not. A stack with capacity 1000 holding 3 elements has 997 empty cells. A linked list only takes space for the elements that exist.
4Capacity and overflow
When a fixed array stack is full, push fails; that is an overflow. You have to guess the capacity correctly up front. A linked list has no such limit: push fails only when memory is completely used up and malloc returns NULL.
5Cache behaviour
Array elements sit next to each other in memory. When the processor reads one cell it brings its neighbours into the cache too, so pushes and pops in a row are very fast. Linked-list nodes can be scattered across memory; each access may go to an address that is not in the cache. Despite the same Big-O, the array version is usually faster in practice. The introduction to linked lists covers this as well.
6Summary table
| Property | Array stack | Linked-list stack |
|---|---|---|
| push, pop, peek, isEmpty | O(1) | O(1) |
| push (dynamic array) | Amortized O(1), O(n) while growing | Always O(1), malloc every time |
| Memory per element (int) | 4 bytes | 12 bytes or more, plus the malloc record |
| Unused space | Empty cells take space | None |
| Capacity | Fixed; overflow when full | Until memory runs out |
| isFull | Yes | Not needed |
| Cache behaviour | Good: elements side by side | Weak: nodes scattered |
| Memory management (C) | None | free on every pop |
7Which one to choose?
- Array: when you know the maximum number of elements or can set a sensible upper limit. It is fast and simple, and in C there is no
freeto worry about. Embedded systems and contest code usually pick this. - Dynamic array: when you do not know the size but speed still matters. Java's
ArrayDequeand Python's list work this way when used as a stack. - Linked list: when the size varies a lot and you need every push to be truly O(1) (no occasional O(n) copy), or when the nodes already come from another structure.
In an exam, the answer to "which one is better" is not a single word. Say that time is O(1) in both, then explain the difference through memory, capacity and the cache.
8Try it
On a 64-bit system a stack of ints holds 1000 elements. Which one uses less memory?
The answer is B. If the array is full there are no empty cells and each element is 4 bytes. In a linked list every node also carries an 8-byte pointer and a malloc record. The linked list only wins when the array is mostly empty.
Show the answer
The answer is B. If the array is full there are no empty cells and each element is 4 bytes. In a linked list every node also carries an 8-byte pointer and a malloc record. The linked list only wins when the array is mostly empty.
You replaced an array stack with a linked-list stack. What has to change in the code that uses the stack?
The answer is B. A stack is an abstract data type. The calling code only knows create, isEmpty, push, pop and peek; those stay the same even when the structure inside changes.
Show the answer
The answer is B. A stack is an abstract data type. The calling code only knows create, isEmpty, push, pop and peek; those stay the same even when the structure inside changes.
Lessons you mark show up in the curriculum. If you are signed in, they are saved to your account too.