JavaScript program to Implement a Circular Queue using Arrays Last Updated : 22 Apr, 2024 Comments Improve Suggest changes Like Article Like Report 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 QueueEnqueue: 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? trueIs the queue full? false10 enqueued to the queue20 enqueued to the queue30 enqueued to the queue40 enqueued to the queue50 enqueued to the queueIs the queue empty? falseIs the queue full? true102030405010 dequeued from the queue20 dequeued from the queue304050Front element: 30Rear element: 50Time Complexity: O(N) Space Complexity: O(N), as the queue is of size N. Comment More infoAdvertise with us Next Article JavaScript program to Implement a Circular Queue using Arrays G gfgking Follow Improve Article Tags : JavaScript Web Technologies javaScript JavaScript-Questions JavaScript Programs +1 More 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 When to use Queue over ArrayList in Java? Array List:ArrayList is a part of the Collection Framework and implements the java List Interface. This class provides the methods to resize the list based on needs.It allows having null and duplicate values.The Array List is similar to a vector in java except that it is unsynchronized. It is not th 6 min read How to manage Full Circular Queue event? What is a circular Queue?A circular queue is a non-primitive, linear data structure. It is an extended version of a linear queue. It is also called a Circular buffer or cyclic buffer. It offers a quick and clean way of storing First First Out (FIFO) data with a maximum size. Mainly it is used in mem 12 min read Queue Implementation Using Linked List in Java Queue is the linear data structure that follows the First In First Out(FIFO) principle where the elements are added at the one end, called the rear, and removed from the other end, called the front. Using the linked list to implement the queue allows for dynamic memory utilization, avoiding the cons 4 min read Difference between PriorityQueue and Queue Implementation in Java Java Queue InterfaceThe Java.util package has the interface Queue, which extends the Collection interface. It is employed to preserve the components that are handled according to the FIFO principle. It is an ordered list of items where new elements are added at the end and old elements are removed f 5 min read Like