A Stacks & Queues
05 — Data Structures
LIFO vs FIFO — same idea, opposite order

Stacks & Queues

A stack hands back the last thing you added. A queue hands back the first. Both do it in O(1) — the structure keeps the order for you.

Stacks and queues are not new memory layouts — they are rules about which end you may touch. A stack enforces LIFO: Last In, First Out. You add (push) and remove (pop) only from the top, like a pile of plates. A queue enforces FIFO: First In, First Out. Items join at the back (enqueue) and leave from the front (dequeue), like a checkout line. Every operation is O(1) — the structure guarantees the ordering automatically.

Try it in 3D

On the left, the stack grows upward (LIFO). On the right, the queue runs left to right (FIFO). Drag to orbit, scroll to zoom, then press an operation.

Drag to rotate — scroll to zoom
Press an operation above.ready
Push / Enqueue Pop / Dequeue Peek (top only)

The operations

Every single operation is O(1) — that is the whole point of restricting access to one end.

Cost cheat sheet

All operations constant time. The order items leave is the only difference.