Check out a free preview of the full Four Semesters of Computer Science in 5 Hours course:
The "Queue Data Structure" Lesson is part of the full, Four Semesters of Computer Science in 5 Hours course featured in this preview video. Here's what you'd learn in this lesson:

- A queue is a “first-in first-out” data structure similar to a waiting line. Like a stack, queues have push/pop methods called enqueue and dequeue. They are useful for items that need to be handled in order or prioritize based on their need.

Get Unlimited Access Now

Transcript from the "Queue Data Structure" Lesson

[00:00:00]
>> [MUSIC]

[00:00:03]
>> Brian Holt: So you can't talk about a stack without talking about queue, I believe that's legally required. A stack is, I always get mess ups, I always checked last in first out. A queue is first in, first out. So a queue is just a line for any of you that don't speak British like it's literally, like you stand in line and then the person has been in line longest gets out of the line right that, that makes sense to us.

[00:00:29] It's fair.
>> Brian Holt: Yeah, similar to people queuing online. It has methods called enqueue and then it has a method called dequeue, which basically enqueue adds something to the array and then dequeue take something off the front.
>> Speaker 2: [COUGH]
>> Brian Holt: So unlike stack, you can also peak to see the next thing coming off usually.

[00:00:55] So queues are useful for other programming things. Obviously, not programming languages, because that'd be weird. But let's say, you needed to print something. That's usually done in a queue. If I added it first to the printer, I should have first rights to do it. And if a bunch of us add things to the printer, we don't want it to be last in, first out, right?

[00:01:14] Otherwise, everyone would be waiting until someone was about ready to print and then was like, no, I'm gonna print it. That would be terrible, because then you would never be able to print anything. Otherwise, Janice from accounting is going to be printing all of her documents before you can print anything.

[00:01:29] Does anyone watch John Oliver? I do, cuz Janice from accounting don't give up. Yeah, [LAUGH] that's what that's referring to. This also priority queues and this should be something more like networking where you have some packets are way more important than other packets right. And so, they need to be pushed to the front of the queue.

[00:01:49] So my classic example, cuz I worked in ethics is streaming video. You want those streaming video packets to be highly prioritized. Because if you start skipping packets when you're streaming video, you start skipping frames and everyone's really sad and then you call me and you even get mad at me and everyone cries.

[00:02:04] So you want those streaming video packets be way higher priority than your Dropbox ones, cuz your Dropbox ones can just happen whenever. It needs to sync sometime. It can be later, but I need to watch my House of Cards right now. That's what a priority queue is. So basically, you give it a priority and things with higher priorities go towards the front of the queue and then things with lower lower priority go to the back of the queue and that's the idea behind a priority queue.

[00:02:29] So where those can get a little bit hairy sometimes is if you have a lot of higher priority traffic and you have things that are in your low priority traffic, that stuff actually might never get done. You might never run out of high-priority things to do. So sometimes you have to have some sort of algorithm that's going through and say, okay, this has been in here long enough that I'm gonna start bumping up its priority until eventually it's gonna get done.

[00:02:50] Maybe.
>> Brian Holt: They have their own problems, just like everything.
>> Brian Holt: So, that's it for interfaces. Any questions about interfaces before we move on?
>> Brian Holt: It's just useful computer science knowledge to have, cuz people will say these things and just expect you. Already know what that is, which is frustrating.

[00:03:18] So hopefully, that adds some more tools to your toolbox.