Skip to content
BENIBIX Learn

Stack vs Queue

A stack and a queue both hold items in a line, but they let them out from different ends. A stack gives back the last item in. A queue gives back the first.

Development🧮 Algorithms & Data Structures

Stack (Data Structure)

A pile where the last item in is the first one out. You push and pop.

A stack is a list where you can only add or remove at one end, called the top. The last item you put in is the first one out, which is called last in, first out. Adding is called push and removing is called pop. The undo button in an editor is a stack of your past changes. A program also keeps a call stack, so it knows where to return after each function.

Example
Lucy types three words, then presses undo twice. The stack of changes gives back the last word, then the one before it.
Why it matters
Undo, the back button and each function call all use a stack.
Full entry
Development🧮 Algorithms & Data Structures

Queue (Data Structure)

A line where the first item in is the first one out. Join at the back.

A queue is a list where items join at the back and leave from the front. The first item in is the first one out, which is called first in, first out. Adding is called enqueue and removing is called dequeue. Print jobs, messages and tasks waiting for a worker all sit in queues. In Python, a deque does both ends quickly.

Example
Emma and then Jack send pages to the office printer. The queue prints Emma's page first, because it arrived first.
Why it matters
Queues keep work fair and in order when it arrives faster than it can be done.
Full entry

In one line

Stack (Data Structure)
A pile where the last item in is the first one out. You push and pop.
Queue (Data Structure)
A line where the first item in is the first one out. Join at the back.