Implementation of Circular Queue Using Array (original) (raw)

Last Updated : 30 Oct, 2025

A circular queue is a linear data structure that overcomes the limitations of a simple queue. In a normal array implementation, dequeue() can be O(n) or we may waste space. Using a circular array, both enqueue() and dequeue() can be done in O(1).

Declaration using Array:

In this implementation, arr is used to store elements. some variables are maintained:

class myQueue { private:

// Fixed-size array to store queue elements
int* arr;

// Index of the front element
int front;

// Current number of elements in the queue
int size;

// Maximum capacity of the queue
int capacity;

public:

// Constructor to initialize the queue with given capacity
myQueue(int cap) {
    capacity = cap;
    arr = new int[capacity]; 
    front = 0;
    size = 0;
}

};

Java

class myQueue {

// Fixed-size array to store queue elements
private int[] arr;

// Index of the front element
private int front;

// Current number of elements in the queue
private int size;

// Maximum capacity of the queue
private int capacity;

// Constructor to initialize the queue with given capacity
public myQueue(int cap) {
    capacity = cap;
    arr = new int[capacity]; 
    front = 0;
    size = 0;
}

}

Python

class myQueue:

def __init__(self, cap):
      # Maximum capacity of the queue
    self.capacity = cap
    
   # Fixed-size list to store queue elements
    self.arr = [0] * cap  
    
   # Index of the front element
    self.front = 0
    
     # Current number of elements in the queue
    self.size = 0

C#

class myQueue {

// Fixed-size array to store queue elements
private int[] arr;

// Index of the front element
private int front;

// Current number of elements in the queue
private int size;

// Maximum capacity of the queue
private int capacity;

// Constructor to initialize the queue with given capacity
public myQueue(int cap) {
    capacity = cap;
    arr = new int[capacity]; 
    front = 0;
    size = 0;
}

}

JavaScript

class myQueue { // Fixed-size array to store queue elements arr;

// Index of the front element
front;

// Current number of elements in the queue
size;

// Maximum capacity of the queue
capacity;

// Constructor to initialize the queue with given capacity
constructor(cap) {
    this.capacity = cap;
    this.arr = new Array(cap); 
    this.front = 0;
    this.size = 0;
}

}

`

Operations On Circular Queue:

enqueue(x) :

**Purpose: Insert an element x at the rear of the circular queue.

// Insert an element at the rear void enqueue(int x) { if (size == capacity) { cout << "Queue is full!" << endl; return; } int rear = (front + size) % capacity; arr[rear] = x; size++; }

Java

// Insert an element at the rear public void enqueue(int x) { if (size == capacity) { System.out.println("Queue is full!"); return; } int rear = (front + size) % capacity; arr[rear] = x; size++; }

Python

Insert an element at the rear

def enqueue(self, x): if self.size == self.capacity: print("Queue is full!") return rear = (self.front + self.size) % self.capacity self.arr[rear] = x self.size += 1

C#

// Insert an element at the rear public void enqueue(int x) { if (size == capacity) { Console.WriteLine("Queue is full!"); return; } int rear = (front + size) % capacity; arr[rear] = x; size++; }

JavaScript

// Insert an element at the rear function enqueue(x) { if (this.size === this.capacity) { console.log("Queue is full!"); return; } let rear = (this.front + this.size) % this.capacity; this.arr[rear] = x; this.size++; }

`

dequeue() :

**Purpose: Remove and return the front element from the circular queue.

// Remove an element from the front int dequeue() { if (size == 0) { cout << "Queue is empty!" << endl; return -1; } int res = arr[front]; front = (front + 1) % capacity; size--; return res; }

Java

// Remove an element from the front public int dequeue() { if (size == 0) { System.out.println("Queue is empty!"); return -1; } int res = arr[front]; front = (front + 1) % capacity; size--; return res; }

Python

Remove an element from the front

def dequeue(self): if self.size == 0: print("Queue is empty!") return -1 res = self.arr[self.front] self.front = (self.front + 1) % self.capacity self.size -= 1 return res

C#

// Remove an element from the front public int dequeue() { if (size == 0) { Console.WriteLine("Queue is empty!"); return -1; } int res = arr[front]; front = (front + 1) % capacity; size--; return res; }

