Computer Science Fundamentals · Foundations

Queues

Want it in plain words first? Jump to Eli explains — the same idea, no jargon.
On this page 9 sections
  1. In 30 seconds
  2. Why this matters
  3. The college version
  4. Eli explains
  5. Worked example
  6. Key takeaway
  7. Quick check
  8. Study tools
  9. Sources & references

In 30 seconds

A is a collection that serves items in the order they arrived: first in, first out (FIFO). You new items at the rear and them from the front, like a line at a checkout. Its core operations - enqueue, dequeue, , and - each run in constant O(1) time when the queue is implemented properly. Queues drive job scheduling, print spooling, buffering, and breadth-first search.

Why this matters

Queues rank among the core data structures you meet again and again, so understanding them early pays off across the rest of computer science. Operating systems queue processes and print jobs; networks buffer packets in queues; breadth-first search explores a graph using a queue; and streaming systems smooth bursts of data with queues. Knowing that a queue guarantees first-in-first-out order - and that its operations are O(1) only with the right implementation - lets you pick the correct structure for a problem and reason about how a system behaves under load. The same FIFO idea also shows up outside code, in any fair 'first come, first served' line, which makes it an intuitive model for scheduling and fairness.

The college version

What a queue is

A queue is an : a collection defined by the operations it supports rather than by how it is stored. Its defining rule is order of service. NIST's Dictionary of Algorithms and Data Structures describes a queue as a collection in which only the earliest added item may be accessed, with basic operations to add at the tail and delete from the head - also known as first-in, first-out, or FIFO. In other words, items leave a queue in exactly the order they arrived. The mental model is a line of people at a checkout: whoever joins the back waits for everyone ahead of them, and the person who has waited longest is served next. This is the opposite of a stack, which is last-in, first-out (a stack serves the most recently added item first); that contrast is the one thing worth remembering about stacks here, and their push/pop behavior is covered in its own lesson. Because a queue is an abstract type, several concrete structures - a linked list, or an array used cleverly - can all realize it, as long as they honor FIFO order.

The core operations

A queue exposes a small, fixed set of operations. Enqueue adds an item at the rear (the tail). Dequeue removes and returns the item at the front (the head) - the item that has been waiting longest. Front, often called peek, returns the front item but leaves it in place, so you can see who is next without serving them. IsEmpty reports whether the queue currently holds any items, which matters because dequeuing from an empty queue is an error you must guard against. Some libraries add a size operation that reports how many items are stored. The important performance fact is that all of these operations can run in O(1) - constant time, independent of how many items the queue holds - when the queue is implemented properly, for example with a linked list that tracks both head and tail pointers, or with a circular array. Python's standard library offers collections.deque for exactly this purpose: its documentation states that appends and pops from either end run with approximately O(1) performance.

Why the implementation matters

The O(1) guarantee is not automatic; it depends on how the front is handled. A tempting but wrong approach is to store the items in an ordinary dynamic array (a Python list, say) and dequeue by removing element zero. That single line looks harmless, but removing the first element of an array forces every remaining element to shift down one position to fill the gap, which is O(n) work - proportional to the number of items left. Python's documentation makes this explicit: list objects incur O(n) memory-movement costs for pop(0) and insert(0, v) because those operations change the position of the underlying data. A proper queue avoids the shift. A linked-list implementation keeps a pointer to both the head and the tail, so enqueue appends at the tail and dequeue unlinks the head, each in constant time. A circular array (a ring buffer) keeps two indices, front and rear, and advances them modulo the array's capacity, reusing freed slots instead of shifting anything. Either way the cost of each operation stays constant. The lesson is general: the same abstract data type can be fast or slow depending on the structure you build it on, so 'a queue is O(1)' is a claim about a correct implementation, not about any code that happens to preserve FIFO order.

Where queues are used, and common variants

