Hide sidebar

Introduction to Queues

A Queue is a linear data structure that follows the First-In, First-Out (FIFO) principle. This means that the first element added to the queue will be the first one to be removed.

Queues are commonly used in programming for tasks such as managing requests in a web server, scheduling tasks, and implementing breadth-first search algorithms.

Interactive Queue Visualization

The visualization below demonstrates the basic operations of a queue. You can see how elements are enqueued at the rear and dequeued from the front.

Queue Visualization (FIFO)

Current Step
1 of 11
Last Operation
Ready to start
Queue Size
3
Queue operations will appear here...
Queue Visualization
FRONT ← Elements ← REAR
FRONT →
10
20
30
← REAR
DequeueFlow DirectionEnqueue
Front Element
10
Next to be dequeued
Rear Element
30
Last enqueued

Array View

[10,20,30]
front: index 0|rear: index 2
+
ENQUEUE
Add to rear
-
DEQUEUE
Remove from front
👁
PEEK
View front (no remove)
Queue (FIFO): Elements are added at the rear and removed from the front. Like a line at a store - first person in line is first to be served! Enqueue adds to rear, dequeue removes from front. All operations are O(1) time complexity. This demo auto-resets to keep cycling through operations.

Queue Data Structure

A Queue is a linear data structure that follows the First-In, First-Out (FIFO) principle. It's like a line of people waiting for a bus: the first person to get in line is the first person to get on the bus.

Key Operations

  • Enqueue: Adds an element to the back of the queue.
  • Dequeue: Removes the front element from the queue.
  • Peek/Front: Returns the front element without removing it.
  • isEmpty: Checks if the queue is empty.
Queue Implementation

from collections import deque

# Using a deque as a queue
queue = deque()

# Enqueue
queue.append(1)
queue.append(2)
queue.append(3)

# Dequeue
front_element = queue.popleft()  # 1

# Peek
front_element = queue[0]  # 2

# isEmpty
if not queue:
    print("Queue is empty")