StackEasy

Is Empty / Is Full

Stack IsEmpty & IsFull Operations

IsEmpty & IsFull Operations

These boundary check operations are vital to call before inserting or deleting to prevent memory access violations.

1. IsEmpty()

Checks if stack has zero elements. In array implementation, this means checking if the top index is -1.

  • Underflow prevention: Always check isEmpty() before executing a pop or peek operation.

2. IsFull()

Checks if the stack has reached its allocated size capacity.

  • Overflow prevention: Always check isFull() before executing a push operation.

Visual Representation

  Empty Stack (top = -1):   Full Stack (top = 4):
  [   ]                      Index [4]: [ 50 ] ◄top
  [   ]                      Index [3]: [ 40 ]
  [   ]                      Index [2]: [ 30 ]
  [   ]                      Index [1]: [ 20 ]
  [   ] ◄top = -1            Index [0]: [ 10 ]