Queues appear wherever items must be handled in arrival order or work must be buffered between a fast producer and a slower consumer. Operating systems place processes and print jobs in queues so they run first-come, first-served. Networks and I/O systems buffer incoming data in queues to absorb bursts without losing anything. Breadth-first search, a fundamental graph and tree traversal, uses a queue to visit nodes level by level: you enqueue a node's unvisited neighbors and dequeue the next node to explore, which naturally explores closer nodes before farther ones. Message systems and task pipelines pass jobs through queues to decouple the parts of a system. Several named variants adjust the basic idea. A circular queue wraps a fixed-size array around on itself so freed slots at the front are reused. A double-ended queue, or deque (pronounced 'deck'), allows adding and removing at both ends, generalizing both queues and stacks. A departs from strict FIFO: instead of serving the oldest item, it serves the highest-priority one, which is a different discipline with its own implementation (typically a heap). Each of these deserves its own study; here it is enough to recognize the names and how they relate to the plain FIFO queue.

Eli, the EliExplains learning guide

Eli explains

The same idea, in plain words

Explain it like I’m 10

A queue is like a single-file line. New arrivals join the back, and the person at the very front is always the next to be served. Nobody cuts and nobody jumps ahead: whoever has been waiting the longest goes first. Adding to the back is called enqueue, and taking someone from the front is called dequeue. You can also peek at who is first without serving them, and check whether the line is empty. Because you only ever touch the two ends - the front and the back - each of these actions is quick no matter how long the line gets, as long as the line is built the right way.

Picture it like this

Think of the checkout line at a grocery store. You walk up and join the end of the line (enqueue). The cashier always helps whoever is at the front (dequeue), and that is the person who has been waiting longest. You can glance at who is next (peek) without changing the line, and you can see whether anyone is waiting at all (isEmpty). First come, first served - that is exactly a FIFO queue.

Where the picture stops working

The line picture leaks in a couple of places. Real shoppers can leave a line or let someone cut, but a plain queue is strict FIFO - no jumping ahead. And a real line does not care how it is 'built,' whereas a computer queue is only fast if it is implemented with the right structure; a poorly built one can get slower to remove from as the line grows. A priority queue also breaks the analogy, since it serves the most important item next rather than the one that arrived first.

Worked example

Start with an empty queue and apply this sequence. Enqueue 1: the queue is now [1], with 1 at both front and rear. Enqueue 2: the queue is [1, 2]; 2 went to the rear, and 1 is still at the front. Enqueue 3: the queue is [1, 2, 3]. Now peek at the front: it returns 1 without removing it, so the queue is unchanged. Dequeue: this removes and returns the front item, 1, leaving [2, 3]. Dequeue again: returns 2, leaving [3]. Dequeue once more: returns 3, leaving an empty queue, so isEmpty is now true. The values came out 1, 2, 3 - the exact order they went in, which is the definition of FIFO. This trace was executed in Python 3.9 using collections.deque (append to enqueue, popleft to dequeue), and the dequeue order was confirmed to be [1, 2, 3]. Contrast this with a stack, which would have returned 3, 2, 1.

Key takeaway

A queue is a FIFO collection: enqueue adds at the rear, dequeue removes from the front, and front/peek and isEmpty support them - each O(1) with a proper implementation (linked list or circular array), but O(n) if you naively remove from the front of a plain array.

Quick check

3 questions here, of 5 in this lesson’s practice set. Answers stay hidden until you check.

Question 1 of 3foundational

What does it mean to say a queue is a FIFO structure?

Choose an answer, then check it.
Question 2 of 3foundational

In the standard queue vocabulary, which operation removes and returns the item at the front of the queue?

Choose an answer, then check it.
Question 3 of 3intermediate

You implement a queue by storing items in a plain dynamic array and dequeuing by removing element zero (the first element). Why is this a poor implementation?

Choose an answer, then check it.
Practice all 5

Keep learning

Ready to build on this? Continue to the next lesson.

Practice this lesson
Study tools & related lessonsYou’ll learn to · Common mistakes · Easily confused · Key vocabulary · Related