JavaScript

// Remove an element from the front function dequeue() { if (this.size === 0) { console.log("Queue is empty!"); return -1; } let res = this.arr[this.front]; this.front = (this.front + 1) % this.capacity; this.size--; return res; }

`

getRear() :

**Purpose: Return the element at the rear of the circular queue.

// Get the rear element int getRear() { if (size == 0) return -1;
int rear = (front + size - 1) % capacity; return arr[rear]; }

Java

// Get the rear element public int getRear() { if (size == 0) return -1;
int rear = (front + size - 1) % capacity; return arr[rear]; }

Python

Get the rear element

def getRear(self): if self.size == 0: return -1
rear = (self.front + self.size - 1) % self.capacity return self.arr[rear]

C#

// Get the rear element public int getRear() { if (size == 0) return -1;
int rear = (front + size - 1) % capacity; return arr[rear]; }

JavaScript

// Get the rear element function getRear() { if (this.size === 0) return -1; let rear = (this.front + this.size - 1) % this.capacity; return this.arr[rear]; }

`

getFront() :

**Purpose: Return the element at the front of the circular queue.

// Get the front element int getFront() { if (size == 0) return -1;
return arr[front]; }

Java

// Get the front element public int getFront() { if (size == 0) return -1;
return arr[front]; }

Python

Get the front element

def getFront(self): if self.size == 0: return -1 return self.arr[self.front]

C#

// Get the front element public int getFront() { if (size == 0) return -1;
return arr[front]; }

JavaScript

// Get the front element function getFront() { if (this.size === 0) return -1; return this.arr[this.front]; }

`

Complete Implementation:

C++ `

#include using namespace std;

class myQueue { private: // fixed-size array int* arr;
// index of front element int front; // current number of elements int size;
// maximum capacity int capacity;

public: myQueue(int cap) { capacity = cap; arr = new int[capacity]; front = 0; size = 0; }

// Insert an element at the rear
void enqueue(int x) {
    if (size == capacity) {
        cout << "Queue is full!" << endl;
        return;
    }
    int rear = (front + size) % capacity;
    arr[rear] = x;
    size++;
}

// Remove an element from the front
int dequeue() {
    if (size == 0) {
        cout << "Queue is empty!" << endl;
        return -1;
    }
    int res = arr[front];
    front = (front + 1) % capacity;
    size--;
    return res;
}

// Get the front element
int getFront() {
    if (size == 0) return -1;
    return arr[front];
}

// Get the rear element
int getRear() {
    if (size == 0) return -1;
    int rear = (front + size - 1) % capacity;
    return arr[rear];
}

};

int main() { myQueue q(5); q.enqueue(10); q.enqueue(20); q.enqueue(30); cout << q.getFront() << " " << q.getRear() << endl; q.dequeue(); cout << q.getFront() << " " << q.getRear() << endl; q.enqueue(40); cout << q.getFront() << " " << q.getRear() << endl; return 0; }

Java

class myQueue { // fixed-size array private int[] arr;
// index of front element private int front;
// current number of elements private int size;
// maximum capacity private int capacity;

public myQueue(int cap) {
    capacity = cap;
    arr = new int[capacity];
    front = 0;
    size = 0;
}

// Insert an element at the rear
public void enqueue(int x) {
    if (size == capacity) {
        System.out.println("Queue is full!");
        return;
    }
    int rear = (front + size) % capacity;
    arr[rear] = x;
    size++;
}

// Remove an element from the front
public int dequeue() {
    if (size == 0) {
        System.out.println("Queue is empty!");
        return -1;
    }
    int res = arr[front];
    front = (front + 1) % capacity;
    size--;
    return res;
}

// Get the front element
public int getFront() {
    if (size == 0) return -1;
    return arr[front];
}

// Get the rear element
public int getRear() {
    if (size == 0) return -1;
    int rear = (front + size - 1) % capacity;
    return arr[rear];
}

public static void main(String[] args) {
    myQueue q = new myQueue(5);
    q.enqueue(10);
    q.enqueue(20);
    q.enqueue(30);
    System.out.println(q.getFront() + " " + q.getRear());
    q.dequeue();
    System.out.println(q.getFront() + " " + q.getRear());
    q.enqueue(40);
    System.out.println(q.getFront() + " " + q.getRear());
}

}

