Live · 1419 articles
dsa

Queue Data Structure: FIFO, Circular Queue, Deque, and Priority Queue (2026)

Complete Queue Data Structure: FIFO, Circular Queue, Deque, and Priority Queue tutorial covering fundamentals, practical examples, and best practices for m

dsa14 Mar 2026

A queue is a linear data structure that follows the First-In-First-Out (FIFO) principle. Elements are added at the rear (enqueue) and removed from the front (dequeue). Queues model real-world waiting lines and are essential in BFS, task scheduling, and buffering systems.

This tutorial covers simple queue, circular queue, deque (double-ended queue), and priority queue implementations with C++, Java, and Python code examples.

Simple Queue and Circular Queue

A simple queue can be implemented with an array and two pointers: front and rear. Enqueue increments rear; dequeue increments front. The drawback is space wastage — once an element is dequeued, its slot cannot be reused. A circular queue solves this by wrapping around using modulo arithmetic: rear = (rear + 1) % capacity.

In a circular queue, the queue is full when (rear + 1) % capacity == front. This design reuses empty slots efficiently and is the foundation for many production queue implementations.

// C++ — circular queue
class CircularQueue {
  int *arr, front, rear, cap;
public:
  CircularQueue(int c) {
    cap = c; arr = new int[c]; front = rear = 0;
  }
  void enqueue(int x) {
    if((rear+1)%cap == front) return;
    arr[rear] = x; rear = (rear+1)%cap;
  }
  int dequeue() {
    if(front == rear) return -1;
    int x = arr[front]; front = (front+1)%cap; return x;
  }
};

// Java — using ArrayDeque
ArrayDeque q = new ArrayDeque<>();
q.addLast(10); // enqueue
q.removeFirst(); // dequeue

// Python — using collections.deque
from collections import deque
q = deque()
q.append(10)  # enqueue
q.popleft()   # dequeue

Deque and Priority Queue

A deque (double-ended queue) allows insertion and deletion at both ends. It supports addFirst, addLast, removeFirst, removeLast — all in O(1) amortized time. Deques are used in sliding window problems and palindrome checking.

A priority queue assigns a priority to each element; the element with the highest (or lowest) priority is dequeued first. Heaps (typically binary heaps) provide the underlying implementation with O(log n) insertion and deletion. Python's heapq and Java's PriorityQueue implement min-heaps by default.

// C++
priority_queue pq; // max-heap
pq.push(10); pq.push(30); pq.push(20);
cout << pq.top(); // 30

// Java
PriorityQueue pq = new PriorityQueue<>();
pq.add(10); pq.add(30); pq.add(20);
System.out.println(pq.peek()); // 10 (min-heap)

// Python
import heapq
heap = []
heapq.heappush(heap, 10)
heapq.heappush(heap, 30)
print(heapq.heappop(heap)) # 10

Frequently Asked Questions

What is the difference between a stack and a queue?

Stack is LIFO (Last-In-First-Out); Queue is FIFO (First-In-First-Out). They differ in the order elements are removed.

When is a circular queue preferred over a simple queue?

A circular queue reuses deallocated space and is preferred when memory efficiency matters and the queue size is bounded.

Originally published on Ayodhyya. Last updated June 1, 2026.

Variations: Circular, Deque, Priority

Circular queues reuse empty slots after deques. Deques allow O(1) insertion and removal at both ends. Priority queues (heaps) extract the minimum in O(log n) — essential for Dijkstra and scheduling.

BFS and Task Scheduling

Queues power BFS (shortest path in unweighted graphs), OS process scheduling (FIFO queues), message brokers (Kafka partitions are queues), and load balancers distributing requests to workers.

Array vs Linked List Implementation

Array-based circular queues have better cache locality and no per-node allocation overhead. Linked-list queues avoid the fixed-size limitation but suffer from pointer chasing and memory overhead.

Variations: Circular, Deque, Priority

Circular queues reuse empty slots after deques. Deques allow O(1) insertion and removal at both ends. Priority queues (heaps) extract the minimum in O(log n) — essential for Dijkstra and scheduling.<\/p>

BFS and Task Scheduling

Queues power BFS (shortest path in unweighted graphs), OS process scheduling (FIFO queues), message brokers (Kafka partitions are queues), and load balancers distributing requests to workers.<\/p>

Array vs Linked List Implementation

Array-based circular queues have better cache locality and no per-node allocation overhead. Linked-list queues avoid the fixed-size limitation but suffer from pointer chasing and memory overhead.<\/p>

Sandip Mhaske
Sandip Mhaske
Senior Engineer · Author of 1419 guides · Follow for System Design & .NET deep-dives
About