 Hello everyone, so we'll talk about our third and final abstract data type that uses an array-baseless data structure And that is the deck or double-ended queue. No, it's not DQ. It is a deck and a deck is actually it's It's kind of an esoteric little ADT doesn't get used in a whole bunch of applications But nonetheless, it still exists and it's not too hard to understand what it is A deck is an ADT where addition and removal Happens at both ends right and you can say that it essentially combines the behaviors of a stack and a queue It has a notion of a front and a rear Just like a regular queue does but you are allowed to add to the front Remove from the front add to the rear and remove from the rear and that's it But you know what differentiates this from a list The differentiator is you can only operate at either end You have more flexibility in what you do at either end But the operations all happen at the end. You don't work in the middle You can't get random elements out of the middle of this thing. Okay? there are some Reasons for using this It's because one area where you see it a lot is CPU process scheduling Right, so you have a whole bunch of processes that are running on your computers for all your different apps that you've got running right now and your operating system decides who gets to go, right and Oftentimes the operating system puts those things in a queue all the apps that are running on your computer Demands some processing power from your your computer's brain from the CPU But your computer's brain can only handle one thing at a time. It can handle them very very fast about three billion times per second But it can still only handle one at a time and it's up to your operating system to determine Which one of the hundreds of applications that are currently running right now actually get the that valuable processor? attention and CPU process scheduling usually it happens in a queue first come first serve But every once in a while something really important comes along and it needs to go first it needs to go to the front of the queue And that's where you see a deck show up, right? Like if you hit control I'll delete on Windows and you want that keyboard stuff to go through and The stuff to pop up immediately. Yeah certain things have higher priority. Okay, so decks exist But the good news is if you understand the concept of a queue, it's pretty easy You have a front you have a rear you can add and remove from the rear and you can add and remove from the front That's it the middle. We don't worry about So let's just make sure we understand conceptually what a deck is Let me switch back over here in my way for it Okay So now we're looking at this page of the worksheet the deck page on the worksheet and let's walk through the operations It's going to look like a queue. Okay, the things that we do so step one We initialize kind of an empty deck nothing there, right? Okay, step two add to the rear Line line is going to be the rear of the queue Well, so are the rear of the deck excuse me So just like a queue a deck needs to have a notion of a front and a rear Now when there's only one item in a queue or a deck the front and the rear are both that one single Okay, if the deck or the queue is empty Front and rear don't exist, right? There's there's kind of like an error state there If you try and access the front or the rear of an ID queue Or deck so when we add to the rear here both the front and the rear are pointing to our In reference to this line. Okay, same will be true if we added front here Right. Okay. There's one item in a queue or a deck. It is both the front and the rear Okay. Now deck step two We just did step two step three We are going to add to the front Add front Bear now add rear add front remove rear remove front those are all methods You will write in your deck class How you in you will implement them using a Python array based list So it's going to look a lot like your queue implementation, but with a couple of extra things All right, so we are adding to the front the bear Move my little step indicator a little bit. So let me put the bear in front Okay, and we need to Move the front here Okay So we've got the bear Then the lion And the bear is front Lion is the rear. Okay. Again, think of it Like a line at a grocery store or a line to get on the airplane Some people can go first and they get added to the front. They are specials. Okay All right Step four adding to the rear. Okay, so we're in line at the airport and the bear gets on the plane first Could get on the plane first the lion gets on the plane Next and now comes the hog step four add to the rear the hog The hog goes at the end and now Conceptually the rear of this list Is here All right, we've just done step four All right, step five. We remove from the front Step five you really see this on the screen Step five we remove from the front Okay, who's at the front? Well conceptually We've defined the front to be here. So the bear where you're going to remove and return the bear And when we remove from these data structures, we are usually returning them as well. Okay, so remove and return So the bear goes away Who is now the front? front is now The line step six Remove from the rear. Okay, who is the rear? Conceptually the rear is the hog Okay, so the hog gets removed and Return hog is gone. He got tired of waiting in line. So he left. I can happen, too Want to get a drink who is now the rear of this deck? Well, there's only one element in it and that's the lion. Okay, so the front and the rear You know as you add front as you add rear as you remove from the front remove from the rear You just got to know conceptually where's the front? Where's the rear and add or remove? You know consistently from whatever a location you specify. Okay? That's about it. It's just a queue on steroids All right, let me briefly go back here to my slides. Okay So Here's the big O's of these operations and like we had let me get rid of myself Like we had with the queue It when we use an array based list it as our data structure for Implementing our deck data type. We have to make a choice in our list our Python list Where's the front? Where's the rear? Okay, is it the rear is the front index zero and the rear index minus one or is the rear Index zero and the front index minus one. It's up to you. You will pick There's no right or wrong answer for a deck or a queue. You just have to pick one and use it consistently and just like with the queue You some of your operations will be big O of one and some will be big O event Which ones are big O of one or n? Depend on whether you choose the front Index zero or index negative one as the front of the queue and the deck Okay, so what what's important here is that you understand why When you have a data structure, that's a Python array based list You're picking one is the conceptual front or conceptual rear of a deck or a regular queue You need to understand why One of those things working at one of those ends is big O of one versus big O event Okay, so convince yourself of why that is and make sure you understand it because you will definitely get questions about like what's the efficiency of queue dot Anqueue or deck dot remove rear when your data structure treats Index zero and the Python list as the front And what's it going to be as big O once a big O event you got to know it and don't memorize it Be able to figure it out based on what you know about array based lists. That's the key You've got to know the efficiency the big O efficiency of those array based list functions Everything else is going to follow from that. So as I mentioned last time, I'll bring myself back for a minute queue and deck operations We care about adding and removing from these data structure or these data types. Excuse me in These ADT's abstract data types one of these operations adding or removing must be big O event Because we use a Python list as our data structure must be must be the case because that's a limitation of the Python data structure Okay, so We could make adding and removing from a queue from both ends of a queue they go one but We have to change we cannot use a Python list Internally we have to take it up a notch. We got to implement our own data structure and we will next time so Have a good look over these You now hopefully have all the knowledge that you need to work on your next assignment Where you're going to implement the stack the queue and the deck ADT's and then you are going to write a little program that uses your Stack to solve a problem Let me know a questions you have and I will see you next time