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.
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.
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.
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) | |
|---|---|---|
| Add | push (top) | enqueue (back) |
| Remove | pop (top) | dequeue (front) |
| Order out | reversed | same as in |
| Cost | O(1) | O(1) |
| Graph search | DFS | BFS |