Data Structures · Visualization

Queue: Introduction

Queue

A queue is a collection where items are added at one end (the back) and removed from the other end (the front). Time: O(1) for enqueue/dequeue with a proper implementation (e.g. linked list or circular buffer). Space: O(n).

Big-O: enqueue / dequeue are both O(1).

Queue: Introduction

Queue

A queue is a collection where items are added at one end (the back) and removed from the other end (the front). It follows the FIFO rule — First In, First Out — just like a line of people waiting: whoever joined the line first is the first one served.

How Queue: Introduction Works

  1. Keep track of both the 'front' (where items leave) and the 'back' (where items enter).
  2. To add an item, place it at the back (enqueue).
  3. To remove an item, take it from the front (dequeue).
  4. Optionally, peek at the front item without removing it.

Queue: Introduction Worked Example

Input: Enqueue 1, enqueue 2, enqueue 3, then dequeue

  1. Enqueue 1 → queue: [1]
  2. Enqueue 2 → queue: [1, 2]
  3. Enqueue 3 → queue: [1, 2, 3]
  4. Dequeue → removes 1 (first in) → queue: [2, 3]

Output: Dequeued value: 1, remaining queue: [2, 3]

📖 Introduction: this is an overview of the topic, so there's nothing to run here. Pick an operation from the side menu to see a live, step-by-step animation.
Queue: Introduction
A First-In-First-Out (FIFO) structure — values leave in the same order they arrived, like a line at a checkout.
front
1
2
3
back
Active / highlighted Min stack Result
Queue: Introduction
Press ▶ Run to watch it step by step.
Python · Stacks & Queues in Code
from collections import deque
q = deque()
q.append(1)     # enqueue
q.append(2)
q.popleft()     # dequeue
# FIFO: first in, first out
# example: qintro 1 on [1, 2, 3]

Queue: Introduction Time & Space Complexity

TimeO(1) for enqueue/dequeue with a proper implementation (e.g. linked list or circular buffer)
SpaceO(n)

When to Use Queue: Introduction

  • Task scheduling and print job queues
  • Breadth-first search (BFS) traversal of graphs and trees
  • Handling requests in the order they arrive (e.g. web server request queues)
  • Buffering data streams

Recommended resourcesAffiliate links · may earn us a commission

Recommended for learners

Structured coding coursesVideo courses that pair well with visual practice — pick one language and go deep.Browse courses Classic DSA interview booksThe reference books interviewers themselves learned from — great next to the visualizers.See the books Cloud hosting for side projectsShip the portfolio project that gets you hired — free credits for new accounts.Claim credits

Ready for the next step?

See the same ideas in action — visualize the algorithms that operate on these structures, or take a guided course.