Python

class myQueue: def init(self, cap): # fixed-size array self.arr = [0]*cap
# index of front element self.front = 0 # current number of elements self.size = 0
# maximum capacity self.capacity = cap

# Insert an element at the rear
def enqueue(self, x):
    if self.size == self.capacity:
        print("Queue is full!")
        return
    rear = (self.front + self.size) % self.capacity
    self.arr[rear] = x
    self.size += 1

# Remove an element from the front
def dequeue(self):
    if self.size == 0:
        print("Queue is empty!")
        return -1
    res = self.arr[self.front]
    self.front = (self.front + 1) % self.capacity
    self.size -= 1
    return res

# Get the front element
def getFront(self):
    if self.size == 0:
        return -1
    return self.arr[self.front]

# Get the rear element
def getRear(self):
    if self.size == 0:
        return -1
    rear = (self.front + self.size - 1) % self.capacity
    return self.arr[rear]

if name == "main": q = myQueue(5) q.enqueue(10) q.enqueue(20) q.enqueue(30) print(q.getFront(), q.getRear()) q.dequeue() print(q.getFront(), q.getRear()) q.enqueue(40) print(q.getFront(), q.getRear())

C#

using System;

class myQueue { // fixed-size array private int[] arr;
// index of front element private int front;
// current number of elements private int size;
// maximum capacity private int capacity;

public myQueue(int cap) {
    capacity = cap;
    arr = new int[capacity];
    front = 0;
    size = 0;
}

// Insert an element at the rear
public void enqueue(int x) {
    if (size == capacity) {
        Console.WriteLine("Queue is full!");
        return;
    }
    int rear = (front + size) % capacity;
    arr[rear] = x;
    size++;
}

// Remove an element from the front
public int dequeue() {
    if (size == 0) {
        Console.WriteLine("Queue is empty!");
        return -1;
    }
    int res = arr[front];
    front = (front + 1) % capacity;
    size--;
    return res;
}

// Get the front element
public int getFront() {
    if (size == 0) return -1;
    return arr[front];
}

// Get the rear element
public int getRear() {
    if (size == 0) return -1;
    int rear = (front + size - 1) % capacity;
    return arr[rear];
}

static void Main() {
    myQueue q = new myQueue(5);
    q.enqueue(10);
    q.enqueue(20);
    q.enqueue(30);
    Console.WriteLine(q.getFront() + " " + q.getRear());
    q.dequeue();
    Console.WriteLine(q.getFront() + " " + q.getRear());
    q.enqueue(40);
    Console.WriteLine(q.getFront() + " " + q.getRear());
}

}

JavaScript

class myQueue { constructor(cap) {

    // fixed-size array
    this.arr = new Array(cap);  
   
    // index of front element
    this.front = 0; 
   
    // current number of elements
    this.size = 0;    
   
    // maximum capacity
    this.capacity = cap;        
}

// Insert an element at the rear
enqueue(x) {
    if (this.size === this.capacity) {
        console.log("Queue is full!");
        return;
    }
    let rear = (this.front + this.size) % this.capacity;
    this.arr[rear] = x;
    this.size++;
}

// Remove an element from the front
dequeue() {
    if (this.size === 0) {
        console.log("Queue is empty!");
        return -1;
    }
    let res = this.arr[this.front];
    this.front = (this.front + 1) % this.capacity;
    this.size--;
    return res;
}

// Get the front element
getFront() {
    if (this.size === 0) return -1;
    return this.arr[this.front];
}

// Get the rear element
getRear() {
    if (this.size === 0) return -1;
    let rear = (this.front + this.size - 1) % this.capacity;
    return this.arr[rear];
}

}

// Driver Code let q = new myQueue(5); q.enqueue(10); q.enqueue(20); q.enqueue(30); console.log(q.getFront(), q.getRear()); q.dequeue(); console.log(q.getFront(), q.getRear()); q.enqueue(40); console.log(q.getFront(), q.getRear());

`