AlgoViz

Stack

easy 14 min

A stack is a linear data structure where items are added and removed at the same end, the top.

Where it shows up

The same pattern appears across everyday software.

  • Browser back and forward

    Each visited page is pushed onto a history stack and Back pops it. A second stack holds pages for Forward, and visiting a new page clears it.

  • Undo and redo

    Text editors (Word, VS Code), image editors (Photoshop, GIMP), spreadsheets (Excel, Google Sheets) and 3D tools (Blender, Maya) push each action onto an undo stack and move undone actions to a redo stack.

  • The function call stack

    Every function call pushes a frame with its parameters, local variables and return address; every return pops it. Stack traces print this stack when a program crashes, and recursion depends on it.

  • Expression evaluation and parsing

    Compilers and calculators use stacks to check balanced brackets, convert infix to postfix and evaluate expressions with operator precedence.

  • Depth-first search and backtracking

    Graph exploration, maze solving and puzzles such as N-Queens push a state before exploring it and pop to back up.

  • Spring-loaded plate dispensers

    The physical version: clean plates go on top and waiters take from the top, so the last plate in is the first out.