Kazi Rahamatullah
Kazi Rahamatullah
AboutProjectsBlogContact
UsesBooks
ResumeView CV
Kazi Rahamatullah

© Copyright 2026 Kazi Rahamatullah

AboutProjectsBlogBooksUses
Twitter/XGitHubProduct HuntCodeSandbox
Back to Blog
DSAStacksData StructuresLIFO

DSA Stacks — LIFO Fundamentals Visualized

Learn stack fundamentals — LIFO push/pop/peek, O(1) operations, interactive step visualizer, code breakdown, and real-world undo/call-stack examples.

Jul 4, 20263 min read

Introduction

Stacks are LIFO (Last In, First Out) structures — the last element pushed is the first one popped. Think of a stack of plates: you add and remove only from the top.

Built on arrays or linked lists, stacks power undo systems, call stacks, DFS, and bracket matching.

Quick index

#Section
1How stacks work
2Interactive visualizer
3Full implementation
4Complexity & use cases
5Real-life examples

1. How stacks work

plaintext
        top → [30]
              [20]
              [10]
           bottom
OperationActionResult
pushAdd to topO(1)
popRemove from topO(1)
peekRead top, no removeO(1)
isEmptyCheck if emptyO(1)

Back to index


2. Interactive visualizer

Stack — Fundamentals

LIFO structure — push and pop from the top only. Foundation for undo, call stacks, and DFS.

top ↓
empty
bottom

Stack starts empty — only top element is accessible.

Stack class

class Stack {
  constructor() { this.items = [] }
  push(val) { this.items.push(val) }
  pop() { return this.items.pop() }
  peek() { return this.items[this.items.length - 1] }
  isEmpty() { return this.items.length === 0 }
}

LIFO — Last In, First Out.

push / pop

O(1)

peek

O(1)

Order

LIFO

Tip

Only the top element is accessible — no random access to middle elements unlike arrays.

Back to index


3. Full implementation

js
class Stack {
  constructor() {
    this.items = []
  }
  push(val) {
    this.items.push(val)
  }
  pop() {
    return this.items.pop()
  }
  peek() {
    return this.items[this.items.length - 1]
  }
  isEmpty() {
    return this.items.length === 0
  }
}

Function calls with output

js
// ── Demo calls — output shown on the right ──
const s = new Stack()
 
s.push(10) // → [10]
s.push(20) // → [10, 20]
s.push(30) // → [10, 20, 30]
s.peek() // → 30
s.pop() // → 30
s.pop() // → 20
s.isEmpty() // → false

Back to index


4. Complexity & use cases

MetricValue
pushO(1)
popO(1)
peekO(1)
searchO(n)
Use caseWhy stack
Undo/redoReverse last action
Call stackFunction returns in reverse call order
DFS traversalTrack path, backtrack by popping
Valid parenthesesMatch opening/closing brackets

Interview Answer

"Stack vs queue?"

Stack = LIFO (last in, first out) — undo, DFS. Queue = FIFO (first in, first out) — BFS, scheduling. Different access ends, different problems.

Back to index


5. Real-life examples

ScenarioStack role
Ctrl+Z undoEach edit pushed; undo pops last action
Browser back stackPages pushed on navigate; back pops
Function call stackEach call pushed; return pops frame
Syntax parsingOpening tokens push; closing tokens pop and match

Browser undo stack

Ctrl+Z in editors — each edit pushes to stack; undo pops the last action.

Document: "Hello"

undoStack: [type "Hello"]

First edit pushed onto undo stack.

Real-world implementation

class UndoStack {
  constructor() { this.actions = [] }
  push(action) { this.actions.push(action) }
  undo() { return this.actions.pop() ?? null }
  canUndo() { return this.actions.length > 0 }
}

Full working code for the scenario above.

Function calls with output

// ── Function calls with output ──
const undo = new UndoStack()
undo.push({ type: "insert", text: "Hello" })→ stack depth 1

Side output shows return value after each call — step through to trace execution.

Back to index


Summary

Stacks give O(1) push/pop at one end with LIFO ordering — essential for reversal, nesting, and backtracking.

Next reads: Stack Types · Stack Applications · DSA Queues

Share this article

XLinkedInFacebook
Kazi Rahamatullah

Written by

Kazi Rahamatullah

FullStack Developer

X / TwitterGitHubLinkedIn

Subscribe to my newsletter

Stay up to date and get notified when I share new contents.

No spam ever, unsubscribe anytime