📖 Scenario: Imagine you are building a simple text editor. You want to keep track of the actions a user performs so you can undo them one by one. A stack is perfect for this because it works like a stack of plates: the last action done is the first one to undo.
🎯 Goal: You will build a stack using an array in C++. You will create the stack, set its size, add items (push), remove items (pop), and finally print the stack's contents.
📋 What You'll Learn
Create an integer array called stack with size 5
Create an integer variable called top and set it to -1
Create an integer variable called max_size and set it to 5
Write a push operation that adds an integer to the stack if there is space
Write a pop operation that removes the top item from the stack if it is not empty
Print the stack contents from bottom to top after operations
💡 Why This Matters
🌍 Real World
Stacks are used in undo features, browser history, expression evaluation, and more.
💼 Career
Understanding stack implementation helps in software development, debugging, and working with system internals.
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 pointer to the last added item. Initially, it is -1 because the stack is empty.
2
Add max_size variable
Create an integer variable called max_size and set it to 5.
DSA C++
Hint
This variable will help us check if the stack is full before adding new items.
3
Write push and pop operations
Write a push operation that adds an integer value to stack only if top is less than max_size - 1. Then write a pop operation that removes the top item by decreasing top only if top is not -1.
DSA C++
Hint
Remember, push adds an item on top, and pop removes the top item by moving top down.
4
Push values, pop one, and print stack
Use push to add 10, 20, 30 to the stack. Then use pop once. Finally, print the stack contents from bottom (index 0) to top (index top) separated by spaces.
DSA C++
Hint
After pushing 10, 20, 30 and popping once, the stack should have 10 and 20 left. Print them from bottom to top.
Practice
(1/5)
1. What is the main purpose of using an array in stack implementation?
easy
A. To store elements in a fixed-size linear structure
B. To allow random access to elements anywhere
C. To sort elements automatically
D. To store elements in a tree-like structure
Solution
Step 1: Understand stack storage
A stack stores elements in a linear order, last-in first-out (LIFO).
Step 2: Role of array in stack
An array provides a fixed-size linear structure to hold stack elements efficiently.
Final Answer:
To store elements in a fixed-size linear structure -> Option A
Quick Check:
Stack uses array for fixed linear storage [OK]
Hint: Stack uses array for fixed linear storage [OK]
Common Mistakes:
Confusing stack with queue or tree
Thinking array sorts elements automatically
Assuming stack allows random access
2. Which of the following is the correct syntax to declare a stack array of size 10 in C++?
easy
A. stack int[10];
B. int stack[10];
C. int stack();
D. array stack[10];
Solution
Step 1: Recall C++ array declaration syntax
In C++, arrays are declared as: type name[size];
Step 2: Match with options
int stack[10]; matches correct syntax: int stack[10];
Final Answer:
int stack[10]; -> Option B
Quick Check:
C++ array declaration = int stack[10]; [OK]
Hint: Array declaration: type name[size]; [OK]
Common Mistakes:
Using parentheses instead of brackets
Wrong order of type and name
Using non-existent 'array' keyword
3. What will be the output after executing the following code?
int stack[5];
int top = -1;
// Push 10
stack[++top] = 10;
// Push 20
stack[++top] = 20;
// Pop
int x = stack[top--];
// Push 30
stack[++top] = 30;
// Print top element
std::cout << stack[top];