Stack

IsFull Operation

What is the "Is Full" Operation?

isFull tells you whether a stack has run out of room to accept another push. It only really matters for a fixed-capacity stack like an array-backed one; a linked-list stack can just keep allocating nodes, so it rarely needs this check.

How It Works

  • Returns true if the stack cannot accept more elements.
  • Returns false if the stack can accept more elements.
  • For dynamic stacks (no fixed size), this operation typically always returns false.
  • Often used with Push operations to prevent stack overflow.

Consider a stack with a maximum capacity of 3 elements. The dashed outlines are the slots still free — isFull() is just asking whether any are left:

size = 0 / 3emptyisFull() → false

Empty — two slots free, so a push is safe

size = 2 / 353topisFull() → false

Partly filled — one slot still free

size = 3 / 3735topisFull() → true

Every slot taken — the next push would overflow

Occupied slotFree slotReturns trueReturns false

Note that the check never looks at the values themselves — it only compares the size against the capacity, which is why it costs O(1) no matter how large the stack is. A dynamic stack has no fixed capacity to compare against, so its isFull() simply always returns false.

Time and Space Complexity

Here's the time and space complexity analysis for stack operations:

  • Fixed-size Stack:
    • Time Complexity: O(1)
    • Space Complexity: O(1)
  • Dynamic Stack:
    • Time Complexity: O(1)
    • Space Complexity: O(1)

Common Use Cases

  • Preventing stack overflow in memory-constrained systems.
  • Implementing bounded buffers or fixed-size caches.
  • Memory management in embedded systems.
  • Validating stack capacity before push operations

The Is Full operation is crucial when working with fixed-size stacks to prevent overflow errors. While not needed for dynamically-sized stacks, it's an essential safety check in many system-level implementations.

Visualize the IsFull operation on a fixed-capacity stack

Capacity: 0/5
Stack is empty

Test Your Knowledge before moving forward!

Stack Quiz Challenge

How it works:

  • +1 point for each correct answer
  • 0 points for wrong answers
  • -0.5 point penalty for viewing explanations
  • Earn stars based on your final score (max 5 stars)

Stack IsFull Implementation

// Stack Implementation with isFull Operation in JavaScript
class Stack {
  constructor(maxSize = 5) {
    this.items = [];
    this.top = -1;
    this.MAX_SIZE = maxSize;
  }

  // Push operation with isFull check
  push(element) {
    if (this.isFull()) {
      console.log("Stack Overflow - Cannot push to full stack");
      return;
    }
    this.items[++this.top] = element;
    console.log(`Pushed: ${element}`);
  }

  // Pop operation
  pop() {
    if (this.isEmpty()) {
      console.log("Stack Underflow - Cannot pop from empty stack");
      return -1;
    }
    return this.items[this.top--];
  }

  // Check if stack is full
  isFull() {
    const full = this.top === this.MAX_SIZE - 1;
    console.log(`Stack is ${full ? "full" : "not full"}`);
    return full;
  }

  // Check if stack is empty
  isEmpty() {
    return this.top === -1;
  }

  // Display stack
  display() {
    console.log("Current Stack:", this.items.slice(0, this.top + 1));
  }
}

// Usage
const stack = new Stack(3); // Small stack for demonstration

console.log("Initial checks:");
stack.isFull();  // false
stack.isEmpty(); // true

stack.push(10);
stack.push(20);
stack.push(30);
stack.display();
stack.isFull();  // true

// Try to push to full stack
stack.push(40);  // Will show overflow message

Done With the Learning

Mark Stack : isFull as done and view it on your dashboard

Explore other operations