Programming

What is the difference between a stack and a queue?

Short answer

A stack is Last-In-First-Out (LIFO) — the last item added is the first removed — while a queue is First-In-First-Out (FIFO) — the first item added is the first removed.

Both are fundamental data structures for storing an ordered collection of items, but they differ in how items are added and removed.

Stack (LIFO)

Think of a stack of plates — you add a new plate to the top, and you take the top plate off first. Stacks are used for undo functionality in editors, browser back-button history, and managing function calls in a program (the "call stack").

Queue (FIFO)

Think of a line at a checkout counter — the first person in line is served first. Queues are used for task scheduling, printer job queues, and handling requests in the order they arrive.

Both support adding and removing items in constant time, but the order in which items come back out is the defining difference between them.

Last reviewed: September 2026