What is a stack?

Push, pop, and why the last thing in is the first thing out, the data structure behind undo and call stacks.

A stack is a pile with one rule: you only touch the top. Push puts a value on top; pop takes the top one off. Last in, first out, like plates in a cafeteria.

That one rule turns out to run your programs: every function call is pushed onto the call stack and popped when it returns. Undo history, back buttons, and expression parsing are all stacks wearing different clothes.

Remember this

  • Only the top is touchable: push adds, pop removes
  • LIFO, last in, first out
  • Function calls, undo, and back buttons are all stacks

Got a different question? SeaThru generates a fresh video for any topic where systems talk or data structures move.

Ask your own question →

Keep learning