Why Stack Exists and What Problems It Solves in DSA C - Performance Analysis
Stacks help us manage data in a last-in, first-out way. Understanding their time complexity shows why they are fast and useful for certain problems.
We want to know how the time to add or remove items grows as we use more data.
Analyze the time complexity of the following stack operations.
// Simple stack push and pop operations
#define MAX 100
int stack[MAX];
int top = -1;
void push(int x) {
if (top < MAX - 1) {
stack[++top] = x;
}
}
int pop() {
if (top >= 0) {
return stack[top--];
}
return -1; // empty stack
}
This code adds (push) and removes (pop) items from the stack one at a time.
Identify the loops, recursion, array traversals that repeat.
- Primary operation: Single step push or pop on the stack array.
- How many times: Each push or pop happens once per call, no loops inside.
Each push or pop takes the same small amount of work, no matter how many items are in the stack.
| Input Size (n) | Approx. Operations |
|---|---|
| 10 | 10 simple steps |
| 100 | 100 simple steps |
| 1000 | 1000 simple steps |
Pattern observation: Time grows directly with number of operations, but each operation is very fast and constant.
Time Complexity: O(1)
This means each push or pop takes the same fixed time, no matter how big the stack is.
[X] Wrong: "Pushing or popping from a stack takes longer as the stack grows."
[OK] Correct: Each operation only changes one position in the array, so it always takes the same short time.
Knowing stack operations are fast helps you choose the right tool for problems like undo actions, expression evaluation, or backtracking.
"What if we used a linked list instead of an array for the stack? How would the time complexity change?"
Practice
Solution
Step 1: Understand the purpose of a stack
A stack stores data in a last-in, first-out order, which is perfect for tracking function calls and returns.Step 2: Identify common uses of stack
Stacks are mainly used to keep track of function calls, especially in recursion, where the last called function must finish first.Final Answer:
To keep track of function calls and return addresses -> Option BQuick Check:
Stack = function call tracking [OK]
- Thinking stack stores data randomly
- Confusing stack with queue
- Assuming stack sorts data
Solution
Step 1: Understand stack push operation
To push, we first increase the top index, then assign the value at that position.Step 2: Check each option
stack[++top] = value; increments top before assignment, which is correct. stack[top++] = value; assigns then increments, which may overwrite. stack[top] = value++; increments value, not top. stack[top--] = value; decrements top, which is wrong for push.Final Answer:
stack[++top] = value; -> Option AQuick Check:
Push = increment top then assign [OK]
- Incrementing after assignment causes overwrite
- Decrementing top on push
- Incrementing value instead of top
int stack[3];
int top = -1;
stack[++top] = 10;
stack[++top] = 20;
stack[++top] = 30;
printf("%d", stack[top]);Solution
Step 1: Trace stack push operations
Initially, top = -1. After first push, top=0, stack[0]=10. Second push: top=1, stack[1]=20. Third push: top=2, stack[2]=30.Step 2: Check printed value
printf prints stack[top], which is stack[2] = 30.Final Answer:
30 -> Option CQuick Check:
Last pushed value = 30 [OK]
- Printing stack[0] instead of stack[top]
- Confusing top index after pushes
- Assuming runtime error without overflow
int pop(int stack[], int *top) {
if (*top == -1) {
printf("Stack Underflow\n");
return -1;
}
return stack[*top--];
}Solution
Step 1: Understand pointer decrement in return statement
The expression stack[*top--] uses post-decrement on *top, but *top-- decrements the pointer, not the value it points to.Step 2: Correct way to decrement top value
It should be stack[(*top)--] to decrement the value pointed by top after accessing stack.Final Answer:
Incorrect decrement of top pointer -> Option AQuick Check:
Use (*top)-- to decrement value, not pointer [OK]
- Using *top-- instead of (*top)--
- Ignoring pointer vs value decrement
- Not checking stack underflow
Solution
Step 1: Understand stack reversal logic
Pushing all characters onto the stack stores them in order. Popping them reverses the order due to last-in, first-out behavior.Step 2: Evaluate options
Push each character of the string onto the stack, then pop all characters to form the reversed string correctly pushes all characters then pops to reverse. Push characters only if they are vowels, then pop to reverse only pushes vowels, losing other characters. Pop characters first, then push them back to reverse pops before pushing, which is invalid. Use a queue instead of a stack to reverse the string suggests queue, which reverses order differently.Final Answer:
Push each character of the string onto the stack, then pop all characters to form the reversed string -> Option DQuick Check:
Stack reversal = push all, then pop all [OK]
- Pushing only some characters
- Popping before pushing
- Using queue instead of stack for reversal
Ready to prove it?
Pass the test and this pattern moves unknown
