# Stack vs Queue

URL: https://softwaredictionary.org/compare/stack-vs-queue
Last updated: 2026-09-30

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.

## What is the difference between a stack and a queue?

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`.

| Aspect | Stack | Queue |
| --- | --- | --- |
| Order | LIFO: last in, first out | FIFO: first in, first out |
| Add | `push` onto the top | `enqueue` at the back |
| Remove | `pop` from the top | `dequeue` from the front |
| Ends used | One end for both adding and removing | Two ends: add at the back, remove at the front |
| Everyday analogy | A stack of plates | A line of people at a checkout |
| In algorithms | Depth-first search, recursion and backtracking | Breadth-first search, scheduling and buffering |
| Typical uses | Call stack, undo history, expression parsing | Job queues, message queues, print spoolers |

## 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.

## Frequently asked questions

**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.

---

Software Dictionary: https://softwaredictionary.org/ · https://softwaredictionary.org/llms.txt
