What if your program crashes just because it tried to add to a full stack or remove from an empty one?
Why Check if Stack is Empty or Full in DSA C++?
Imagine you have a stack of plates at home. You want to add a new plate or take one off, but you need to know if the stack is empty or already full to avoid dropping or breaking plates.
Without checking, you might try to take a plate from an empty stack and end up with nothing or try to add a plate to a full stack and cause a mess. Manually keeping track is tiring and easy to forget.
By having simple checks to see if the stack is empty or full, you can safely add or remove plates without mistakes. These checks act like safety signals that prevent errors.
if (count == 0) { // no plates to remove } if (count == max) { // no space to add }
bool isEmpty() { return top == -1; }
bool isFull() { return top == max - 1; }This lets you manage your stack safely and efficiently, avoiding errors and crashes.
In a web browser, the back button uses a stack to remember pages. Checking if the stack is empty prevents errors when there is no page to go back to.
Manual tracking of stack state is error-prone.
Checking empty/full states prevents mistakes.
These checks make stack operations safe and reliable.
Practice
isEmpty() function check in a stack data structure?Solution
Step 1: Understand the purpose of
TheisEmpty()isEmpty()function is designed to check whether the stack contains any elements or not.Step 2: Interpret the meaning of empty stack
An empty stack means it has zero elements, soisEmpty()returns true if no elements are present.Final Answer:
If the stack has no elements -> Option AQuick Check:
isEmpty()= stack empty check [OK]
- Confusing isEmpty() with isFull()
- Thinking isEmpty() checks for one element
- Assuming isEmpty() checks stack order
s is full in C++ assuming MAX is the stack capacity and top is the index of the top element?Solution
Step 1: Understand stack indexing
Stack top starts at -1 when empty and increments by 1 for each push. The last valid index isMAX - 1.Step 2: Check condition for full stack
The stack is full whentopequalsMAX - 1, meaning all positions are filled.Final Answer:
if (top == MAX - 1) {} -> Option AQuick Check:
Full stack = top == MAX - 1 [OK]
- Using top == MAX instead of MAX - 1
- Checking top > MAX which is invalid
- Using >= MAX + 1 which is out of range
const int MAX = 3; int stack[MAX]; int top = -1; // Push elements if (top < MAX - 1) top++; stack[top] = 10; if (top < MAX - 1) top++; stack[top] = 20; // Check if stack is full if (top == MAX - 1) std::cout << "Full"; else std::cout << "Not Full";
Solution
Step 1: Trace the push operations
Initially,top = -1. First push increments top to 0 and stores 10. Second push increments top to 1 and stores 20.Step 2: Check if stack is full
Stack is full iftop == MAX - 1which is 2. Currently, top is 1, so stack is not full.Final Answer:
Not Full -> Option DQuick Check:
top = 1, MAX - 1 = 2, so Not Full [OK]
- Assuming stack is full after 2 pushes when MAX is 3
- Confusing top index with number of elements
- Ignoring initial top = -1
bool isEmpty(int top) {
if (top == 0)
return true;
else
return false;
}Solution
Step 1: Understand stack top initialization
In stack implementation,topis usually -1 when stack is empty, not 0.Step 2: Analyze the condition in code
The code checkstop == 0which means stack has one element, so it incorrectly returns true for empty.Final Answer:
The function should check if top == -1 for empty stack -> Option BQuick Check:
Empty stack top = -1, not 0 [OK]
- Checking top == 0 instead of -1
- Confusing empty with one element
- Returning wrong boolean for empty condition
top initialized to -1. After pushing 5 elements, you pop 2 elements. Which condition correctly checks if the stack is full now?Solution
Step 1: Calculate top after operations
Initially, top = -1. After pushing 5 elements, top = 4 (0-based index). After popping 2 elements, top decreases by 2 to 2.Step 2: Determine full stack condition now
Stack is full only when top = 4. After popping, top is 2, so stack is not full. To check if full now, top must be 4.Step 3: Understand the question's intent
The question asks which condition correctly checks if stack is full now. Since top is 2, stack is not full. The correct condition to check full istop == 4.Final Answer:
if (top == 4) {} -> Option CQuick Check:
Full stack top = capacity - 1 = 4 [OK]
- Using top == 5 which is out of range
- Confusing popped elements with full condition
- Checking top == -1 which means empty
Ready to prove it?
Pass the test and this pattern moves unknown
