JavaScript Program to Implement a Queue
In this article, we will explain how to create a queue using JavaScript. A queue is a collection of elements that follows the "first-in, first-out" (FIFO) principle. In simple terms, the element that is inserted first will be the first one to be removed.
Whether you're a beginner or an experienced developer, this article will help you understand queues and how to implement them in JavaScript. So, without further ado, let's dive into the world of queues and make your code more efficient and organized!
We also have an interactive JavaScript course where you can learn JavaScript from basics to advanced and get certified. Check out the course and learn more from here.
What is a Queue?
Queue is also an abstract data type or a linear data structure, just like stack data structure, in which the first element is inserted from one end called the REAR(also called tail), and the removal of existing element takes place from the other end called as FRONT(also called head).
This makes queue as FIFO(First in First Out) data structure, which means that an element inserted first will be removed first.
Which is exactly how queue system works in real world. If you go to a ticket counter to buy movie tickets, and are first in the queue, then you will be the first one to get the tickets. Right? The same is the case with Queue data structure. Data inserted first, will leave the queue first.
You can read more about the Queue from here.
How to Implement Queue?
In the program given below, the Queue
class is created to implement the queue data structure. The class includes methods like enqueue()
, dequeue()
, peek()
, isEmpty()
, size()
, and clear()
.
A Queue
object is created using a new
operator and various methods are accessed through the object.
- Initially,
this.items
is an empty array.
- The
push()
method adds an element to this.items.
- The
shift()
method removes the first element from this.items.
- The
length
property gives the length of this.items.
Program to Implement a Queue
// JavaScript Program to implement queue data structure
class Queue {
constructor()
this.items = [];
// add element to the queue
enqueue(element) {
return this.items.push(element);
}
// remove element from the queue
dequeue() {
if(this.items.length > 0) {
return this.items.shift();
}
}
// view the last element
peek()
return this.items[this.items.length - 1];
// check if the queue is empty
isEmpty()
return this.items.length == 0;
// the size of the queue
size()
return this.items.length;
// empty the queue
clear()
this.items = [];
}
let queue = new Queue();
queue.enqueue(1);
queue.enqueue(2);
queue.enqueue(3);
queue.enqueue(4);
console.log(queue.items);
queue.dequeue();
console.log(queue.items);
console.log(queue.peek());
console.log(queue.isEmpty());
console.log(queue.size());
queue.clear();
console.log(queue.items);
[1, 2, 3, 4]
[2, 3, 4]
4
false
3
[]
Conclusion
In conclusion, implementing a Queue using JavaScript program is a great way to improve the efficiency and performance of your application. We've covered the basics of Queues, how to create a Queue in JavaScript. By using a Queue, you can easily manage and process multiple requests simultaneously, making your website more responsive and user-friendly.