You’ll learn to

  • Define a queue as a FIFO (first-in, first-out) abstract data type.
  • Describe the core operations enqueue, dequeue, front/peek, and isEmpty and state that each is O(1) with a proper implementation.
  • Explain why removing from the front of a plain array is O(n) and how a proper implementation avoids that cost.
  • Apply the FIFO rule to trace a sequence of enqueue and dequeue operations.
  • Identify real uses of queues (scheduling, buffering, breadth-first search) and name common variants.

Common mistakes

  • Confusing a queue (FIFO) with a stack (LIFO).

    A queue removes the oldest item first (first-in, first-out); a stack removes the newest item first (last-in, first-out). Enqueue 1, 2, 3 then dequeue gives 1, 2, 3; a stack would give 3, 2, 1.

  • Assuming any FIFO-preserving code makes enqueue and dequeue O(1).

    Storing items in a plain array and removing element zero is O(n), because every remaining element must shift down. Constant-time operations require a proper implementation, such as a linked list with head and tail pointers or a circular array.

  • Adding at the front and removing at the front (or mixing up which end does what).

    By convention enqueue adds at the rear and dequeue removes from the front. Adding and removing at the same end turns the structure into a stack, not a queue.

  • Dequeuing or peeking without checking whether the queue is empty.

    Dequeue and front are undefined on an empty queue. Call isEmpty (or check size) first and handle the empty case, or you will hit an error or undefined behavior.

  • Thinking a priority queue is just a normal queue.

    A priority queue serves the highest-priority item next, not the oldest, so it does not follow FIFO order. It is a related but distinct structure, usually built on a heap.

Easily confused

Queue (FIFO) vs. Stack (LIFO)

A queue removes the item that was added earliest (first-in, first-out); a stack removes the item added most recently (last-in, first-out). Queues add at the rear and remove at the front; a stack adds and removes at one end.

Enqueue vs. Dequeue

Enqueue adds an item at the rear of the queue; dequeue removes and returns the item at the front. They operate at opposite ends, and both are O(1) in a proper implementation.

Front (peek) vs. Dequeue

Front returns the front item but leaves the queue unchanged; dequeue returns the front item and removes it. Use peek to inspect who is next without serving them.

Plain queue (FIFO) vs. Priority queue

A plain queue serves items strictly in arrival order; a priority queue serves the highest-priority item next regardless of when it arrived, and is typically implemented with a heap rather than a simple list.

Key vocabulary

Queue
An abstract data type that stores a collection of items and serves them in first-in, first-out order: the item that has waited longest is removed next.
FIFO (first-in, first-out)
The ordering rule of a queue, in which items are removed in the same order they were added.
Enqueue
The operation that adds an item at the rear (tail) of a queue.
Dequeue
The operation that removes and returns the item at the front (head) of a queue - the item added earliest.
Front (peek)
An operation that returns the item at the front of the queue without removing it.
isEmpty
An operation that reports whether the queue currently contains no items; used to guard against dequeuing from an empty queue.
Abstract data type
A data structure defined by the operations it supports and their behavior, independent of the concrete representation used to implement it.
Circular queue (ring buffer)
A queue implemented over a fixed-size array whose front and rear indices wrap around modulo the capacity, reusing freed slots without shifting elements.
Deque (double-ended queue)
A generalization of a queue that allows adding and removing items at both the front and the rear.
Priority queue
A queue-like structure that serves the highest-priority item next instead of the oldest, so it does not follow strict FIFO order.

Sources & references

  1. queue - Dictionary of Algorithms and Data Structures (DADS) — National Institute of Standards and Technology (NIST)
  2. Open Data Structures (in Python), Section 1.2: Interfaces - The Queue, Stack, and Deque Interfaces — Pat Morin
  3. collections - Container datatypes (deque objects) — Python Software Foundation

EliExplains lessons are original prose written from the open, credible references above. See Copyright & Licensing.

Researched 2026-08-19

Educational content only. It is not medical, legal or professional advice. Found an error? Tell us.