In short: A queue is a first-in, first-out (FIFO) data structure: elements are added at the back and removed from the front, like a line of people. Enqueue and dequeue both run in O(1) time, and queues power breadth-first search and task scheduling.

Quick reference

Worst Case
space
enqueue
dequeue
peek

A queue stores items in a first-in, first-out (FIFO) order.

Picture a queue like the line outside a busy restaurant. First come, first served.

Loading...

Strengths:

  • Fast operations. All queue operations take time.

Uses

  • Breadth-first search uses a queue to keep track of the nodes to visit next.
  • Printers use queues to manage jobs—jobs get printed in the order they're submitted.
  • Web servers use queues to manage requests—page requests get fulfilled in the order they're received.
  • Processes wait in the CPU scheduler's queue for their turn to run.

Implementation

Queues are easy to implement with linked lists:

  • To enqueue, insert at the tail of the linked list.
  • To dequeue, remove at the head of the linked list.

You could implement a queue with an array or dynamic array, but it would get kinda messy. Try drawing it out. You'll notice that you'd need to build out a "scoot over" or "re-center" operation that automatically fires when your queue items hit the bottom edge of the array.

Frequently Asked Questions

What is a queue?

A linear data structure that follows first-in, first-out (FIFO) order; the first element added is the first one removed.

What is the time complexity of queue operations?

Enqueue (add to back) and dequeue (remove from front) are both O(1) when implemented with a linked list or a circular buffer.

What's the difference between a stack and a queue?

A stack is last-in, first-out (you remove the most recently added item); a queue is first-in, first-out (you remove the oldest item).

Last updated: June 17, 2026

. . .