Skip to content

Stack Visualizer

Push and pop values on a stack, last in first out, as an array with a top index or as linked nodes.

Implementation
Popped values
Size: 0 Capacity: 8 Top: −1

A stack is last in, first out (LIFO): you can only add or remove at the top, like a pile of plates.

Step 1 of 1

// Array stack with capacity N (top = −1 when empty)push(x):  if top = N − 1: error "stack overflow"  top = top + 1; a[top] = xpop():  if top = −1: error "stack underflow"  x = a[top]; top = top − 1; return xpeek(): return a[top] // Linked stack (top = null when empty)push(x):  node = new Node(x); node.next = top  top = nodepop():  if top = null: error "stack underflow"  x = top.value; top = top.next; return xpeek(): return top.value
Stack operations
OperationArray stackLinked stack
PushO(1)O(1)
PopO(1)O(1)
PeekO(1)O(1)
SearchO(n)O(n)
Size limitFixed capacity (or resize)Only memory

A stack is a last in, first out (LIFO) collection: you add (push) and remove (pop) only at the top, just like a pile of plates. This visualizer shows a stack two ways. The array version is a fixed block of 8 slots with a top index, so you can see what stack overflow means when you push onto a full stack. The linked version stores each value in a node at its own memory address, pointing to the node below it.

Push, pop and peek are animated one step at a time with the matching pseudocode line highlighted, and popped values collect on the side so you can see that they come out in reverse order. All three operations take O(1) time in both versions.

How to use the Stack Visualizer tool

  1. Choose Array or Linked list.
  2. Type a value and press Push. Press Pop to remove the top value or Peek to look at it.
  3. Keep pushing onto the array stack to trigger an overflow, or pop an empty stack to see an underflow.
  4. Use Step and Back to follow each operation and read the pseudocode.

Why use ToolOrca Stack Visualizer

  • Array stack with capacity, top index and overflow
  • Linked stack with node addresses and next pointers
  • Push, pop and peek animated
  • Popped values shown in the order they came out
  • Underflow and overflow messages
  • Code in C++, Java, Python and JavaScript

How a stack works

A stack supports three main operations: push(x) adds x on top, pop() removes and returns the top value, and peek() (or top) returns the top value without removing it. The last value pushed is always the first one popped, which is called LIFO.

In an array stack a variable top holds the index of the top element (−1 when empty). Push does top = top + 1 and stores the value; pop reads a[top] and decrements top. If the array is full, a push causes a stack overflow; popping an empty stack is a stack underflow. A linked stack pushes by creating a node that points to the old top, so it only runs out when memory does.

Where stacks are used

Stacks are everywhere in computing. The call stack stores each function call’s local variables and return address, which is where the error name “stack overflow” comes from in deep recursion. Stacks also implement undo, the browser back button, checking balanced brackets, evaluating expressions in reverse Polish notation, converting infix to postfix, and depth-first search.

Every visualizer on this site runs in your browser with the same controls: Play, Pause, Step forward, Step back and a speed slider. You can also drag the position slider to jump to any step, or focus the drawing and use the Space bar and arrow keys.

Frequently asked questions

What does LIFO mean?

Last in, first out: the most recently added item is the first one removed, like taking the top plate off a pile.

What is the time complexity of push and pop?

Both are O(1) for array and linked stacks. A dynamic array stack occasionally needs to grow, but push is still O(1) amortized.

What is the difference between stack overflow and underflow?

Overflow is pushing onto a stack that is already full. Underflow is popping or peeking at a stack that is empty.

Should I use an array or a linked list for a stack?

Usually an array (or dynamic array): it uses less memory and is faster because the values sit together in memory. A linked stack never needs resizing, which can matter in some low-level code.

How do I use a stack in Python, Java or JavaScript?

Python: a list with append() and pop(). Java: ArrayDeque with push(), pop() and peek(). JavaScript: an array with push() and pop(). C++: std::stack.