Data Structures · Stacks and Expression Evaluation
Array versus Linked List Implementation of a Stack
Overview
A stack can be implemented in only two ways: with an array (contiguous memory, fixed size) or with a linked list (heap memory, nodes joined by links); a combination of the two is also possible.
The choice matters because the array requires knowing the maximum size in advance but is generally faster, while the linked list grows and shrinks as needed but often takes more time — yet for a stack, both give constant-time push and pop.
Key concepts
- Two implementation methods: any data structure can be implemented (1) using an array — contiguous allocation with a fixed size, or (2) using heap memory — create a structure and use links, i.e. a linked list. A combination of the two is a third possibility.
These are the only implementation methods available for any data structure, including the stack. - Stack: a data structure with two defined operations — push (insert an element onto the stack) and pop (remove an element from the stack).
- Array implementation — core problem: the maximum size the stack will grow to must always be known in advance.
- Linked list implementation: uses dynamic memory allocation with structures; its advantage is flexibility — the structure can grow and shrink as needed; its disadvantage is that it may take more time to implement the data structure in many cases.
- Stack special case: whether implemented with an array or a linked list, all stack operations take the same time — constant time, .
How it works
- Decide the implementation: array (contiguous, fixed size) or linked list (heap memory, links), or a combination of the two.
- If the maximum size can be predicted, choose the array — in most cases the implementation is faster.
- If the size cannot be predicted, use dynamic memory allocation with structures (linked list) for flexibility to grow and shrink.
- Implement the two stack operations: push puts an element on top of the stack; pop takes an element off the top of the stack.
- Either implementation performs push and pop in constant time, .
Formulas
- Push and pop time for a stack, array or linked list: (constant time)
Comparison
| Criterion | Array implementation | Linked list implementation |
|---|---|---|
| Memory allocation | Contiguous (array) | Heap memory, nodes linked by pointers |
| Size | Fixed, must be known in advance | Dynamic — grows and shrinks as needed |
| Flexibility | Low (size must be predicted) | High |
| Speed (general data structures) | Faster in most cases | May take more time in many cases |
| Speed for stack operations | Constant time | Constant time — same as array |
Revision summary
- Only two ways to implement any data structure: array (contiguous, fixed size) or linked list (heap, links); a combination of the two is also possible.
- The array implementation forces you to know the maximum stack size in advance — very difficult in most cases.
- Choose the array only when the size can be predicted, because it is faster in most cases.
- The linked list gives flexibility (grow/shrink) but may take more time for general data structures.
- For a stack, both implementations give constant time for every operation — push and pop are equally fast either way.
- Stack operations: push (insert at top) and pop (remove from top).
Previous-year exam insights
- In the array implementation, push must first check for overflow (top == MAX-1) and pop must check for underflow (top == -1);
in the linked-list implementation, push fails only when the heap is exhausted (malloc returns NULL) and pop fails on an empty list. - The array implementation needs one index variable top (initialised to -1);
push does top = top + 1 then A[top] = x, and pop returns A[top] then top = top - 1. - The linked-list implementation pushes at the head (new node's next = top;
top = new node) and pops from the head, so no traversal is needed and both remain . - A dynamic array that doubles its capacity gives amortised push, but a single push that triggers resizing costs and temporarily needs about twice the memory.
- The array implementation wastes memory when the size is overestimated and cannot grow beyond its declared size; the linked-list implementation uses extra memory per element for the pointer/link field.
- A linked-list stack supports multiple stacks sharing memory easily (each with its own top pointer), whereas k stacks in one array require either fixed partitions or a more complex free-list scheme.