Stacks vs Queues: When to Use Which
You'll learn to
- -Compare stacks and queues side by side on ordering, use cases, and complexity
- -Choose the correct structure by asking which item you need to handle next
- -Recognize when neither fits and a priority queue (a heap) is the answer
- -Use collections.deque in Python, and say why list.pop(0) is a trap
With both structures now built from scratch, this short chapter exists purely to crystallize the distinction between them before moving on. No new code beyond one quick reference to Python's standard library.
An Analogy: A Stack of Plates and a Cafe Queue
In a cafe's plate stack, you take the plate on top, which is the one that was put there last. In the queue for the till, the person served first is the one who arrived first. Same kind of collection, opposite rule about who comes out next. That one question is the whole chapter: of everything waiting, which do I want to handle next, the newest or the oldest?
- -The plate stack is a stack: last in, first out (LIFO).
- -The queue at the till is a queue: first in, first out (FIFO).
Same Items, Opposite Order
Add a, b and c in that order, then remove all three. A stack gives them back newest first, and a queue gives them back oldest first:
A Quick Mental Test
- -A browser-style back button, undo history, and matching parentheses all want a stack. The most recent action matters first.
- -A printer job queue, a customer support ticket line, and processing requests fairly in arrival order all want a queue.
- -Tracking in-progress function calls during recursion is a stack, whether or not it is managed by hand. This connects directly to the recursion module later in this tier.
- -Task scheduling and breadth-first search, covered in later tiers, both want a queue for the same fairness reason.
A Third Option: Most Important First
Sometimes neither newest nor oldest is the right rule. An emergency room does not treat patients in arrival order, and a task scheduler runs urgent jobs first. That is a priority queue, usually built on a heap: it always hands over the item with the highest priority. You will meet it again in Dijkstra's algorithm, which always expands the cheapest known node next.
The Practical Real-World Answer: collections.deque
Python's standard library already ships a structure that behaves correctly as either one: collections.deque, a doubly-linked list under the hood offering O(1) append and pop from both ends. Building a Stack and a Queue from scratch in this module was not busywork. Understanding why list.pop(0) is slow, and how a circular buffer avoids that cost, is exactly the reasoning that lets you evaluate any structure's complexity, including ones a language or library hands you ready-made, or the constrained sandbox environment in the Forge Algorithm Lab where a full standard library is not always available.
The trap to avoid is using a plain list as a queue. list.pop(0) removes the first item, and every other item then has to shift one place left, so popping from the front of a list of 100,000 items moves 99,999 of them. deque.popleft() removes from the front in O(1).
Interview Signal is part of Pro
See a real weak answer next to a real strong one for this exact topic.
Quiz is part of Pro
Test what you just read with a short quiz, and bank the XP.
Discussion0
Join the Discussion
Sign in to leave comments, reply to others, or like insights.
No comments yet. Be the first to start the thread!