Skip to content

What is the difference between a stack and a queue

A stack is a LIFO (last‑in, first‑out) structure while a queue is FIFO (first‑in, first‑out); they differ in the order elements are added and removed. Stacks expose only the top element, queues expose only the front element.

Computer Science · Data structures


Both stacks and queues store collections of items, but they enforce opposite access policies. In a stack, the most recently added element is the only one that can be removed, which makes it useful for backtracking, expression evaluation, and recursion. In a queue, the earliest added element leaves first, supporting scheduling, breadth‑first search, and buffering.

LIFO vs FIFO

The LIFO rule of a stack means push and pop operations occur at the same end, often called the top. The FIFO rule of a queue means enqueue adds at the rear while dequeue removes from the front, requiring two ends. Because of these constraints, the internal implementation differs: a stack can be realized with a simple array index, whereas a queue often needs a circular buffer or linked list with head and tail pointers. Real‑world examples include call stacks for function calls (stack) and printer job queues (queue).

The main contrasts are:

  • Access order: LIFO for stack, FIFO for queue
  • Typical operations: push/pop vs enqueue/dequeue
  • Common uses: recursion, undo vs scheduling, buffering
  • Implementation ease: single pointer vs two pointers

How to use a stack for depth‑first traversal:

  1. 1Start with an empty stack and push the root node
  2. 2Pop the top node, process it, then push its children in reverse order
  3. 3Repeat until the stack is empty

Operation comparison between stack and queue:

OperationStack behaviorQueue behavior
Push / EnqueueAdds element to topAdds element to rear
Pop / DequeueRemoves top elementRemoves front element
Peek / FrontLooks at top without removingLooks at front without removing

Suppose a stack holds the integers [2,5,9] with 9 on top. Pushing 4 results in [2,5,9,4]; popping now returns 4 and leaves [2,5,9]. For a queue containing [2,5,9] where 2 is at the front, enqueuing 4 yields [2,5,9,4]; dequeuing returns 2 and leaves [5,9,4]. This side‑by‑side illustration shows how the same sequence behaves differently under LIFO and FIFO rules, which is why choosing the right structure matters for algorithm correctness.

Performance and memory considerations also differ. A stack can often be implemented with a single index, giving O(1) push and pop and minimal overhead, but it may overflow if the recursion depth exceeds the allocated space. A queue usually requires two indices (head and tail) or a circular buffer; this adds a constant amount of extra memory but still provides O(1) enqueue and dequeue. Understanding these trade‑offs helps decide which structure fits the problem’s time and space constraints.

Check yourself

Which operation removes the most recently added element?

Get this as a lesson built for you

Describe what you are studying and Lernex writes the lesson and the questions around it. Free, and it takes about a minute.

Try it

No account needed to try it.

What people ask next