📖 Scenario: Imagine you are organizing a stack of plates in a kitchen. You can only add or remove the top plate. This is how a stack data structure works in programming.Stacks help solve problems where the last thing added is the first thing you need to remove, like undo actions in text editors or checking if parentheses in math expressions are balanced.
🎯 Goal: You will create a simple stack using an array in C, push some numbers onto it, then pop them off to see how the last number added is the first one removed.
📋 What You'll Learn
Create an integer array called stack with size 5
Create an integer variable called top and set it to -1
Write a function push that adds a number to the stack and updates top
Write a function pop that removes and returns the top number from the stack and updates top
Push the numbers 10, 20, and 30 onto the stack
Pop all numbers from the stack and print them in order
💡 Why This Matters
🌍 Real World
Stacks are used in many real-world applications like undo features in text editors, browser history, and expression evaluation.
💼 Career
Understanding stacks is important for software developers to solve problems involving order and reversals, and is often asked in coding interviews.
Progress0 / 4 steps
1
Create the stack array and top variable
Create an integer array called stack with size 5 and an integer variable called top set to -1.
DSA C
Hint
Think of top as the position of the last added item. Starting at -1 means the stack is empty.
2
Write the push function
Write a function called push that takes an integer value, adds it to the stack at position top + 1, and updates top accordingly.
DSA C
Hint
Check if top is less than 4 before adding to avoid overflow.
3
Write the pop function
Write a function called pop that removes and returns the top value from the stack and updates top. If the stack is empty (top == -1), return -1.
DSA C
Hint
Remember to decrease top after removing the top item.
4
Push and pop values, then print the popped values
Push the numbers 10, 20, and 30 onto the stack using push. Then pop all values from the stack using pop and print each popped value on a new line.
DSA C
Hint
Use a loop to pop until pop() returns -1, printing each value.
Practice
(1/5)
1. Why do we use a stack data structure in programming?
easy
A. To store data in random order
B. To keep track of function calls and return addresses
C. To sort numbers quickly
D. To store large amounts of data permanently
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 B
Quick Check:
Stack = function call tracking [OK]
Hint: Stack = last-in, first-out for function calls [OK]
Common Mistakes:
Thinking stack stores data randomly
Confusing stack with queue
Assuming stack sorts data
2. Which of the following is the correct way to push an element onto a stack in C?
easy
A. stack[++top] = value;
B. stack[top] = value++;
C. stack[top++] = value;
D. stack[top--] = value;
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 A
Quick Check:
Push = increment top then assign [OK]
Hint: Push means increment top first, then assign [OK]
Common Mistakes:
Incrementing after assignment causes overwrite
Decrementing top on push
Incrementing value instead of top
3. What will be the output of the following C code snippet?
int stack[3];
int top = -1;
stack[++top] = 10;
stack[++top] = 20;
stack[++top] = 30;
printf("%d", stack[top]);
medium
A. 10
B. 20
C. 30
D. Runtime error
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 C
Quick Check:
Last pushed value = 30 [OK]
Hint: Last pushed value is at stack[top] [OK]
Common Mistakes:
Printing stack[0] instead of stack[top]
Confusing top index after pushes
Assuming runtime error without overflow
4. Identify the error in this stack pop function in C:
int pop(int stack[], int *top) {
if (*top == -1) {
printf("Stack Underflow\n");
return -1;
}
return stack[*top--];
}
medium
A. Incorrect decrement of top pointer
B. Missing check for stack overflow
C. Returning wrong stack element
D. No error, function is correct
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 A
Quick Check:
Use (*top)-- to decrement value, not pointer [OK]
Hint: Use (*top)-- to decrement value, not pointer [OK]
Common Mistakes:
Using *top-- instead of (*top)--
Ignoring pointer vs value decrement
Not checking stack underflow
5. You want to reverse a string using a stack in C. Which approach correctly uses the stack to solve this problem?
hard
A. Use a queue instead of a stack to reverse the string
B. Push characters only if they are vowels, then pop to reverse
C. Pop characters first, then push them back to reverse
D. Push each character of the string onto the stack, then pop all characters to form the reversed string
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 D
Quick Check:
Stack reversal = push all, then pop all [OK]
Hint: Push all chars, then pop all to reverse [OK]
Common Mistakes:
Pushing only some characters
Popping before pushing
Using queue instead of stack for reversal
Recommended
Ready to prove it?
Pass the test and this pattern moves unknownstrongwhile earning +1 Grip toward your certificate.