Queue Visualization

← Back to Linear DS

What is a Queue?

A queue is a linear data structure that follows the FIFO(First-In-First-Out) principle. The first element added to the queue is the first one to be removed, similar to a line of people waiting where the first person in line is served first.

Queues provide O(1) time complexity for enqueue (insert at rear) and dequeue (remove from front) operations, making them ideal for managing tasks in order, scheduling, and breadth-first search algorithms.

Key Characteristics

  • FIFO Order: First element inserted is first to be removed
  • Two Ends: Insertion at rear (back), deletion from front
  • Enqueue Operation: Add element to the rear - O(1)
  • Dequeue Operation: Remove and return front element - O(1)
  • Common Uses: Task scheduling, print queues, BFS traversal, message buffers
Interactive queue simulation demonstrating FIFO (First-In-First-Out) behavior. Enqueue elements at the rear, dequeue from the front.
Queue is empty. Enqueue elements to begin.

How to Use

Enqueue: Enter a value and click "Enqueue" to add it to the rear of the queue.

Dequeue: Click "Dequeue" to remove and see the front element of the queue.

Peek Front: Click "Peek" to view the front element without removing it.

Clear: Click "Clear" to remove all elements and reset the queue.

Visualization: Watch elements flow from rear to front to understand FIFO (First-In-First-Out) behavior.

Queue Operations

Enqueue: Add an element to the rear of the queue

Dequeue: Remove and return the front element from the queue

Peek Front: View the front element without removing it

Clear: Remove all elements from the queue

Queue Operations Time Complexity

Enqueue: O(1) - Add to rear with tail pointer or circular array.

Dequeue: O(1) - Remove from front with head pointer or index.

Peek/Front: O(1) - Direct access to front element.

isEmpty: O(1) - Compare head and tail pointers or check size.

Search: O(n) - Linear traversal required; not a standard queue operation.

Space Complexity: O(n) - Circular array avoids wasted space from linear array.

Queue vs Other Data Structures

vs Stack: Queue is FIFO (First-In-First-Out); Stack is LIFO (Last-In-First-Out). Use queue for fairness/ordering, stack for backtracking.

vs Array: Queue restricts access to front/rear only; arrays allow random access. Circular arrays optimize queue implementation.

vs Priority Queue: Queue processes in arrival order; priority queue processes by priority value (implemented with heaps).

vs Deque: Queue operates on opposite ends (add rear, remove front); deque allows operations at both ends freely.

Best Use Case: Task scheduling, BFS traversal, print spooling, request handling, and any scenario requiring fair ordering.

Real-World Applications

Print Spooler: Operating systems queue print jobs so documents are printed in the order they were submitted.

Task Scheduling: CPU schedulers use queues to manage processes waiting for execution time in round-robin scheduling.

BFS Traversal: Breadth-first search uses queues to explore graph nodes level by level, finding shortest paths in unweighted graphs.

Message Queues: Distributed systems like RabbitMQ and Kafka use queues to handle asynchronous communication between services.

Request Handling: Web servers queue incoming HTTP requests to process them in order during high traffic periods.

Buffering: Video streaming services buffer data in queues to ensure smooth playback despite variable network speeds.

Call Center Systems: Customer service systems queue callers and route them to agents in first-come-first-served order.

Keyboard Buffer: Operating systems queue keystrokes to handle typing faster than the computer can process each character.

Common Queue Patterns

BFS Traversal: Use queue to explore nodes level by level; essential for shortest path in unweighted graphs.

Level Order Traversal: Process tree nodes level by level using queue; track level boundaries with size or null markers.

Sliding Window Maximum: Deque-based approach to track maximums in current window efficiently in O(n).

Rotten Oranges: Multi-source BFS starting from all rotten oranges simultaneously to find minimum time to rot all.

First Non-Repeating Character: Use queue with frequency map to track first unique character in a stream.

Implement Stack using Queues: Use two queues or single queue with rotation to simulate LIFO behavior.

Circular Queue: Fixed-size queue with wrap-around indexing; useful for bounded buffers and round-robin scheduling.

/* Simple queue using array in C */
#include <stdio.h>
#define CAP 100

int front = 0, rear = 0;
int q[CAP];

void enqueue(int v) { if (rear < CAP) q[rear++] = v; }
int dequeue() { if (front < rear) return q[front++]; return -1; }
int peek() { if (front < rear) return q[front]; return -1; }

int main(){
  enqueue(1); enqueue(2); enqueue(3);
  printf("Front: %d
", peek());
  dequeue();
  printf("After dequeue Front: %d
", peek());
  return 0;
}