0
0
DSA Pythonprogramming

Check if Stack is Empty or Full in DSA Python

Choose your learning style9 modes available
Mental Model
A stack is like a pile where you add or remove items only from the top. Checking if it is empty means no items are there, and checking if it is full means no more items can fit.
Analogy: Imagine a stack of plates in a cupboard. If there are no plates, the stack is empty. If the cupboard is full and can't hold more plates, the stack is full.
Top -> [ ] ← Bottom (empty stack)
Top -> [5] -> [3] -> [1] ← Bottom (partially filled stack)
Top -> [9] -> [8] -> [7] -> [6] -> [5] ← Bottom (full stack)
Dry Run Walkthrough
Input: stack capacity 3, current stack elements: [5, 3], check empty and full
Goal: Determine if the stack is empty or full at this moment
Step 1: Check if stack size is 0 to see if empty
Stack: 5 -> 3 -> null, size=2, capacity=3
Why: If size is 0, stack is empty; here size is 2, so not empty
Step 2: Check if stack size equals capacity to see if full
Stack: 5 -> 3 -> null, size=2, capacity=3
Why: If size equals capacity, stack is full; here size is 2, capacity 3, so not full
Result:
Stack: 5 -> 3 -> null
Empty? False
Full? False
Annotated Code
DSA Python
class Stack:
    def __init__(self, capacity):
        self.capacity = capacity
        self.items = []

    def is_empty(self):
        return len(self.items) == 0

    def is_full(self):
        return len(self.items) == self.capacity

    def push(self, item):
        if self.is_full():
            print("Stack is full, cannot push")
            return
        self.items.append(item)

    def __str__(self):
        return ' -> '.join(map(str, reversed(self.items))) + ' -> null'

# Driver code
stack = Stack(3)
stack.push(5)
stack.push(3)
print(stack)
print("Empty?", stack.is_empty())
print("Full?", stack.is_full())
return len(self.items) == 0
Check if stack has zero elements to determine emptiness
return len(self.items) == self.capacity
Check if stack size reached capacity to determine fullness
if self.is_full():
Prevent pushing if stack is already full
self.items.append(item)
Add new item to top of stack
OutputSuccess
5 -> 3 -> null Empty? False Full? False
Complexity Analysis
Time: O(1) because checking length and comparing is a constant time operation
Space: O(n) where n is capacity, because stack stores up to n items
vs Alternative: Using a linked list stack would also have O(1) checks for empty but no fixed capacity, so no full check
Edge Cases
Empty stack
is_empty returns True, is_full returns False
DSA Python
return len(self.items) == 0
Stack with one element
is_empty returns False, is_full depends on capacity
DSA Python
return len(self.items) == self.capacity
Stack at full capacity
is_full returns True, push is blocked
DSA Python
if self.is_full():
When to Use This Pattern
When you need to control adding or removing items safely in a fixed-size container, check if the stack is empty or full to avoid errors.
Common Mistakes
Mistake: Checking if stack is empty by comparing to None instead of length zero
Fix: Use len(self.items) == 0 to check emptiness
Mistake: Not checking if stack is full before pushing, causing overflow
Fix: Always check is_full() before push to prevent overflow
Summary
Checks if a stack has no items or is at its maximum capacity.
Use when managing a stack with limited size to avoid errors.
Remember: empty means zero items, full means size equals capacity.