Advanced Linear Structures

Queue
Variants

Beyond the basic FIFO queue. Explore Circular, Priority, and Double-ended queuesโ€”specialized structures for specific algorithmic challenges.

Circular Queue

Last position connects back to first, forming a circle. Eliminates wasted space in array implementation.

O(1) enqueue Fixed size

Priority Queue

Elements served based on priority, not insertion order. Highest priority element is dequeued first.

O(log n) Heap based

Deque

Double-ended queue allowing insertion and deletion at both front and rear ends.

O(1) all ops Versatile
Space Optimized

Circular Queue

A circular queue connects the last position back to the first, forming a circle. This solves the "false overflow" problem of linear queues where space is available at the front but we can't insert at the rear.

1

Circular Arrangement

After the last position, we wrap around to the first position using modulo arithmetic.

2

No Wasted Space

Unlike linear queues, empty spaces at the front can be reused when rear reaches the end.

3

Modulo Arithmetic

Front and rear pointers use (position + 1) % size to wrap around.

Circular Queue Operations
class CircularQueue {
    constructor(size) {
        this.items = new Array(size);
        this.size = size;
        this.front = -1;
        this.rear = -1;
    }
    
    enqueue(element) {
        if (this.isFull()) return false;
        
        if (this.front === -1) this.front = 0;
        
        // Circular increment
        this.rear = (this.rear + 1) % this.size;
        this.items[this.rear] = element;
        return true;
    }
    
    dequeue() {
        if (this.isEmpty()) return null;
        
        const item = this.items[this.front];
        
        if (this.front === this.rear) {
            // Last element
            this.front = -1;
            this.rear = -1;
        } else {
            // Circular increment
            this.front = (this.front + 1) % this.size;
        }
        return item;
    }
    
    isFull() {
        return (this.rear + 1) % this.size === this.front;
    }
}

Circular Queue Visualizer

Live Demo
Fill 0%
Front: -
Rear: -
Empty circular queue. Size: 8

Key Insight

Circular queue uses modulo arithmetic to reuse empty slots at the beginning, achieving O(1) operations without shifting elements.

Priority Queue Visualizer

Max Heap
Heap structure visualization
Empty priority queue
O(log n)
Insert / Extract
Heap maintenance
O(1)
Peek Max
Root access
Priority Based

Priority Queue

A priority queue serves elements based on priority, not insertion order. The highest priority element is always served first. Typically implemented using a Heap for efficient operations.

1

Max Heap Structure

Parent node is always greater than or equal to children. Root is the maximum priority element.

2

Heapify Operations

After insertion or extraction, heap property is restored through heapify-up or heapify-down.

3

Applications

Dijkstra's algorithm, A* pathfinding, Huffman coding, task scheduling, and load balancing.

Min Heap Implementation
class PriorityQueue {
    constructor() {
        this.heap = [];
    }
    
    // Insert with priority
    enqueue(value, priority) {
        const node = { value, priority };
        this.heap.push(node);
        this.heapifyUp(this.heap.length - 1);
    }
    
    heapifyUp(index) {
        const parent = Math.floor((index - 1) / 2);
        
        if (parent >= 0 && this.heap[parent].priority > this.heap[index].priority) {
            [this.heap[parent], this.heap[index]] = 
            [this.heap[index], this.heap[parent]];
            this.heapifyUp(parent);
        }
    }
    
    // Extract minimum (highest priority)
    dequeue() {
        if (this.heap.length === 0) return null;
        
        const min = this.heap[0];
        const end = this.heap.pop();
        
        if (this.heap.length > 0) {
            this.heap[0] = end;
            this.heapifyDown(0);
        }
        return min;
    }
}

Common Applications

Dijkstra's Algorithm
A* Pathfinding
Huffman Coding
CPU Scheduling
Double Ended

Deque (Double-ended Queue)

A deque (pronounced "deck") allows insertion and deletion at both the front and rear ends. It combines the capabilities of both stacks and queues, offering maximum flexibility.

O(1)
Add/Remove Front
pushFront / popFront
O(1)
Add/Remove Rear
pushRear / popRear
1

Input Restricted

Insertion at only one end, deletion at both ends. Useful for specific scheduling algorithms.

2

Output Restricted

Deletion at only one end, insertion at both ends. Used in buffer management.

Deque Implementation
class Deque {
    constructor() {
        this.items = [];
    }
    
    // Add to front
    pushFront(element) {
        this.items.unshift(element);
    }
    
    // Add to rear (same as queue enqueue)
    pushRear(element) {
        this.items.push(element);
    }
    
    // Remove from front (same as queue dequeue)
    popFront() {
        if (this.isEmpty()) return null;
        return this.items.shift();
    }
    
    // Remove from rear (same as stack pop)
    popRear() {
        if (this.isEmpty()) return null;
        return this.items.pop();
    }
    
    peekFront() {
        return this.items[0];
    }
    
    peekRear() {
        return this.items[this.items.length - 1];
    }
}

Deque Visualizer

Double-ended
FRONT
REAR
Empty deque. Add elements from either end.

๐Ÿ”„ Palindrome Checker

Classic deque application: Check if a string is palindrome by comparing characters from both ends.

Choose the Right Queue

Each variant solves specific problems. Here's when to use which.

Circular Queue

โœ“ Fixed memory constraints
โœ“ Memory efficiency critical
โœ“ Streaming data buffers
โœ“ Round-robin scheduling
Best For
Traffic management, CPU scheduling

Priority Queue

โœ“ Task scheduling by urgency
โœ“ Graph algorithms (Dijkstra)
โœ“ Huffman coding
โœ“ Load balancing
Best For
Emergency rooms, network packets

Deque

โœ“ Undo-Redo functionality
โœ“ Sliding window problems
โœ“ Palindrome checking
โœ“ Steal work scheduling
Best For
Browser history, A* algorithm

Time Complexity Comparison

Operation Circular Priority Deque
Insert Front O(1) O(log n) O(1)
Insert Rear O(1) O(log n) O(1)
Delete Front O(1) O(log n) O(1)
Delete Rear O(1) - O(1)
Peek O(1) O(1) O(1)