Four Semesters of Computer Science in 5 Hours Stack Data Structure
This course has been updated! We now recommend you take the Complete Intro to Computer Science course.
Transcript from the "Stack Data Structure" Lesson
>> Brian Holt: Let's move on to stacks. Stacks are the interface and they are last in first out. So the last thing you put in is the first thing you get out of it, and you can't get anything else out of it. So if I if you look at the diagram that there is right here, it may be a little bit bigger.
[00:00:25] So if I have 1 here on my stack and I push on 2, I can no longer get at 1 until I bump off 2. And the same thing if I push on 3 here, now I can't get a 2 or 1, I can only get a 3m, and so I keep pushing things on.
[00:00:40] So this is where the terms push and pop come from, it's stack terminology. You're pushing things onto the top and then you're popping it off the top. And a pure stack, a thing that is just a stack will only let you get things off the top. Now we kind of borrow that in arrays because why not?
[00:00:58] It's really useful and it's really descriptive. But arrays have a stack interface, but they are not stacks because they aren't strictly adhering to that only push, only pop. There's also a peek, peek is like let me see what's on the top without pulling it off, just FYI. So you use stacks all the time, because you're programming on a stack.
[00:01:28] Like here, I just used the term stack overflow. Well what is stack referring to? It's because you're programming on a stack. So let's take an example here. I had this function called maths very descriptive, good job, Brian. And inside of maths I called double, and inside of there I called SquareAndAddFive and then I return the answer.
[00:01:52] So let's actually draw out what that stack is gonna look like.
>> Brian Holt: So come here, you have like your main thread running. And then on top of that I'm gonna push on maths
>> Brian Holt: And then inside of maths, so inside maths now, I'm going to push on double
>> Brian Holt: And then it's goes into this double function which just does something then returns. So now I'm going to pop off double
>> Brian Holt: And then I'm gonna call the squareAndAddFive. So put that on there.
>> Brian Holt: Square whatever, square plus 5, you get what I'm talking I'm talking about?
[00:02:51] So then it's gonna go into the squareAndAddFive and it's gonna call square and there. So that gets pushed onto the stack, and then square does something and returns. So then that gets popped off, then squareAndAddFive finishes so it gets popped off, and we're back in maths, and maths finishes and that gets popped off.
[00:03:14] So programming is a stack, that's why we call it a stack because we're pushing functions on or popping them off and like when you're reading code like that just intuitively makes sense to you. You wouldn't click finish double and then well I added math first so you're gonna go out of that relic.
[00:03:31] You wouldn't go any other way like that doesn't make any sense. So anyway, stacks are very useful for modeling some things particularly programming languages. Any questions about stacks?
>> Brian Holt: Cool.
>> Brian Holt: Let's go with that. Yeah, I basically described what goes on right there. So
>> Brian Holt: Any modern C based programming language and I would extrapolate that to almost every other language is based on some sort of stack based architecture cuz that's what makes sense to us.