A lesson from the data structures and algorithms bootcamp. About a 10 minute read.
A queue is first in, first out. Swapping a stack for a queue changes nothing about your code except the order things come out, and that one change turns a depth-first search into a breadth-first one, which is how you get shortest paths. It is the smallest edit in this whole subject with the biggest consequence.
A queue is a sequence where you enqueue at the back and dequeue from the front, so the first thing in is the first thing out. Where a stack is the memory for nesting, a queue is the memory for FAIRNESS: nothing jumps the line, and everything waits its turn. A deque, pronounced 'deck', is a queue you can push and pop at both ends, which makes it a stack and a queue at once and is what most languages actually hand you.
The rest of this lesson continues with 5 further sections. See the full curriculum.
Browse all 536 practice puzzles