ITKokka

Stack Visualizer

See how a LIFO stack works step by step, with a live step log explaining each move.

30
← top
20
10

Size: 3 / 8

Current size: 3

Push

O(1)

Add a value to the top. Nothing else moves.

Step Log

$ Run an operation above to see step-by-step narration here.

Stack Operation Complexity

OperationTime ComplexityWhy
PushO(1)Adds to the top only, nothing else moves.
PopO(1)Removes the top value only, nothing else moves.
PeekO(1)Just reads the top value, no removal or shifting.
SearchO(n)May need to check every value from the top down.

How to Use

  1. 1

    Pick an operation tag (Push, Search, Pop, Peek, or Clear). Hover a tag to see its complexity and what it does.

  2. 2

    Enter a value where needed, then run it. Pop, Peek, and Clear run immediately since they need no input.

  3. 3

    Watch the top of the stack animate and read the step log below to see exactly what happened.

FAQ

What is a stack?

A stack is a LIFO (Last In, First Out) data structure: the last value pushed onto it is the first one popped off. Think of a stack of plates, you can only add or remove from the top.

Where are stacks used in real programs?

Undo history in editors, the browser's back button, and a program's own call stack (tracking which function to return to) are all stacks. Whenever "last action first undone" behavior is needed, a stack is the right structure.

Why is search O(n) if push/pop are O(1)?

Push and pop only ever touch the top of the stack, so they're O(1) no matter how many values are stored. Search has to check values one by one from the top down until it finds a match, so in the worst case it checks all n values.

Related

All Data StructuresJSON Visualizer