๐Ÿ“š Full Lesson ยท Data Structures
Stack: LIFO (Last In, First Out) | Queue: FIFO (First In, First Out)
Stack vs Queue

Two of the simplest data structures in computer science, distinguished by exactly one rule: which end you remove from. That single difference shapes everything from undo buttons to print queues.

The Core Idea
Same Shape, Opposite Removal Rule

Both stacks and queues are linear collections where you add items one at a time and remove items one at a time โ€” the entire difference between them is which end you're allowed to remove from. A stack is LIFO โ€” Last In, First Out โ€” you always remove the item that was added most recently. A queue is FIFO โ€” First In, First Out โ€” you always remove the item that was added earliest.

Both structures restrict access on purpose: unlike an array, you can't just grab any element you want โ€” you can only interact with one designated end (stack) or two designated ends (queue: one for adding, one for removing). This restriction is exactly what makes them fast and predictable for the specific patterns they're built for.

๐Ÿ’ก Memory Trick
A STACK is a stack of plates: you can only take the top plate off (the one placed most recently), and you can only add a new plate to the top. A QUEUE is a line at a coffee shop: whoever joined the line first gets served first, and new people join at the back โ€” never cutting to the front.
The Operations
Push, Pop, Enqueue, Dequeue
1
Stack: push()
Adds a new element to the top of the stack โ€” the only place elements can be added.
Example: push(5) on a stack containing [3, 7] results in [3, 7, 5], with 5 now on top.
2
Stack: pop()
Removes and returns the element currently on top of the stack โ€” always the most recently added one still present.
Example: pop() on [3, 7, 5] removes and returns 5, leaving [3, 7].
3
Queue: enqueue()
Adds a new element to the back of the queue โ€” the only place elements can be added.
Example: enqueue(5) on a queue containing [3, 7] (front to back) results in [3, 7, 5].
4
Queue: dequeue()
Removes and returns the element currently at the front of the queue โ€” always the earliest-added one still present.
Example: dequeue() on [3, 7, 5] (front to back) removes and returns 3, leaving [7, 5].
Where Each Shows Up
Matching Structure to Real Problems

Stacks naturally model any 'undo' or 'backtrack' behavior: browser back buttons, undo/redo in text editors, and the call stack itself (which is literally a stack of function calls waiting to return) all rely on LIFO order. Stacks are also the natural tool for checking balanced parentheses, evaluating mathematical expressions, and implementing depth-first search.

Queues naturally model any 'first come, first served' behavior: print job queues, task scheduling, and breadth-first search all rely on FIFO order โ€” because breadth-first traversal needs to process nodes in the exact order they were discovered, level by level, which is precisely what a queue guarantees.

๐Ÿ–ฅ๏ธ Applied Scenario
You're implementing an 'undo' feature for a drawing app, and separately, a print job manager for a shared office printer.
1
For undo, you choose a stack: every action the user takes gets pushed onto it, and clicking 'undo' pops the most recent action off โ€” exactly the LIFO behavior undo requires.
2
For the print manager, you choose a queue: every submitted print job gets enqueued at the back, and the printer always dequeues and processes whichever job has been waiting longest.
3
You recognize that using a queue for undo would be wrong โ€” it would undo the very first action taken, not the most recent one, which isn't what users expect from 'undo.'
4
Conclusion: the correct structure follows directly from which order removal needs to happen in โ€” most-recent-first (stack) or earliest-first (queue) โ€” not from any other property of the data.
๐Ÿ“Œ Exam Application
Exam questions often describe a real-world scenario (browser history, task scheduling, undo functionality, breadth-first vs. depth-first search) and ask you to identify whether a stack or queue is the appropriate structure, and to justify the choice by naming the removal order the scenario requires. You may also be asked to state the time complexity of push/pop/enqueue/dequeue โ€” all O(1) when implemented properly with a linked list or array with tracked ends.
โš ๏ธ Most Common Stack vs Queue Mistakes
The most common mistake is confusing which end each structure removes from under pressure โ€” remembering 'LIFO' and 'FIFO' as abstract acronyms without anchoring them to a concrete mental image (plates vs. a line) leads to mixing them up. Another frequent error is assuming a queue implemented with a plain array is efficient โ€” removing from the front of an array requires shifting every remaining element over, which is O(n); a proper queue implementation uses a circular buffer or linked list to achieve true O(1) dequeue.
โœ“ Quick Self-Test
Can you explain, using the plate-stack and coffee-line images, why a stack is LIFO and a queue is FIFO? Can you name two real-world systems that rely on each structure, and explain what would go wrong if you swapped them?
Next Lesson
Linked Lists
โ†’
โ† All Data Structures Lessons