JavaScript program to Implement a Circular Queue using Arrays
Last Updated :
22 Apr, 2024
A circular queue is a roundabout. When the last person in line is served, they go back to the start of the line. This way, there's no wasted space and the line keeps moving efficiently. It is an extended version of a conventional queue, a circular queue is created by joining the last element to the first element to create a circle.
Operations on Circular Queue
- Enqueue: Add an element to the rear of the queue.
- Dequeue: Remove an element from the front of the queue.
- isFull: Check if the queue is full.
- isEmpty: Check if the queue is empty.
- Front: It will return the first element of the circular queue.
- Rear: It will return the last element of the circular queue.
How to Implement a Circular Queue using Array in JavaScript?
To implement a circular queue using an array in JavaScript, follow these steps:
- Initialize an array named queue of size n, where n is the maximum number of elements the queue can hold.
- Initialize two variables, front and rear, both set to -1 initially.
Enqueue Operation:
To enqueue an element x into the queue:
- Increment rear by 1.
- If rear equals n, set rear to 0 (wrap around).
- If front is -1, set front to 0.
- Set queue[rear] to x.
Dequeue Operation:
To dequeue an element from the queue:
- Check if the queue is empty by verifying if front is -1. If it is, return an error message indicating that the queue is empty.
- Set x to queue[front].
- If front equals rear, set both front and rear to -1.
- Otherwise, increment front by 1, and if front equals n, set front to 0 (wrap around).
- Return x.
Example: The below code implements the circular queue and performs operations on it.
JavaScript
class CircularQueue {
constructor(size) {
this.size = size;
this.queue = new Array(size);
this.front = -1;
this.rear = -1;
}
isFull() {
return (this.front === 0 &&
this.rear === this.size - 1) ||
(this.rear === (this.front - 1 + this.size) %
this.size);
}
isEmpty() {
return this.front === -1;
}
enqueue(item) {
if (this.isFull()) {
console.log("Queue is full");
return;
}
if (this.isEmpty()) {
this.front = 0;
this.rear = 0;
} else {
this.rear = (this.rear + 1) %
this.size;
}
this.queue[this.rear] = item;
console.log(
`${item} enqueued to the queue`);
}
dequeue() {
let item;
if (this.isEmpty()) {
console.log("Queue is empty");
return;
}
if (this.front === this.rear) {
item = this.queue[this.front];
this.front = -1;
this.rear = -1;
} else {
item = this.queue[this.front];
this.front = (this.front + 1) %
this.size;
}
console.log(
`${item} dequeued from the queue`);
return item;
}
displayQueue() {
if (this.isEmpty()) {
console.log("Queue is empty");
return;
}
let i = this.front;
do {
console.log(this.queue[i]);
i = (i + 1) % this.size;
} while (i !== (this.rear + 1) %
this.size);
}
getFront() {
if (this.isEmpty()) {
console.log("Queue is empty");
return;
}
console.log(
`Front element: ${this.queue[this.front]}`);
}
getRear() {
if (this.isEmpty()) {
console.log("Queue is empty");
return;
}
console.log(
`Rear element: ${this.queue[this.rear]}`);
}
}
const queue = new CircularQueue(5);
console.log(
"Is the queue empty? ",
queue.isEmpty());
console.log(
"Is the queue full? ",
queue.isFull());
queue.enqueue(10);
queue.enqueue(20);
queue.enqueue(30);
queue.enqueue(40);
queue.enqueue(50);
console.log(
"Is the queue empty? ",
queue.isEmpty());
console.log(
"Is the queue full? ",
queue.isFull());
queue.displayQueue();
queue.dequeue();
queue.dequeue();
queue.displayQueue();
queue.getFront();
queue.getRear();
Output:
Is the queue empty? true
Is the queue full? false
10 enqueued to the queue
20 enqueued to the queue
30 enqueued to the queue
40 enqueued to the queue
50 enqueued to the queue
Is the queue empty? false
Is the queue full? true
10
20
30
40
50
10 dequeued from the queue
20 dequeued from the queue
30
40
50
Front element: 30
Rear element: 50
Time Complexity: O(N)
Space Complexity: O(N), as the queue is of size N.
Similar Reads
C++ Program to Implement Queue using Array A queue is a linear data structure that consists of elements arranged in a sequential order where one end is used to add elements, and another for removing them which results in the FIFO (First-In First-Out) order of operations. In this article, we will learn how to write a program to implement queu
8 min read
JavaScript program to implement queue using stack A queue is a First In First Out (FIFO) data structure, in which the first element added to the queue is the first one to be removed. The different operations associated with Queue include Enqueue, Dequeue etc. A stack is a Last In, First Out (LIFO) data structure, in which the last element added to
3 min read
C Program to Implement Circular Queue A circular queue is a linear data structure that follows the FIFO (First In, First Out) principle but connects the last position back to the first, forming a circle. In this article, we will learn how to implement circular queue in C programming language. What is a Circular Queue in C?In a circular
5 min read
C++ Program to Implement Circular Queue In C++, Queues are a fundamental data structure in computer science which works on the principle of FIFO (First In, First Out). They can be implemented using both array and linked list. A circular queue is a type of queue in which the last element is connected to the first element, forming a circula
7 min read
How to implement Stack and Queue using ArrayDeque in Java ArrayDeque in Java The ArrayDeque in Java provides a way to apply resizable-array in addition to the implementation of the Deque interface. It is also known as Array Double Ended Queue or Array Deck. This is a special kind of array that grows and allows users to add or remove an element from both si
6 min read
Java Program to Implement the Queue Data Structure Queue is the fundamental data structure that follows the First-In-First-Out(FIFO) principle where the element that is inserted first is one that gets removed first. Imagine the queue of the people waiting in the line at the ticket counter: the person who arrives the first gets served first and so on
4 min read