AlgoThrive
← Home

Stacks & Queues

Two of the simplest data structures, and two opposite disciplines for removing what you added — watch LIFO and FIFO order play out operation by operation.

O(1) push / pop · O(n) space

Stack

A stack holds elements in Last-In-First-Out order: the most recently pushed element is always the first one popped. Both push (add to the top) and pop (remove from the top) touch only one end, so each operation runs in constant time no matter how many elements sit underneath. Call stacks, undo history, and backtracking all lean on this discipline.

Open visualizer

O(1) enqueue / dequeue · O(n) space

Queue

A queue holds elements in First-In-First-Out order: whichever element has waited longest is the next one out. Enqueue adds to the back, dequeue removes from the front — two different ends, so nothing needs to shift out of the way. Print spoolers, task schedulers, and breadth-first search all rely on this ordering.

Open visualizer