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.
stack = [] max_size = 3 pointer = -1 # Check empty is_empty = (pointer == -1) # Check full is_full = (pointer == max_size - 1)
| Step | Operation | Pointer Value | Condition Checked | Result | Stack Visual |
|---|---|---|---|---|---|
| 1 | Initial state | -1 | pointer == -1 | True (Empty) | [] |
| 2 | Push element 'A' | 0 | pointer == -1 | False | [A] |
| 3 | Check if full | 0 | pointer == max_size-1 (2) | False | [A] |
| 4 | Push element 'B' | 1 | pointer == max_size-1 (2) | False | [A, B] |
| 5 | Push element 'C' | 2 | pointer == max_size-1 (2) | True (Full) | [A, B, C] |
| 6 | Pop element | 1 | pointer == max_size-1 (2) | False | [A, B] |
| 7 | Pop element | 0 | pointer == -1 | False | [A] |
| 8 | Pop element | -1 | pointer == -1 | True (Empty) | [] |
| 9 | Check if full | -1 | pointer == max_size-1 (2) | False | [] |
| Variable | Start | After Step 2 | After Step 4 | After Step 5 | After Step 6 | After Step 7 | After Step 8 | Final |
|---|---|---|---|---|---|---|---|---|
| pointer | -1 | 0 | 1 | 2 | 1 | 0 | -1 | -1 |
| stack | [] | [A] | [A, B] | [A, B, C] | [A, B] | [A] | [] | [] |
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.
isEmpty() function check in a stack?isEmpty()isEmpty() function checks whether the stack contains any elements or not.isEmpty() returns true if no elements are present.stack is full in Python assuming max size is stored in max_size and current size in top?top == max_size to confirm fullness.class Stack:
def __init__(self, max_size):
self.stack = []
self.max_size = max_size
def isFull(self):
return len(self.stack) == self.max_size
def isEmpty(self):
return len(self.stack) == 0
s = Stack(2)
s.stack.append(10)
s.stack.append(20)
print(s.isFull())
print(s.isEmpty())isFull() returns True because length equals max_size; isEmpty() returns False because stack is not empty.class Stack:
def __init__(self):
self.stack = []
def isEmpty(self):
if self.stack == 0:
return True
else:
return False
s = Stack()
print(s.isEmpty())self.stack == 0, but self.stack is a list, not a number.len(self.stack) == 0 or simply not self.stack.isFull() and isEmpty() methods?class Stack:
def __init__(self, max_size):
self.stack = []
self.max_size = max_size
def push(self, val):
if len(self.stack) < self.max_size:
self.stack.append(val)
def pop(self):
if not self.isEmpty():
return self.stack.pop()
def isFull(self):
return len(self.stack) == self.max_size
def isEmpty(self):
return len(self.stack) == 0
s = Stack(3)
s.push(1)
s.push(2)
s.push(3)
s.pop()
print(s.isFull())
print(s.isEmpty())isFull() returns False. Stack is not empty, so isEmpty() returns False.Pass the test and this pattern moves unknown