Stacks

Lesson 11 of 20

Push, pop and peek your way to LIFO mastery — then watch the call stack juggle function frames in 3D.

The big picture

Meet the stack

A stack is a pile where only the top is ever touched. Whatever went in last comes out first — that rule powers everything from Ctrl+Z to function calls.

LIFO rule

Last In, First Out. Push 12 then 7, and the first pop hands you back 7 — the newest value always leaves first.

Three O(1) moves

push places a value on top, pop removes it, peek just reads it. None of them ever shift the other elements.

Where you meet it

Undo history, the browser back button, matching brackets and the function call stack are all stacks doing their thing.

Two homes

An array with a top index or a linked list with a head pointer — both implement the same stack interface.

Overflow is literal

Push onto a full stack and you get the error that gave this website its name: stack overflow.