Last in. First out.
One way in, one way out. Add a value to the top, then watch the newest item leave first.
THE STACK
Push · Pop · PeekOnly the top is accessible.
↓ PUSH ↑ POP
∅Your stack is empty
Push a value to create the first item.
BOTTOM OF STACK
Ready
Push a value or play the example to see LIFO in action.
SIZE0 items
TOP VALUE—
TOP INDEX—
STATEEmpty
WHY A STACK?
Think of a pile of plates.
You place a new plate on top and take that plate off first. A stack follows the same rule: last in, first out.
Push(value)Add a value above the current top.
Pop()Remove and return the top value.
Peek()Read the top value; leave every item in place.
Used in undo history, function calls, and expression parsing. Pop and peek are O(1); array-backed push is amortized O(1). Index 0 is the bottom.