← All animations

๐Ÿฅž Stacks & Queues

Two of the simplest, most useful containers in all of computing โ€” and they differ by a single rule: which end you take from. A stack is last-in, first-out (LIFO) โ€” like a pile of pancakes, you take from the top. A queue is first-in, first-out (FIFO) โ€” like a line at a counter, you serve the front. Feed both the same items and watch one come out reversed and the other come out in order.

1. LIFO vs FIFO โ€” feed them the same items

Press Push / Enqueue to add the next lettered item to each. Then remove them: the stack pops from the top (so it reverses), while the queue dequeues from the front (so it keeps order). Peek looks without removing; removing from an empty container is an underflow and is refused.

Both empty โ€” add some items.

2. Where they show up โ€” undo & the print spool

A stack is exactly how Undo works: every action is pushed on top, and Undo pops the most recent one (Redo pushes it back). The same shape is your program's call stack โ€” calling a function pushes a frame, returning pops it. A queue is a fair waiting line: a print spool or task list runs jobs in the order they arrived. Below, the queue also wraps around a circular buffer โ€” the same array reused as a ring.

Stack โ€” text editor Undo / Redo

Queue โ€” print spool (circular buffer)

3. In one screen

Both are restricted lists where you only ever touch the ends โ€” which is exactly why every core operation is O(1): no searching, no shuffling, just a pointer to one (stack) or both (queue) ends.

Stack (LIFO)Queue (FIFO)
Addpush (top)enqueue (back)
Removepop (top)dequeue (front)
Order outreversedsame as in
CostO(1)O(1)
Graph searchDFSBFS