Skip to main content

Side by side

StackvsQueue

What is the difference between a stack and a queue?

Updated 2 min read7 differences

In short

A stack removes the newest item first (last in, first out, LIFO), while a queue removes the oldest first (first in, first out, FIFO), like plates versus a line.

Stack

A stack is a data structure that stores items in last in, first out (LIFO) order, so the most recently added item is always the first one removed.

Read the page on Stack

Queue

A queue is a data structure that stores items in first in, first out (FIFO) order, so the item that has waited longest is always the next one removed.

Read the page on Queue

Stack and Queue compared

AspectStackQueue
OrderLIFO: last in, first outFIFO: first in, first out
Addpush onto the topenqueue at the back
Removepop from the topdequeue from the front
Ends usedOne end for both adding and removingTwo ends: add at the back, remove at the front
Everyday analogyA stack of platesA line of people at a checkout
In algorithmsDepth-first search, recursion and backtrackingBreadth-first search, scheduling and buffering
Typical usesCall stack, undo history, expression parsingJob queues, message queues, print spoolers

The difference, explained

A stack is a collection where you add and remove items at the same end, called the top: push adds an item and pop removes one. A queue is a collection where you add items at the back and remove them from the front, with operations usually called enqueue and dequeue.

The difference is the order of removal, and it decides what each one is good for. A stack's LIFO order naturally tracks nested or reversible work, like function calls, undo history and matching brackets. A queue's FIFO order keeps things fair and in arrival order, like print jobs, network requests and tasks waiting for a worker.

Both are abstract data types, so they can be built on arrays or linked lists, and both offer O(1) adds and removes when implemented well. They also appear side by side in algorithms: depth-first search uses a stack, while breadth-first search uses a queue.

A common misconception is that any array makes an efficient queue. In many languages, removing from the front of an array, such as shift() in JavaScript or pop(0) in Python, moves every remaining element, so large queues should use a dedicated structure like Python's collections.deque.

Which one should you use?

Choose Stack when…

  • The newest item should be handled first.
  • You need to undo or backtrack through steps.
  • You are processing nested structures like brackets or function calls.

Choose Queue when…

  • Items must be processed in the order they arrived.
  • You are handing out work fairly to workers.
  • You need level-by-level traversal, as in breadth-first search.

Adding three items and removing one

Stackpython
# Stack: last in, first out
stack = []
stack.append("a")
stack.append("b")
stack.append("c")

print(stack.pop())  # "c", the newest item
Queuepython
# Queue: first in, first out
from collections import deque

queue = deque()
queue.append("a")
queue.append("b")
queue.append("c")

print(queue.popleft())  # "a", the oldest item

Readers ask

Which is faster, a stack or a queue?

Both add and remove items in O(1) time when implemented properly. The choice depends on the order you need, not on speed.

Can you build a queue from two stacks?

Yes. Push new items onto one stack and pop from a second; when the second is empty, move everything over, which reverses the order and gives FIFO behavior in amortized O(1) time.

Is a priority queue a queue?

It is a variation: items leave in order of priority rather than arrival, and it is usually built on a heap rather than a plain list.

Read a random page
Open today's review
Switch to the dark theme
Read this page in Türkçe

More

Settings