After, writing about STACKS, the positive feedback, and all the support and nice DMs I got on Instagram and Twitter has made me turn this into a series, yeah you read that right.
I hope you reading this enjoys it. Let’s go…🤓🤓🤓
In today’s blog article, we would be talking about queues!
What is a queue A queue is a data structure that follows the first in first out (FIFO)principle.
Example: people standing in a line (first come first serve) to get groceries from a grocery store, etc.
Queues are very similar to stack, but instead of using the LIFO principles like stacks do they use the FIFO principle which we would implement as we go on.
Adding an item to the queue is commonly known as enqueuing and removing an item from the queue is known as dequeuing.
push() is used to enqueue while shift() is used to dequeue.
Let’s create a queue
Let’s write some codes, shall we? As usual, we start with the basics and declare a class using an array in the constructor property of our class.
// Queue class
//Methods to be implemented go here
Let’s implement each method for our queue class.
Enqueue: This adds a new item to the back of the queue, this is similar to standing in a line (queue) to get items from a grocery store.
//enqueuing items onto the queue
Dequeue: This removes and returns the first item on the queue, this is the first come first serve.
The first person to get to our imaginary grocery store is the first person to be attended to.
//remove items from the queue
Front: This method returns the first item from the queue, but it does not modify the queue.
In our imaginary grocery store, let’s imagine that the grocery store attendant wants to know who is first on the queue.
Note that he has not attended to this person yet in our case has not modified the queue.
He just wants to know who is first on the queue.
//returns the first item on the queue
returns false if the queue contains items or is greater than 0, and
returns true if the queue is empty.
In our imaginary grocery store, let’s imagine that the grocery store attendant wants to know if there are more customers to attend to, if there are customers, that means the queue is not empty so as a result, we get false.
But if the grocery attendant has attended to everyone in the queue, then that means the queue is empty so as a result, we get
this.items.length == 0;
Size: This gives us the number of items in our queue.
Back to our imaginary grocery store where we have a queue of customers.
Let’s imagine that the grocery store attendant for some reasons best known to him wants to know how many people he is attending to at the moment (people on the queue) then he will have to count them right? Yeah.
//check the number of items on the queue
Just like when we implemented the STACKS class. We could go a step further to add a print method to help us print all the items in the queue whenever we want.
//log all the items in the queue
console.log(items to String())
Let’s use the queue class First, we have to instantiate the queue class we created
//instantiating the queue
let queue = new Queue()
Next, we can add items to our queue (since we are using the imaginary grocery store queue we would use real peoples names and enqueue them unto our queue. Let's use Vivian, Gideon, and Shalom)
// pushing a new item (customers) to the queue
Next, we can go ahead to check if items are on the queue (checking if anyone is on our grocery store queue)
Next, we call the
front() method, by doing so we would get
Vivian because she happens to be the first person in the queue.
Now, we want to check the size of our queue to see how many items (customers) are on it.
Let’s print out all the items in our queue
//returns [“Vivian”, “Gideon”, “Shalom”]
Let’s remove items from the queue.
In our imaginary grocery store, we have to attend to our customers right? Yeah! If that be the case this has to happen in a first come first serve format (FIFO)).
Let’s do this
//remove each item from the queue
if we run
we see that
Gideon has left the queue (our first two customers have been tended too) so we have only
shalom to attend too.
Let’s not waste his time let’s attend to him, so once again we run the dequeue method of our queue class
To confirm our last action we can once again check if our queue is empty. This should
Whenever a new tab is opened in a browser a task queue is created.
This is because of something we call the event loop and in the event loop only a single thread handles all the tasks for a single tab.
Once again as always thanks for being with me till the end.
Next, I would be writing about a very popular kind of queue implementation or maybe a linked list.
Well, I don’t know which to write about if you could make that choice for me I’d really appreciate you could consider sending a DM on Twitter or Instagram (join our 36k community members).
Cheers! Keep Grinding🧡