Stack

Implementation Using Linked List

What is Stack Implementation Using Linked List?

Building a stack with a linked list instead of an array gets you the same LIFO (Last In, First Out) behavior, but without a fixed capacity: every push allocates a fresh node, so the stack can keep growing as long as memory allows.

Initialize

A top pointer is created and set to null, meaning there are no nodes yet. Some implementations also keep a size counter, initialized to 0, so size() doesn't need to walk the whole list.

topnull

push()

A new node is created pointing to whatever top currently points to, then top is repointed to the new node. Nothing else in the list is touched, which is why this runs in O(1).

top53null

↓ push(7) ↓

top753null

pop()

If top is null there's nothing to remove, so pop reports "Stack Underflow". Otherwise the data at top is saved, top is moved to point at the next node, and the saved data is returned. The old top node itself is left for garbage collection.

top753null

↓ pop() → returns 7 ↓

top53null

peek()

Returns the data at the top node without moving the top pointer, so the stack is left exactly as it was. If top is null, it returns null instead.

top753null

isEmpty() & size()

isEmpty() is just a null check on top. size() is O(1) if a counter is maintained on every push/pop, or O(n) if it has to walk the whole list counting nodes instead.

isEmpty() → true

topnull

Time Complexity

OperationComplexity
push()O(1)
pop()O(1)
peek()O(1)
isEmpty()O(1)
size()O(1) or O(n)

Key Characteristics

  • Dynamic Size: No fixed capacity (grows as needed)
  • Memory Efficiency: Uses only needed memory
  • No Wasted Space: Unlike array implementation
  • Extra Memory: Requires space for pointers
  • Flexibility: Can grow until memory exhausted

Linked List vs Array Implementation

FeatureLinked ListArray
Memory UsageExtra for pointersFixed size, may be wasted
Dynamic SizeYesNo (unless resized)
Memory AllocationDynamicStatic (usually)
Access TimeO(1) for topO(1) for all
Implementation ComplexitySlightly more complexSimpler

Stack Implementation using Linked-List

// Stack Implementation using Linked List (JavaScript)
class Node {
  constructor(value) {
    this.value = value;
    this.next = null;
  }
}

class LinkedListStack {
  constructor() {
    this.top = null;
    this.size = 0;
  }

  // Push operation
  push(value) {
    const newNode = new Node(value);
    newNode.next = this.top;
    this.top = newNode;
    this.size++;
  }

  // Pop operation
  pop() {
    if (this.isEmpty()) {
      console.log("Stack Underflow");
      return null;
    }
    const value = this.top.value;
    this.top = this.top.next;
    this.size--;
    return value;
  }

  // Peek operation
  peek() {
    if (this.isEmpty()) {
      console.log("Stack is empty");
      return null;
    }
    return this.top.value;
  }

  // Check if stack is empty
  isEmpty() {
    return this.size === 0;
  }

  // Get stack size
  getSize() {
    return this.size;
  }

  // Print stack contents
  print() {
    if (this.isEmpty()) {
      console.log("Stack is empty");
      return;
    }
    let current = this.top;
    console.log("Stack contents (top to bottom):");
    while (current) {
      console.log(current.value);
      current = current.next;
    }
  }
}

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

Done With the Learning

Mark Stack using Linked List as done and view it on your dashboard

Explore other implementation