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