Simulation

Stack Data Structure Visualizer

Visualize PUSH, POP and PEEK using a vertical stack, TOP pointer, indexes, placeholders, highlighted pseudocode, and simple animations.

Stack Visualization

The stack grows upward. All insertion and deletion happen at the TOP.

Current Code

Current line is highlighted.

Stack rule:
    PUSH adds to TOP
    POP removes from TOP
    PEEK reads TOP only

Operations

[5]
empty
← TOP
[4]
empty
← TOP
[3]
empty
← TOP
[2]
empty
← TOP
[1]
empty
← TOP
[0]
empty
← TOP

Bottom

Maximum Size

Zoom

100%

Current State

Size

0

TOP

-1

Value

Stack is ready. Choose PUSH, POP, or PEEK.

Limited mode shows empty placeholder slots. Unlimited mode expands as values are pushed. Use the stack size slider if the stack becomes too tall.

Stack Notes

A stack is a linear data structure where insertion and deletion happen only from one end, called the TOP.

Core Idea

  • A stack stores items in a linear order.
  • It follows LIFO: Last In, First Out.
  • The last value inserted is the first value removed.
  • Only the top item can be accessed directly.
  • The top of the stack is the active end.
  • A stack of plates is a common real-life example.

TOP Pointer

  • TOP stores the index of the current top item.
  • When the stack is empty, TOP is usually -1.
  • After a successful PUSH, TOP increases by 1.
  • After a successful POP, TOP decreases by 1.
  • PEEK reads stack[TOP] without changing TOP.
  • In this visualizer, TOP is shown beside the top item.

Stack as an Array

  • The first inserted value is stored at index 0.
  • The next value is stored at index 1, and so on.
  • TOP points to the highest filled index.
  • Empty slots represent unused array positions.
  • In limited mode, the array has a fixed maximum size.
  • In unlimited mode, the visual stack expands as values are added.

Stack Operations

The three main stack operations are PUSH, POP and PEEK. Each operation works with the TOP of the stack.

PUSH

  1. Check whether the stack is full.
  2. If full, output stack overflow.
  3. If not full, increase TOP by 1.
  4. Store the new value at stack[TOP].
  5. The inserted value becomes the new top item.

POP

  1. Check whether the stack is empty.
  2. If empty, output stack underflow.
  3. If not empty, read stack[TOP].
  4. Decrease TOP by 1.
  5. The previous top item is removed from the stack.

PEEK

  1. Check whether the stack is empty.
  2. If empty, there is no value to return.
  3. If not empty, read stack[TOP].
  4. Return the top value.
  5. Do not remove anything from the stack.

Errors and Boundary Cases

Stack algorithms must check for invalid operations before changing TOP.

Overflow

  • Overflow happens when PUSH is attempted on a full stack.
  • It only applies when the stack has a fixed maximum size.
  • The new value should not be inserted.
  • TOP should not change.

Underflow

  • Underflow happens when POP is attempted on an empty stack.
  • There is no top item to remove.
  • TOP remains -1.
  • The stack stays empty.

Empty Stack

  • An empty stack has no stored items.
  • TOP is normally represented as -1.
  • POP is not possible.
  • PEEK is not possible.

Full Stack

  • A limited stack is full when size equals maximum size.
  • For an array of size n, the last valid index is n - 1.
  • When full, TOP is at the last valid index.
  • Another PUSH would cause overflow.

Exam and Programming Notes

These points help when writing pseudocode, explaining algorithms, or answering exam-style questions.

Time Complexity

  • PUSH is O(1).
  • POP is O(1).
  • PEEK is O(1).
  • No shifting is needed.
  • Only TOP changes during PUSH and POP.

Common Uses

  • Undo and redo systems.
  • Browser back button history.
  • Function call stack.
  • Expression evaluation.
  • Bracket matching.
  • Depth-first search.

Pseudocode Tips

  • Always check overflow before PUSH in a fixed stack.
  • Always check underflow before POP.
  • Use TOP to access the current top item.
  • Update TOP after checking the stack condition.
  • Do not change TOP during PEEK.