Algorithm Learning/Queue

Queue

FIFO (First In First Out) structure. The first data in is the first out.

EasyData StructureFIFOQueue

Definition

Queue is a FIFO (First In First Out) data structure. The first item added is the first to be removed, just like waiting in line.

Key Characteristics

  • FIFO (First In, First Out) structure - First item in is first out
  • Enqueue and Dequeue operations in O(1) time
  • Managed with two pointers: Front and Rear
  • Used in BFS (Breadth-First Search), task scheduling, etc.

Use Cases

Used in these scenarios:

🏦

Bank Queue

Customers who arrive first are served first. This is the most common real-life example of a queue.

🖨️

Print Queue

When multiple people use a printer, print jobs are processed in the order they were requested.

🌳

BFS Traversal

Queue is used for breadth-first traversal of graphs or trees. Visits nearby vertices first.

⚙️

Task Scheduler

Operating systems and programs use queues to process tasks in order.

Operations

Main operations:

enqueue

O(1)

Add a new element to the rear of the queue.

dequeue

O(1)

Remove and return the element at the front of the queue.

front / peek

O(1)

View the front element without removing it.

isEmpty

O(1)

Check if the queue is empty.

Complexity

Time Complexity

Best
O(1)
Average
O(1)
Worst
O(1)

Space Complexity

O(n)

Understand Deeper with Visualization

See how the algorithm works through step-by-step animations and code execution.

Start Visualization