Queues

College Depth 50 in the knowledge graph I know this Set as goal
Unlocks 226 downstream topics
queue FIFO data-structures enqueue-dequeue

Core Idea

A queue is a first-in, first-out (FIFO) data structure that supports enqueue (add to back) and dequeue (remove from front), both in O(1) time. Queues model real-world waiting lines, print spoolers, and process scheduling. A circular buffer (ring buffer) efficiently implements a queue using a fixed-size array without shifting elements. Priority queues extend this concept by dequeuing based on priority rather than arrival order.

How It's Best Learned

Implement a queue using a linked list and then using a circular array with modular index arithmetic. Study how BFS uses a queue to explore graph nodes level by level.

Common Misconceptions

Explainer

A queue enforces a simple discipline: the first item added is the first item removed. This is called FIFO — first-in, first-out — and it models the behavior of any real-world waiting line. When you join the back of a line at a grocery store and leave from the front, you are participating in a queue. The two fundamental operations are enqueue (add an element to the back) and dequeue (remove the element from the front), and both must run in O(1) time for the data structure to be useful.

You already know how arrays and linked lists work, and both can implement a queue — but with important differences. A linked list makes queues straightforward: maintain a pointer to both the head and the tail. Enqueue appends a new node at the tail, dequeue removes the node at the head, and both operations are O(1) with no wasted space. The downside is the per-node memory overhead from storing pointers.

An array-based queue is trickier. The naive approach — adding to the end and removing from the front by shifting all remaining elements left — makes dequeue O(n), which defeats the purpose. The solution is a circular buffer (also called a ring buffer): you maintain two indices, front and rear, that wrap around the array using modular arithmetic. When rear reaches the end of the array, it wraps to index 0 if space is available. Enqueue places an element at rear and advances it; dequeue reads from front and advances it. Both are O(1), and no elements ever need to shift. The tradeoff is that circular buffers have a fixed capacity — when full, you must either reject new elements or resize the underlying array (an amortized O(1) operation if you double the capacity).

Queues appear everywhere in computing. Breadth-first search — which you will encounter soon — depends on a queue to explore graph nodes level by level: you enqueue all unvisited neighbors, then dequeue the next node to process, guaranteeing that closer nodes are visited before farther ones. Operating systems use queues for CPU scheduling (processes wait their turn), print spoolers (documents print in submission order), and network packet buffering. The priority queue extends this concept by dequeuing based on priority rather than arrival order, but that requires a heap — a different data structure entirely — rather than a simple FIFO buffer.

Practice Questions 5 questions

Prerequisite Chain

Counting to 10Counting to 20Understanding ZeroThe Number ZeroCounting to FiveOne-to-One CorrespondenceCombining Small Groups Within 5Addition Within 10Addition Within 20Two-Digit Addition Without RegroupingTwo-Digit Addition with RegroupingAddition Within 100Repeated Addition as MultiplicationMultiplication Facts Within 100Division as Equal SharingDivision as Grouping (Measurement Division)Division: Grouping (Repeated Subtraction) ModelDivision: Fair Sharing ModelDivision as Equal SharingDivision as GroupingBasic Division FactsDivision Facts Within 100Two-Digit by One-Digit DivisionDivision with RemaindersRemainders and Quotients in DivisionDivision Word ProblemsIntroduction to Long DivisionFactors and MultiplesPrime and Composite NumbersEquivalent FractionsRelating Fractions and DecimalsDecimal Place ValueReading and Writing DecimalsComparing and Ordering DecimalsAdding and Subtracting DecimalsMultiplying DecimalsDividing DecimalsDividing FractionsMixed Number ArithmeticOrder of OperationsOperators and ExpressionsArithmetic Operators and Operator PrecedenceComparison Operators and Boolean TestsConditional StatementsDefining and Calling FunctionsFunction Parameters and Argument PassingReturn ValuesVariable ScopeIntroduction to ClassesLinked ListsQueues

Longest path: 51 steps · 210 total prerequisite topics

Prerequisites (2)

Leads To (5)