Data Structures - Stack
A stack is a linear data structure that follows the Last In First Out (LIFO) principle. It can be visualized as a stack of plates - you can only place a plate on top and only take one from the top.
Concept and Principle of Stacks
Stack - LIFO Principle Illustration
Stack Top (Top) - the only place you can operate
Plate 1 (Stack Bottom)
Plate 2
Plate 3
Plate 4 (Stack Top)
Stack Bottom (Bottom) - only removable last
push (insert onto stack)
pop (remove from stack)
LIFO verification: push 10->20->30, pop 30->20->10 (last in, first out) | the stack only cares about the top, not the middle
Array Implementation (Sequential Stack)
Storage: fixed-size array + top pointer
push:items[++top] = value
pop:return items[top--]
Pros: fast access, cache-friendly, simple to implement
Cons: fixed capacity, may overflow
Linked List Implementation (Linked Stack)
Storage: dynamic nodes + stack top pointer (list head)
push: head insertion, newNode->next = top
pop: top = top->next; free(old head)
Pros: no capacity limit, flexible dynamic expansion
Cons: extra pointer overhead, not cache-friendly
Array Implementation of a Stack
Example
#include
#include
#define MAX_SIZE 100 /* Maximum stack capacity */
/* Stack struct: contains a data array and a top pointer */
struct
Stack
{
int
items
[
MAX_SIZE
]
;
/* Array storing stack elements */
int
top
;
/* Top pointer, -1 means the stack is empty */
}
;
/* Initialize the stack: set top to -1 */
void
initStack
(
struct
Stack
s
)
{
s
->
top
=
1
;
}
/* Empty check: the stack is empty when top is -1 */
bool isEmpty
(
struct
Stack
s
)
{
return
s
->
top
==
1
;
}
/* Full check: the stack is full when top reaches capacity limit - 1 */
bool isFull
(
struct
Stack
s
)
{
return
s
->
top
==
MAX_SIZE
1
;
}
/* Push: place the element at the top of the stack
Move top first, then write the data */
void
push
(
struct
Stack
s
,
int
value
)
{
if
(
isFull
(
s
)
)
{
printf
(
"Stack is full, cannot push!\n"
)
;
return
;
}
s
->
items
[
++
s
->
top
]
=
value
;
/* Increment top first, then assign */
printf
(
"Push: %d\n"
,
value
)
;
}
/* Pop: remove and return the top element
Retrieve the data first, then move top */
int
pop
(
struct
Stack
s
)
{
if
(
isEmpty
(
s
)
)
{
printf
(
"Stack is empty, cannot pop!\n"
)
;
return
1
;
/* Return a special value to indicate an error */
}
return
s
->
items
[
s
->
top
--
]
;
/* Return the value first, then decrement top */
}
/* Peek at the top: do not pop, only view */
int
peek
(
struct
Stack
s
)
{
if
(
isEmpty
(
s
)
)
{
printf
(
"Stack is empty!\n"
)
;
return
1
;
}
return
s
->
items
[
s
->
top
]
;
}
int
main
(
)
{
struct
Stack s
;
initStack
(
&
s
)
;
push
(
&
s
,
10
)
;
/* Push: 10 */
push
(
&
s
,
20
)
;
/* Push: 20 */
push
(
&
s
,
30
)
;
/* Push: 30 */
printf
(
"Top element: %d\n"
,
peek
(
&
s
)
)
;
/* Output: Top element: 30 */
printf
(
"Pop: %d\n"
,
pop
(
&
s
)
)
;
/* Output: Pop: 30 (last in, first out) */
printf
(
"Pop: %d\n"
,
pop
(
&
s
)
)
;
/* Output: Pop: 20 */
printf
(
"Pop: %d\n"
,
pop
(
&
s
)
)
;
/* Output: Pop: 10 */
if
(
isEmpty
(
&
s
)
)
{
printf
(
"Stack is empty\n"
)
;
/* Output: Stack is empty */
}
return
0
;
}
Linked List Implementation of a Stack
Example
#include
#include
#include
/* Linked list node */
struct
Node
{
int
data
;
struct
Node
next
;
}
;
/* Push: use the list head as the stack top, head insertion
Time complexity: O(1) */
struct
Node
push
(
struct
Node
top
,
int
value
)
{
struct
Node
newNode
=
(
struct
Node
)
malloc
(
sizeof
(
struct
Node
)
)
;
newNode
->
data
=
value
;
newNode
->
next
=
top
;
/* New node points to the original stack top */
printf
(
"Push: %d\n"
,
value
)
;
return
newNode
;
/* New node becomes the new stack top */
}
/* Pop: remove the list head node
Time complexity: O(1) */
struct
Node
pop
(
struct
Node
top
)
{
if
(
top
==
NULL
)
{
printf
(
"Stack is empty!\n"
)
;
return
NULL
;
}
struct
Node
temp
=
top
;
printf
(
"Pop: %d\n"
,
top
->
data
)
;
top
=
top
->
next
;
/* Move the stack top back */
free
(
temp
)
;
/* Free the old stack top */
return
top
;
}
/* Empty check */
bool isEmpty
(
struct
Node
top
)
{
return
top
==
NULL
;
}
int
main
(
)
{
struct
Node
stackTop
=
NULL
;
/* Stack is empty at initialization */
stackTop
=
push
(
stackTop
,
100
)
;
/* Push: 100 */
stackTop
=
push
(
stackTop
,
200
)
;
/* Push: 200 */
stackTop
=
push
(
stackTop
,
300
)
;
/* Push: 300 */
printf
(
"Top: %d\n"
,
stackTop
->
data
)
;
/* Output: Top: 300 */
stackTop
=
pop
(
stackTop
)
;
/* Pop: 300 */
stackTop
=
pop
(
stackTop
)
;
/* Pop: 200 */
stackTop
=
pop
(
stackTop
)
;
/* Pop: 100 */
if
(
isEmpty
(
stackTop
)
)
{
printf
(
"Stack is empty\n"
)
;
/* Output: Stack is empty */
}
return
0
;
}
The advantage of a linked-list stack is that in theory it has no fixed capacity limit and is only constrained by available system memory. But each node requires extra pointer storage space.
Application Scenarios of Stacks
| Application scenarios | Description | Why Use a Stack |
|---|---|---|
| Function Call Stack | Save the function's return address and local variables | Function calls are nested, naturally LIFO |
| Bracket matching | Check whether brackets in code are correctly paired | Push left brackets when encountered; on a right bracket, match and pop from the stack top |
| Undo operation | Text editors and browser undo functionality | Push each operation onto the stack; pop when undoing |
| Expression Evaluation | Compute the value of infix/postfix expressions | Push operands onto the stack; after computing an operator, push the result |
| Depth-First Search | Graph DFS traversal algorithm | Explicitly use a stack or recursion (implicitly uses the call stack) |