Check out a free preview of the full The Last Algorithms Course You'll Need course
The "Queue Q&A" Lesson is part of the full, The Last Algorithms Course You'll Need course featured in this preview video. Here's what you'd learn in this lesson:
ThePrimeagen answers student questions regarding if having no tail means there is no node, clarification on the peek method, and why this.tail.next is being set to the new node.
Transcript from the "Queue Q&A" Lesson
[00:00:00]
>> It's a great time to ask questions if you don't feel like you quite get it.
>> In line six you're saying that if there is.
>> Six up ,sorry I use relative numbers are you saying six up right here.
>> Yeah
>> Yeah. So when there's no tail, that means that is there one note is no note at all like.
[00:00:17]
>> There's no note at all. S normally what I would do is I do something like this right. This dot length, yeah we can do that. This dot length equals one, meaning I just got done adding it. But of course what happens right here, type script is like, Hey, man, right, it can't handle that.
[00:00:38]
So therefore, I had to do that. And D queuing of course, there's one thing I didn't technically do here correctly, which is what about the tail? What happens when we remove this. Really what you should be having is if this dot length equals zero, then this dot detail should equal undefined right.
[00:00:57]
We need to make sure that we forget about it at all points in time. So will implementation detail. But so then this thing of course falls apart. It's like hey, I can't do it so I didn't want to just be encumbered by TypeScript types or it really. TypeScript just actually technically doing the right thing there we go.
[00:01:16]
And so for that, I just go knock this out there. There we go, awesome it's the exact same check right. As long as we've kept everything in nice book keeping order this should all work out.
>> Could you clarify the peak method.
>> The peak method? Yeah, so the peak method is very simple So, right now if we just take this as our linkless right here and we wanted to peek into our list.
[00:01:41]
Meaning we want to see the next value returned without mutating the state of the queue. All we're gonna do is go to our head and get the value out of the head. In other words, we need to be able to get that generic t value out. Because the implementer or the the instantiater of the queue does not care about how the queue is book keeped.
[00:02:00]
It only cares about the values that it puts in and gets out. And so we do that by doing this right here. We go to our head. If it does exist, we get the value out. If it doesn't exist, we return undefined. Good?
>> Sorry, why are we setting this top tail time next to the new note itself?
[00:02:24]
>> All right so yeah, so right here. So let's go over that operation. So that is an insertion at the end. So let's actually just do that again. So if we want to insert F into here we have to do two operations. We first have to go to the tail and set its next to point to F.
[00:02:39]
After that, our tail will still be pointing at E, which is not our true tail, right? It has to point to F. So we also have to update tail to point to F So if you look at the code, exact same thing. We go to tail and we set the next one after the tail then catch the tail up because the tail should always represent the end of the queue.
[00:03:05]
Does this look good? Awesome. All right. So what's the opposite of a queue? Stack, I know obvious. It's very obvious. Oddly enough, I've also created a lot of these things. These things are great for performance. Technically, with JavaScript arrays, awesome to use. You can use push and pop at pretty much constant time rates.
[00:03:28]
Good times.
Learn Straight from the Experts Who Shape the Modern Web
- In-depth Courses
- Industry Leading Experts
- Learning Paths
- Live Interactive Workshops