Skip to content

What is a Queue?

A queue is a data structure that also stores a sequence of elements, but unlike a stack, it follows FIFO (First In, First Out). The first element added is the first one removed, like a queue of people waiting in line.

Structure

FRONT                        BACK
  |                           |
  v                           v
 [1] -> [2] -> [3] -> [4] -> [5]

Elements are added to the back and removed from the front. Just like with stacks, we cannot access elements in the middle directly.

Core Operations

OperationDescriptionTime
enqueue(x)Add an element to the backO(1)
dequeue()Remove and return the front elementO(1)
peek()Return the front element without removingO(1)
isEmpty()Check if the queue is emptyO(1)

Queue vs Stack

QueueStack
OrderFIFOLIFO
AddBackTop
RemoveFrontTop
ExampleWaiting lineStack of plates

When to use a Queue

  • You need to process elements in the order they arrived
  • You are implementing breadth-first search (BFS)
  • You need to buffer or schedule tasks
  • You are simulating real world lines or queues

Personal study notes for learning data structures