Implementing circular queue ring buffer in JavaScript

A circular queue is a linear data structure that operates on the FIFO (First In First Out) principle, where the last position connects back to the first position forming a circle. It's also called a "Ring Buffer".

The main advantage of a circular queue is efficient space utilization. Unlike regular queues, when elements are dequeued from the front, those positions become available for new elements, preventing wasted space.

0 1 2 3 4 5 Front Rear Circular Queue

Problem Statement

Implement a circular queue in JavaScript supporting these operations:

  • MyCircularQueue(k) - Constructor to set queue size to k

  • Front() - Get front item (return -1 if empty)

  • Rear() - Get rear item (return -1 if empty)

  • enQueue(value) - Insert element (return true if successful)

  • deQueue() - Remove element (return true if successful)

  • isEmpty() - Check if queue is empty

  • isFull() - Check if queue is full

Implementation

class CircularQueue {
    constructor(k) {
        this.size = k;
        this.queue = new Array(k);
        this.front = 0;
        this.rear = 0;
        this.count = 0;
    }
    
    enQueue(value) {
        if (this.isFull()) {
            return false;
        }
        this.queue[this.rear] = value;
        this.rear = (this.rear + 1) % this.size;
        this.count++;
        return true;
    }
    
    deQueue() {
        if (this.isEmpty()) {
            return false;
        }
        this.queue[this.front] = undefined;
        this.front = (this.front + 1) % this.size;
        this.count--;
        return true;
    }
    
    Front() {
        if (this.isEmpty()) {
            return -1;
        }
        return this.queue[this.front];
    }
    
    Rear() {
        if (this.isEmpty()) {
            return -1;
        }
        return this.queue[(this.rear - 1 + this.size) % this.size];
    }
    
    isEmpty() {
        return this.count === 0;
    }
    
    isFull() {
        return this.count === this.size;
    }
}

Example Usage

const queue = new CircularQueue(3);

console.log("Enqueue 1:", queue.enQueue(1));
console.log("Enqueue 2:", queue.enQueue(2));
console.log("Enqueue 3:", queue.enQueue(3));
console.log("Enqueue 4:", queue.enQueue(4)); // Should fail (full)

console.log("Front:", queue.Front());
console.log("Rear:", queue.Rear());
console.log("Is Full:", queue.isFull());

console.log("Dequeue:", queue.deQueue());
console.log("Enqueue 4:", queue.enQueue(4)); // Should succeed now
console.log("Rear:", queue.Rear());

console.log("Current state:");
console.log("Front:", queue.Front());
console.log("Rear:", queue.Rear());
console.log("Is Empty:", queue.isEmpty());
Enqueue 1: true
Enqueue 2: true
Enqueue 3: true
Enqueue 4: false
Front: 2
Rear: 3
Is Full: true
Dequeue: true
Enqueue 4: true
Rear: 4
Current state:
Front: 2
Rear: 4
Is Empty: false

Key Features

  • Modular Arithmetic: Uses (index + 1) % size to wrap around

  • Count Tracking: Maintains element count for efficient isEmpty/isFull checks

  • Space Efficiency: Reuses freed positions automatically

  • Time Complexity: All operations are O(1)

Conclusion

Circular queues efficiently implement FIFO with automatic space reuse. The modular arithmetic approach ensures proper wraparound behavior, making it ideal for buffering and resource management scenarios.

Updated on: 2026-03-15T23:19:00+05:30

1K+ Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements