Linear Data StructuresBeginner LevelStudy Time: 15 mins

Stack Data Structure

A Stack is a LIFO (Last-In, First-Out) data structure where elements are pushed and popped exclusively from the top pointer.

Loading visualizer workspace...

Stack LIFO Push & Pop Step-by-Step

Sample Input:Empty Stack (Capacity: 5), Operations: Push(10), Push(20), Pop()
Step 1

Push Initial Element (Push 10)

Advance top pointer from -1 to 0 and insert 10 at top of stack.

Top: 0 | Stack: [10]
Top pointer tracks the current active element index.
Step 2

Push Second Element (Push 20)

Advance top pointer from 0 to 1 and place 20 above 10.

Top: 1 | Stack: [10, 20] (Top element is 20)
Newly added items sit at top and will be popped first (LIFO).
Step 3

Pop Top Element (Pop)

Remove and return element at top pointer (20), then decrement top pointer.

Popped Value: 20 | Top: 0 | Stack: [10]
Stack pop returns the most recently pushed item in O(1) time.

Real-world analogy: Cafeteria Plate Stack

Push
Pop
Peek
Hover over items for details

Clean plates are stacked on top of each other. You can only place a new plate on the top (Push) or take the top plate off (Pop).

  • Push= Puts an item on top of the stack.
  • Pop= Removes the top item.
  • Peek= Inspects the top item without removing it.

Operations explained

Complexity analysis

Scenario Simulator: Dataset Size (N = 500)
Presets:
N = 10N = 10,000
Complexity Curve MapStack: O(1) Constant Time
Time (t)Size N (500)N = 500O(N²)O(N log N)O(N)O(log N)O(1)Stack Direct Ops
Time Steps (N=500)

1 step (Constant)

O(1) Constant Time

Space Footprint

O(N) Auxiliary Space

Memory growth rate as N expands.

Performance Rank
Optimal Tier

Relative efficiency rating for large N.

OperationTime ComplexitySpace Complexity
PushO(1)O(1)
PopO(1)O(1)
PeekO(1)O(1)

Code implementations

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

Real-world applications

Case 01

Browser History

Tracks back/forward navigation history.

Case 02

Function Call Stack

Tracks active compiler execution frames.

Case 03

Undo/Redo Actions

Stores action history in text editors.

Interview questions

Topic Knowledge Test & Assessment

10 Questions Assessment

Stack Data Structure Mastery Test

Evaluate your understanding of LIFO stack mechanics, push/pop operations, call stack frames, and bracket matching.