Stack & Queue
Easy+60 XPLIFO vs FIFO: a stack pops the newest, a queue dequeues the oldest.
No visualization loaded.
Watch
—
Press Run to begin.
Stacks and queues are two ways to hold a line of items where you only ever touch one end. A stack is a pile of plates: you add to the top and take from the top, so the LAST one in is the FIRST one out. A queue is a line at a shop: you join the back and leave from the front, so the FIRST one in is the FIRST one out. Same items, opposite order of removal.
▸What do LIFO and FIFO actually mean?
They're just the removal rule spelled out. LIFO = Last In, First Out (a stack — the newest item leaves first, like the top plate). FIFO = First In, First Out (a queue — the oldest item leaves first, like the person who's waited longest). If you remember 'plates vs line at a shop,' you've got it.
▸What do push/pop and enqueue/dequeue mean?
They're the add/remove words for each. Stack: push = put on top, pop = take off the top. Queue: enqueue = join the back, dequeue = leave from the front. Different names, but both are just 'add one' and 'remove one' from the allowed end.
▸Why restrict yourself to one end — isn't an array more flexible?
The restriction is the feature. By only touching one end, add and remove are both instant (O(1)) and the order is predictable, which is exactly what many algorithms need. Stacks power 'undo' and function calls; queues power task schedulers and BFS (coming later). The simplicity is what makes them reliable building blocks.
▸Which one do I reach for?
Ask: do I want to handle the newest thing first, or the oldest? Newest-first (like 'undo the last action') → stack. Oldest-first (like 'serve customers in order') → queue. The data's the same; you pick the rule that matches the job.
How the work grows
O(1) per push/pop is instant — doesn't grow at all. Because you only ever touch one end, adding or removing one item is instant no matter how many are stored — a flat line. Space grows with the number of items you're holding.
Faint dotted lines = O(1) (flat) and O(n) (straight) for comparison.