0
0
DSA Pythonprogramming~10 mins

Check if Stack is Empty or Full in DSA Python - Execution Trace

Choose your learning style9 modes available
Concept Flow - Check if Stack is Empty or Full
Start
Check if stack pointer == -1?
Stack is Empty
Stack is Full
End
We check the stack pointer to see if it is -1 (empty) or max_size-1 (full) to determine stack state.
Execution Sample
DSA Python
stack = []
max_size = 3
pointer = -1

# Check empty
is_empty = (pointer == -1)

# Check full
is_full = (pointer == max_size - 1)
This code checks if the stack is empty or full by comparing the pointer with -1 and max_size-1.
Execution Table
StepOperationPointer ValueCondition CheckedResultStack Visual
1Initial state-1pointer == -1True (Empty)[]
2Push element 'A'0pointer == -1False[A]
3Check if full0pointer == max_size-1 (2)False[A]
4Push element 'B'1pointer == max_size-1 (2)False[A, B]
5Push element 'C'2pointer == max_size-1 (2)True (Full)[A, B, C]
6Pop element1pointer == max_size-1 (2)False[A, B]
7Pop element0pointer == -1False[A]
8Pop element-1pointer == -1True (Empty)[]
9Check if full-1pointer == max_size-1 (2)False[]
💡 Operations stop after stack pointer returns to -1 (empty) and no more pushes are done.
Variable Tracker
VariableStartAfter Step 2After Step 4After Step 5After Step 6After Step 7After Step 8Final
pointer-101210-1-1
stack[][A][A, B][A, B, C][A, B][A][][]
Key Moments - 3 Insights
Why does the pointer start at -1 instead of 0?
Pointer at -1 means the stack is empty before any push. See Step 1 in execution_table where pointer == -1 means empty stack.
Why do we check pointer == max_size - 1 to know if stack is full?
Because pointer starts at -1 and increments with each push, max index is max_size - 1. Step 5 shows pointer == 2 (max_size-1) means full.
What happens if we try to push when stack is full?
Pointer won't increase beyond max_size - 1, so push should be blocked. Step 5 shows stack full at pointer 2, no further push allowed.
Visual Quiz - 3 Questions
Test your understanding
Look at the execution_table, what is the pointer value after pushing element 'B'?
A0
B1
C2
D-1
💡 Hint
Check Step 4 in execution_table where 'Push element B' updates pointer.
At which step does the stack become full according to the execution_table?
AStep 3
BStep 4
CStep 5
DStep 6
💡 Hint
Look for 'True (Full)' result in the Result column.
If the pointer was initialized to 0 instead of -1, what would change in the empty check?
AEmpty check would be pointer == 0
BEmpty check would be pointer == -1
CEmpty check would be pointer == max_size - 1
DEmpty check would not be needed
💡 Hint
Refer to variable_tracker and concept_flow about pointer initialization and empty condition.
Concept Snapshot
Stack pointer starts at -1 meaning empty stack.
Push increments pointer, pop decrements.
Empty if pointer == -1.
Full if pointer == max_size - 1.
Check these conditions to know stack state.
Prevent push if full, pop if empty.
Full Transcript
This concept shows how to check if a stack is empty or full by using a pointer that tracks the top element index. The pointer starts at -1 when the stack is empty. When we push elements, the pointer increases by one each time. When the pointer reaches max_size - 1, the stack is full. We check if the pointer equals -1 to know if the stack is empty, and if it equals max_size - 1 to know if it is full. The execution table traces these pointer changes and stack states step by step, showing pushes and pops. Key moments clarify why pointer starts at -1 and why full check uses max_size - 1. The visual quiz tests understanding of pointer values and conditions. This helps beginners see how stack fullness and emptiness are tracked simply by pointer comparisons.