Stack

Implementation Using Array

What is Stack Implementation Using Array?

A stack follows LIFO (Last In, First Out), meaning whatever you pushed most recently is the first thing that comes back out. Backing it with an array is the most direct way to build one, since push and pop just work on the array's last index in constant time.

Initialize

An empty array is allocated with a fixed capacity, and the top pointer starts at -1 to signal there's nothing on the stack yet.

top = -1012345

push()

If the array isn't already at capacity, the top pointer is incremented first, then the new value is written at that index, so top always marks the most recently added element.

top50312345

↓ push(7) ↓

top503172345

pop()

The element at array[top] is read and returned, then the top pointer is decremented; the value itself is left in the array, just no longer considered part of the stack.

top503172345

↓ pop() → returns 7 ↓

top50312345

peek()

Returns array[top] without touching the pointer, so the stack is left exactly as it was, useful for checking what's on top before deciding whether to pop.

top503172345

isEmpty() & isFull()

Both are just pointer comparisons: isEmpty() is true when top equals -1, and isFull() is true when top reaches the array's last valid index.

isEmpty() → true

top = -1012345

isFull() → true

top503172239415

Time Complexity

OperationComplexity
push()O(1)
pop()O(1)
peek()O(1)
isEmpty()O(1)

Key Characteristics

  • LIFO Principle: Last element added is first removed
  • Dynamic Size: Can grow until memory limits
  • Efficiency: All operations work in constant time
  • Versatility: Foundation for many algorithms

Stack Implementation using Array

// Stack Implementation using Array (JavaScript)
class Stack {
  constructor(size = 10) {
    this.items = new Array(size);
    this.top = -1;
    this.capacity = size;
  }

  // Push operation
  push(element) {
    if (this.isFull()) {
      console.log("Stack Overflow");
      return;
    }
    this.items[++this.top] = element;
  }

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

  // Peek operation
  peek() {
    if (this.isEmpty()) {
      console.log("Stack is empty");
      return undefined;
    }
    return this.items[this.top];
  }

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

  // Check if stack is full
  isFull() {
    return this.top === this.capacity - 1;
  }

  // Get stack size
  size() {
    return this.top + 1;
  }

  // Print stack contents
  print() {
    if (this.isEmpty()) {
      console.log("Stack is empty");
      return;
    }
    console.log("Stack contents:");
    for (let i = this.top; i >= 0; i--) {
      console.log(this.items[i]);
    }
  }
}

// Usage
const stack = new Stack(5);
stack.push(10);
stack.push(20);
stack.push(30);
console.log("Top element:", stack.peek()); // 30
console.log("Stack size:", stack.size());  // 3
stack.print();
stack.pop();
console.log("After pop, top element:", stack.peek()); // 20

Done With the Learning

Mark Stack using Array as done and view it on your dashboard

Explore other implementation