Stacks
Tags: #◇CCDSALG #◇Algorithms #◇Computer Science #◇Searching #◇Data Structures #◇Search Efficiency #◇Algorithm #◇Data Abstraction #◇Accessing Elements #◇Fundamental Data Structures #◇Time Complexity #◇Space Complexity
◇Overview
A stack is an abstract data type that represents a collection of elements with two principal operations:
◇Key Concepts
PUSH POP TOP STACK_EMPTY STACK_FULL
◇Details / Theory
◇Overview
A stack is an abstract data type that represents a collection of elements with two principal operations:
PUSH: Adds an element to the top of the stack. POP: Removes the most recently added element (the top).
Stacks follow the Last-In, First-Out (LIFO) principle—the last element inserted is the first one to be removed.
◇Key Terminologies
TOP: Pointer indicating the position of the most recently inserted item in the stack. STACK_EMPTY: Check to see if the stack contains no elements. STACK_FULL: Check to see if the stack has reached its maximum capacity.
◇Stack Operations
1. CREATE(STACK)
Initializes an empty stack:
TOP = 0;
2. STACK_EMPTY(STACK)
if (TOP == 0)
return 1; // true
else
return 0; // false
3. STACK_FULL(STACK)
Assuming stack has capacity n:
if (TOP == n)
return 1; // true
else
return 0; // false
4. PUSH(STACK, x)
Adds an element x to the top:
if (STACK_FULL(STACK))
printf("Overflow error!\n");
else {
TOP = TOP + 1;
STACK[TOP] = x;
}
5. POP(STACK)
Removes and returns the top element:
if (STACK_EMPTY(STACK))
printf("Underflow Error!\n");
else {
x = STACK[TOP];
TOP = TOP - 1;
return x;
}
6. TOP(STACK)
Returns the current top element without removing it:
if (STACK_EMPTY(STACK))
printf("Stack Empty!\n");
else
return STACK[TOP];
◇Stack Simulation Walkthrough
Assume a stack S with capacity 9.
| Operation | Stack Contents | TOP |
|---|---|---|
| CREATE(S) | ? ? ? ? ? ? ? ? ? | 0 |
| PUSH(S, 8) | 8 ? ? ? ? ? ? ? ? | 1 |
| PUSH(S, 12) | 8 12 ? ? ? ? ? ? ? | 2 |
| PUSH(S, -7) | 8 12 -7 ? ? ? ? ? ? | 3 |
| PUSH(S, 1) | 8 12 -7 1 ? ? ? ? ? | 4 |
| PUSH(S, 3) | 8 12 -7 1 3 ? ? ? ? | 5 |
| PUSH(S, 17) | 8 12 -7 1 3 17 ? ? ? | 6 |
| POP(S) | returns 17 → 8 12 -7 1 3 17 ? ? ? | 5 |
| TOP(S) | returns 3 | 5 |
| STACK_EMPTY(S) | returns false | 5 |
| PUSH(S, 20) | 8 12 -7 1 3 20 ? ? ? | 6 |
| PUSH(S, 9) | 8 12 -7 1 3 20 9 ? ? | 7 |
| PUSH(S, 2) | 8 12 -7 1 3 20 9 2 ? | 8 |
| PUSH(S, 21) | 8 12 -7 1 3 20 9 2 21 | 9 |
| STACK_FULL(S) | returns true | 9 |
◇Stack Errors
Underflow: Trying to pop from an empty stack. Overflow: Trying to push into a full stack.
◇Real-World Analogies
Book pile: Only the top book is accessible. Plates stack: Only the top plate can be taken or added.
Function call stack Expression evaluation (e.g., infix to postfix conversion) Backtracking (e.g., mazes, recursion) Undo features in editors
◇Time and Space Complexities
| Operation | Time Complexity | Space Complexity |
|---|---|---|
| PUSH | ||
| POP | ||
| TOP | ||
| EMPTY/FULL |
◇Common Pitfalls
Calling pop or top on an empty stack — causes underflow error; always check empty() first Calling push on a full (fixed-size) stack — causes overflow error; always check full() first Confusing LIFO (stack) with FIFO (queue) — stacks return the most recently added element, queues return the oldest Forgetting that top only peeks without removing, while pop removes the element — mixing these up corrupts the stack state Assuming stack operations are — all core operations (push, pop, top, empty, full) are
◇Summary
TL;DR — A stack is a LIFO data structure with
push,pop,top,empty, andfulloperations. Watch for overflow (push on full) and underflow (pop/top on empty). Implemented via arrays (fixed size) or linked lists (dynamic size).
◇Links
Related: Source / Reference:
◇References & Navigation
- Previous Topic: ◇Searching Algorithms
- Next Topic: ◇Supplement-Infix-to-Postfix
- Related Notes: ◇Function Calls, ◇Searching Algorithms
ACTIVE RECALL VERIFICATION
Select an answer to verify conceptual comprehension and reveal the technical rationale.
What principle does a stack follow?
What does the PUSH operation do?
What does the POP operation do?
What error occurs if you try to POP from an empty stack?
What is the time complexity of the PUSH operation?
Which operation returns the current top element WITHOUT removing it?
In the Stack Simulation Walkthrough, after PUSHing 8, 12, -7, 1, 3, 17 and then calling POP once, what does the next TOP operation return?
What error occurs if you try to PUSH into a stack that has reached its maximum capacity?
Which of the following is a real-world application of a stack?
What is a common pitfall when working with stacks and queues?