![](/style/images/good.png)
![](/style/images/bad.png)
Queues in Javascript
source link: https://medium.com/@dcortes.net/queues-in-javascript-c40a9fe6baac
Go to the source link to view the article. You can view the picture content, updated content and better typesetting reading experience. If the link is broken, please click the button below to view the snapshot at that time.
Queues in Javascript
![1*_jLMsU1kyQ2EZXFg7b7PQQ.png](https://miro.medium.com/max/700/1*_jLMsU1kyQ2EZXFg7b7PQQ.png)
Introduction article to the data structure. Queue concept and examples applied to the Javascript language.
Introduction
Data structures are a means of handling large amounts of data efficiently, Knowing its structure and composition gives us more effective tools to design our product in relation to certain problems. On this occasion we will perform a queue introduction, reviewing its peculiarity and use.
Queue concept
Queues are a dynamic data structure that allows elements to be stored and retrieved based on a structure FIFO(first in, first out). The given name comes from the analogy of a client queue in the bank. Customer who arrives first will be served first, and the one who arrives later will be placed at the end of the queue and so on.
![1*MYHLFnwt0bxI7xIgFZtQbA.jpeg](https://miro.medium.com/max/700/1*MYHLFnwt0bxI7xIgFZtQbA.jpeg)
Implement queue in Javascript
To implement a queue in Javascript we will rely on the repository https://github.com/trekhleb/javascript-algorithms that contains an excellent implementation, but we will simplify some methods and structures to be able to understand its operation. We will support everyone in object-oriented programming.
Queue class
Class to represent a queue, this class will have the property called items
that will represent a set of elements to store (in this case we will use an array of elements, but it can be of any other type, for example, objects or sets. The most important thing is to encapsulate the behavior). At startup, the items
property will have no elements.
class Queue{
constructor() {
this.items = [];
}
}
the next steps will be to add to class Queue
methods that will help us to make the manipulation of elements in our queue.
Method to insert elements
To insert items into our queue, we create the common method called enqueue.
enqueue()
enqueue(element) {
this.items.push(element);
}
![1*OoGlmoUEa5CdojDDQSBFgw.png](https://miro.medium.com/max/700/1*OoGlmoUEa5CdojDDQSBFgw.png)
Element removal method
to remove elements in our queue, we will create the common method called dequeue. This method aims to dequeue elements in our queue.
dequeue()
dequeue() {
return this.items.shift();
}
![1*h7J1oFWI7a5foGDTySJ4KA.png](https://miro.medium.com/max/295/1*h7J1oFWI7a5foGDTySJ4KA.png)
Element display method
Another more common queue operation is the display of the element that is about to exit. This method is commonly called peek.
peek()
peek() {
return this.items[this.items.length - 1];
}
Full code
Ending
In this article we deal with queues in a simple way, however, these methods should be enough to cover the basic use cases. Of course, there are many ways to extend and refine our example. I recommend to continue exploring implementations and adapt them to your need.
Thanks for coming this far, if you find this useful don’t forget to clap 👏 and share 😀. Subscribe to receive more content 🔔.
If you need additional help, please contact me 🤠.
Thank you very much for reading, I appreciate your time.
Recommend
About Joyk
Aggregate valuable and interesting links.
Joyk means Joy of geeK