Check out a free preview of the full Data Structures and Algorithms in JavaScript course:
The "Queues" Lesson is part of the full, Data Structures and Algorithms in JavaScript course featured in this preview video. Here's what you'd learn in this lesson:

Queues are similar to a stack except for the order in which items are added. In the case of a queue, the first item in is the first item out. Rather than push() and pop() methods, a queue has an enqueue() method for adding items and a dequeue() method for removing items.

Get Unlimited Access Now

Transcript from the "Queues" Lesson

[00:00:00]
>> Bianca Gandolfo: So Queues, pretty similar stacks except kind of like the name suggest, is kind of like a queue or a line. People are talking about Ideli a lot when they talk about this for whatever reason. Everyone's like a queue is like a line for the deli. You know, the first person in line is the first person to get their sandwich.

[00:00:20] We call this FIFO. I don't like these abbreviations, but you'll see them a lot. LIFO, LEFO, FIFO. LIFO is last in, first out and FIFO is first in first out. So, they, like, super confused me. So I even just avoid even saying them, but I have them here for your reference because you'll see them.

[00:00:41] So when we add an item into our queue, we call it enqueuing. We take them off and call it dequeuing. You'll see, push, pop, enqueue, dequeue, things like these. They'll be called different names in different languages. But in general, we'll call them this in this class.
>> Bianca Gandolfo: Yep, so here, is our little line, so we're gonna go myQueue.enque(1).

[00:01:03] We're gonna add that value, so you have a little man, he has a one. And then two, this person becomes behind that person. We just dequeued them. So we just put a red, so they're gone. So we're gonna enque, enque and then deque, yeah? One more time for good measure, enque one, enque, deque, one is gone.

[00:01:33] Enque, enque Deque, two is gone. So whenever we take anything off, you can only take from the front. And when you put anything on, you can only put it on the back.
>> Bianca Gandolfo: Yeah, we're good? Okay, awesome, here's our diagram. I don't know if you guys remember a time when these pop-up windows didn't have a little checkbox, say prevent more pop-ups.

[00:02:00] Then you'd get trolled into these stupid websites where you're just like in this pop-up hell. You know what I'm talking about? So, the underlying data structure of that is a queue. And you can see, this is a pop-up handler queue as pop-ups rain Javascript, you just say alert, alert, alert, right?

[00:02:16] Loop times one bajlion. At least that's what it feels like. You enqueue on the back, and you pop from the top. So as you hit OK, it's gonna take off the front. Okay, okay, okay, so that is our queue in action, yeah? People remember that, right? Okay, good, cool.

[00:02:37] So that's our queue. What's our interface? We have our storage. We enqueue, dequeue, we can also do size. Also lots of methods where I can go on this. But these are just the standard ones. So whatever you wanna do with your queue. Like maybe you wanna make it sparkly.

[00:02:56] Maybe you wanna make it dance. Whatever it is, you can do it to your queue. Just a rule of a queue is that the enqueue and the dequeue, that's a core definition of the queue. Whatever else you're gonna do with it, great. As we are queue make things, you know, I don't know, who knows?

[00:03:15] You are the master of your queue and your stacks and everything else, great. So this is our interface [COUGH]. I'm not gonna make you do any string, don't worry. And I'm not gonna pseudo code it for you yet.