Stack and queue: what is the difference?
Last updated: 2026-11-26
A stack removes the item added most recently: last in, first out. A queue removes the item that has waited longest: first in, first out. To choose between them for a task, ask which waiting item the task needs next.
Start learning No account needed
1. Two ways to take items back
In a stack, you push an item onto the top and pop from the top, like a stack of plates. In a queue, you enqueue at the back and dequeue from the front, like a line of people. Both are abstract data types, which means the description says how they behave and leaves open how they are stored.
2. A worked example: the same operations on both
Add A, B and C, remove once, add D, then remove twice. Trace both on paper before you read the script output.
step | operation | stack (top at right) | removed | queue (front at left) | removed
1 | add A | A | - | A | -
2 | add B | A B | - | A B | -
3 | add C | A B C | - | A B C | -
4 | remove | A B | C | B C | A
5 | add D | A B D | - | B C D | -
6 | remove | A B | D | C D | B
7 | remove | A | B | D | C
brackets ({}): matched
brackets ({)}: not matched
| Remove at step | Stack gives | Queue gives |
|---|---|---|
| 4 | C | A |
| 6 | D | B |
| 7 | B | C |
The stack ends holding A, and the queue ends holding D. A short script, scripts/stack-queue-worked-example.py, printed the trace and also ran the bracket check in section 3.
3. Choose by what the task needs next
An undo command reverses the latest change first, so the changes fit a stack. Print jobs served in order of arrival fit a queue. Function calls fit a stack, since the function called last returns first. Breadth-first search, which looks at the neighbors of a node in a graph before going further out, is typically implemented with a queue. Depth-first search, which goes down from a node before moving to its siblings, can use a stack to back up.
Matching brackets is a stack task. Push each opening bracket. When a closing one arrives, it has to match the opening bracket on top. The text ({}) passes. The text ({)} fails, because ) arrives while { is on top.
4. A common mistake: reciting the rule without naming the ends
You can say "last in, first out" and still trace a queue wrong. Before you start, write which end receives items and which end gives them back. A stack uses one end for both. A queue uses opposite ends.
In Python, a list works as a stack with append() and pop(). For a queue, the tutorial recommends collections.deque with append() and popleft(), because removing from the front of a list has to shift the other elements.
5. Test yourself
Add 1, 2, 3, remove twice, add 4, and remove once. What does each structure hold at the end? Then pick a structure for a help desk that serves tickets in arrival order, and for a browser's back button. Give the reason in terms of which item is needed next.
See also how to learn programming without copying code and how to test yourself.
What the sources found, and their limits
NIST Dictionary of Algorithms and Data Structures. Paul E. Black. It defines a stack and a queue by which item may be removed. Sources: stack and queue. Its entries on breadth-first search and depth-first search describe the two search orders. All read on 2026-11-25.
Wikipedia, Stack and Queue. They give the operation names, the call stack, backtracking with depth-first search, and the use of queues in breadth-first search. Sources: Stack and Queue, read in part on 2026-11-25.
Python tutorial, sections 5.1.1 and 5.1.2. It shows lists as stacks and deque for queues. Source: Data structures, read in part on 2026-11-25. Limits: the undo, print job and bracket examples are ours, and a real program may need more than either structure, such as priorities.
How Professor Turing helps
Professor Turing is a tutor for adults learning any subject, in college or on their own. It asks you questions instead of giving the final answer, does not grade a bare guess, and remembers what you got wrong. No account is needed to start. It's an AI tutor, free to try.
Bring a task where you have to pick a data structure. Its first reply asks what you already know before it teaches anything. If you answer with a bare guess, it asks how you got there instead of saying right or wrong, and when you stay stuck it changes its approach and makes the next step smaller, so you work it out yourself. You must be 18 or older, and guest conversations are deleted 7 days after the last activity.
FAQ
Which one is first in, first out? The queue. A stack is last in, first out.
Can a Python list be used as a queue? It can, but the Python tutorial recommends collections.deque, because removing from the front of a list shifts the other elements.
Start with what you're working on
Tell the tutor which task you are deciding about and let it ask you the questions.
Start learning No account needed
See also: what is a hash table · what is recursion · array and linked list · breadth-first and depth-first search · all study guides