 Hello, everyone. So in this video, we're going to talk about another abstract data type called a stack. Okay, stacks are pivotal in computing. They're used all over the place inside your computer, inside your computer's operating system for accomplishing different goals. And you've already learned one abstract data type, which is the unordered list. And this is kind of the list you're used to. Things in a particular order. A stack is basically a specialized list where you do all the work at one end of the list or the other, okay? And it turns out this is really useful. And the reason we're going to come up with stacks, and the reason we use stacks, is we only care about a tiny subset of the operations that you would normally find in a list. So this kind of simplifies things in a lot of cases. Okay, so what in the heck is a stack? Okay. So a stack or a push down stack is an abstract data type, ADT is abstract data type, where addition and removal of items always happens at the same end, which is called the top of the stack. Okay, so when I say stack, you should think of a stack of pancakes, right? When you're eating a stack of pancakes, you eat from the top down unless you're some sort of animal. And when somebody brings you another pancake, they put it on top of the stack. You don't lift up the gooey stack of pancakes and try and slide it in the middle, right? So a stack, again, it's an arrangement. It's a collection of items, but you only ever care about the top item, okay? And it turns out that's it's actually a useful model of things. You have been working with a list that is much more generic, much more diverse, a list where you can access any element at random. The first one, the last one, one in the middle, you can search through it. Lists are actually pretty sophisticated in what they can do. Stacks are much, much, much simpler. Now again, stacks are abstract data types. You can find them in any programming language, but the implementation of a stack, well, that's up to you. That's up to you, the programmer. How do you want it to look? Python does not have a notion of a built-in stack. We have to make one and we will make one and I don't know why Zoom popped up, but there we go. All right. So again, we are working at one end of a collection of items and we call that end the top, the top of the stack. A stack has last-in, first-out behavior or LIFO behavior. So this means usually what we're doing with stacks and computing is we're adding things to them and we're taking them away. Okay. I'll show you in a second an example of what we use this for, but you add to a stack and you take away from it. But the way the stack works, the hallmark of it is that the last thing you added, the thing you put on top of the stack is the first thing that comes out. Always. That is the defining characteristic of a stack. Okay. So it's pretty simple in concept. It's much simpler than a list. It's just a collection of items where we add and remove from this so-called top. And that's it. Okay. That's all there is to it. It turns out, so again, the last in, last thing on top, first thing out, nature of stacks, means that items are removed in reverse order from the order in which they were added. That's what a stack does. And that's actually something that we wind up needing a whole lot in computing. So you have used stacks, whether you knew it or not. The back button in your web browser. Think about it. The back button, the last page you visited is going to be the first one you go back to. Two pages that you visited ago. That's two back buttons, right? So what happens is whenever you visit a page, it puts it on the stack and then you go somewhere else and it puts it on top of the first one. And then you go somewhere else and it goes on top. And then when you click the back button, it's popping them off the stack one by one top down. Same thing when you use an undo function and like pie charm or your word processor, it undoes the last thing you did. So these are all accomplished using stacks. Now stacks play a very important role in processing programming languages. Things like matching of braces, of parentheses, of square brackets, of angle brackets in programming languages. That's accomplished using a stack as well as just general processing of the language. If you're a computer science major, you'll wind up taking a programming languages course later on and you'll get into the details of how a stack is used there. OK, so stacks are quite useful in computing. OK, they have a few behaviors as we define our abstract data types. Remember, we're talking about abstract general conceptual things that you can implement in any language. We will always define the data items and the behaviors for our data type abstract data type. OK, so a stack is pretty simple. It's got a constructor. You push items on top of the stack. That is the terminology you push an item on top of the stack when you want to remove an item, you pop it. OK, that takes the top item off the stack and returns it, gets rid of it and returns it. Remember, there's a list method called pop. That's where the term comes from. There's a peak method where you just tell me what's at the top of the stack, but do not remove it. Just tell me what's up there. This is often useful in applications. And then two utility methods that are probably going to be on almost every abstract data type we implement. Is it empty? Yes, true or false? Is there anything on the stack? Oh, and what is the length of the stack? How many items have been added to it? OK, now, typically, the data structure that we use to implement a stack is some sort of list. OK, an array based list is the most common. OK, and that's what we're going to use. But before we get into the code, all right, let's talk about just kind of make sure we understand what happens when you call different stack operations. Now, again, it's going to be up to us to implement these operations in Python. And you're going to have to do that for your next homework assignment. But let's make sure we understand what these operations do. OK, so there's a worksheet that accompanies this. Got a pancake stack of pancakes at the top. OK, it's going to trace through what happens. And this is something you need to understand for all of our data structures that we talk about and abstract data types from here until the end. You need a really good mental model of what these things look like, OK, in your head. It's going to look different than the code, but you've got to have a good mental model of it. All right, so the mental model of a stack. Let's make sure we've got it down by working on this worksheet. All right, I'm going to switch to my whiteboard here. OK, and I'm just going to go down from one to seven, steps one to seven. OK, I'm going to draw up here kind of the mental model, the visual model of the stack. OK, so step one is in step one, it's a constructor call. We initialize an empty stack. So how do I show empty, you know, I'll just put something like that, something that looks like an empty list. OK, now step two, we're going to be visual here. Stack dot push, what does pushing do in a stack? It adds to the top of the stack. Well, our stack is empty currently. When I push onto an empty stack, the value I push is both the top and the bottom of the stack. All right, so what do I push? I am pushing Achilles onto my stack. OK, so my stack is going to be stored. I'm saying it's in this variable stack and on the bottom is Achilles, OK, just like that. You know what, let me move everything down a little bit. I'm going to need some room because stacks, conceptually, they grow upward. When you add things to the stack of pancakes, the stack gets taller, right? So let me move everybody down here a little bit. OK, so after my first or my operation number two here, my stack is just Achilles. He's the top of the stack and coincidentally, he's the bottom of the stack. He's the only thing in the stack. OK, step three, I'm going to push Helen onto the stack. Now, conceptually, what does this look like? Achilles is still in the stack, but on top of him is now Helen. Nice. OK, so I've pushed Helen onto the top of the stack. OK, Helen is the top and why don't we note that the top of the stack, right? Where the top of the stack is, is important, right? OK, step four, I'm going to push Hector onto the top of the stack. Now, when I push, I am not removing anyone from the stack. They stay there, removing them as a different operation. OK, so I've added Hector onto the stack and Hector is now the top. And we'll write code to realize all of this stuff in a couple of minutes. But conceptually, let's just make sure we're on the same page. Pushing adds things to the top of the stack. OK, now, step five, stack got peak. OK, what does peaking do on a stack? Back a slide or two, peaking on the stack just says, tell me who's on top, tell me that value, right? So when I peak, I get the value Hector back because he is the top of the stack. But Hector is not removed from the stack. Not yet. OK, we're just looking. I want to know who's up there. So I peak, push and peak. That's what we've looked at. All right, step six, now I'm going to pop. OK, so popping removes the item from the top of the stack. OK, removes and returns it. And that's key. You've got to remember that popping removes the top of the stack and returns it. OK, so in code, you can do something with that value that you just removed. OK, but the top Hector is no longer the top. But there is still a top. It's the next person down who just happens to be L. OK. So popping removes and returns. Now I'm going to push again. Step seven on your worksheet. Step seven, I push Paris. OK. All right, where does he go? Pushing goes to the top of the stack. So Paris will be up here. The top moves to Paris and now you've got to throw some more. OK. So there you go. This is what it means to be a stack. OK. Now again, imagine this in the context of going backwards in a web browser. You might have the first place you go is Google here. Then to Instagram and then to Canvas. And then you hit the back button, you get you back out of Canvas and you go to Instagram and you back out of Instagram. You go to Google. OK, same thing. Stacks can grow as big or as tall as you want. The key is we're kind of simplifying the idea of a list of a collection of items by only working at one end of the stack. OK. So let's kind of switch back here to my code.