Transcript from the "Creating Stacks and Queues Solution" Lesson
>> Bianca Gandolfo: I'm gonna quickly go over some of the solution code, which you have access to. You may be asking where can I find that, Bianca? Does your link even work? Well, we will find out now. Here's we go. Are you ready? One, two, three. Here we are. You might notice it looks almost exactly the same except this is a solution branch from that repo.
[00:00:25] Feel free to do pull requests to this branch if you wanna add different solutions, that's always cool. This is an open source repo, so feel free to contribute. All right, so where were we? We were at stacks. What do you want to do first, stack or cue? Which one did you guys do first?
>> Speaker 2: Do stack first.
>> Bianca Gandolfo: Stack first? All right, let's take a look. All right, so here's just our stack implementation. So we have the ability to set a capacity if you'd like, this will allow your stack to overflow. So we have our storage, we have an empty object, and then we have a counter in here.
[00:01:15] So we have our push, it takes a value. If this._count is less than the capacity, right? This is a software overflow. Then you're gonna increment count, or actually you're going to, it starts at 0 and then it increments after. And we're gonna add it to our storage, yeah?
[00:01:41] This look familiar to how you guys did it? Anyone do it differently?
>> Bianca Gandolfo: Anyone do it differently? Everyone get to this part? Thumbs, middle thumbs, down thumb, okay, a lot of people didn't. Okay, no worries, I'll just kind of review. And then remember try to do it on your own but don't look at the solution at the same time.
[00:02:04] That's my only request. And I say that because I care about you and your effective use of your time. Cool. So our push. We are gonna just check that we're not overflowing. If we aren't then we're just going to use our counter. And save it there. We're gonna return this._count.
[00:02:27] This is the same implementation the the array does, so if you push into an array, it's gonna return the length of the array. So, that's why we're doing that. And then otherwise, we're like stack overflow, stop, right? So, we have pop. Here we are. We are saving the value here.
[00:02:50] We're gonna delete it. And, again, this._count is gonna have a reference to the last index plus one. Yeah, thumbs on what I mean by that. Okay, we're gonna decrement it first, that's why the minus minus is at the beginning. It decrements first so if count is five before we run this line, it's gonna be four.
[00:03:19] Yeah, this could be confusing to people when the decrement is at the beginning versus sometimes you see it at the end. The beginning it decrements first, at the end it decrements after. So it'll return a number and then decrement. All right, so we have two things that you can do in this moment.
[00:03:39] One, you can repeat what I just described or two you can ask me a question.
>> Speaker 2: Repeat.
>> Bianca Gandolfo: Yeah, let's hear it.
>> Speaker 2: Okay so if you put the incremeter or the decrementer in front of the value, it will use the value after that has happened to it.
[00:04:01] If you do it after, it will use the value and then do that.
>> Bianca Gandolfo: Yes, awesome. We're good on that? You have a thing and you're doing this with your eyebrows. Is that how you just look or are you thinking.
>> Speaker 2: [LAUGH]
>> Bianca Gandolfo: [LAUGH]
>> Speaker 3: I don't know, I guess I'd rather avoid, seems like you're asking for bugs if you're using features, but okay.
>> Bianca Gandolfo: Yeah, it'a a stylistic choice for sure. But this, so this is like I'm judging you and your coding style or I'm confused.
>> Speaker 3: Yeah, I'm judging you. [LAUGH]
>> Bianca Gandolfo: Okay, that's fair.
>> Bianca Gandolfo: All right, cool. All right, I will say it one more time. Because the live streamers say so and so I will do it.
[00:05:28] So each of these expressions is a shortcut that does two things. The first thing it does is it either adds or or it minuses one. The second thing it does is it returns a number. The difference is between the order in which those operations happen. So for this one, where the operator is to the right of the variable, it is going to return the number first, and then it's going to either increment or decrement.
[00:06:02] So for example, if x ==3 at the beginning, yeah? Or let's say 4. Then we will, so it'll be 4 and it will return 4 and then it will decrement it, and it will be 5, yeah? Cool. For example for this one the number in the brackets if this is in some brackets will first be four and then the next time you loop around it'll be three, yeah?
>> Bianca Gandolfo: Great. And the difference here, for this one, is that first it's gonna do the operation, and then return it. So this will actually return 5, while this one returns 4.
>> Bianca Gandolfo: Yeah, and this one will return 3, while this one still returns 4.
>> Bianca Gandolfo: Cool.
>> Bianca Gandolfo: All right, great.
>> Bianca Gandolfo: Okay, where were we? Okay, so, we're decrementing it first.
>> Bianca Gandolfo: Then passing in the integer. Yeah, okay. Then we delete it and then we say if this._count is less than zero, make it zero and you're gonna return the value. Cool. This is for the case where we pop off the last one.
>> Bianca Gandolfo: Cool, so this is peek. And this is just gonna show us the last one that's about to pop. So this is if you want to look at the last one but you don't want to pop it off. Count, that's easy, return this._count. And then we have the MinStack which I'm not gonna give you a walk through because a lot of you guys haven't done it yet.
[00:08:10] But you have the code here. It's really fun. It's not something you're gonna do in real life. We were just talking about this in the chat. You're not gonna ever, well, actually I'm thinking of a different one. But we were talking about something else. Nevermind. Anyway, MinStack, I want you guys to implement it before I show you the solutions, okay?
[00:08:35] Cool, any questions about this?
>> Bianca Gandolfo: This is pretty straightforward. So who here kind of had a gap? So I was just talking with Maurice a few minutes ago, and we were talking about this sort of gap when that happens, when I'm talking about this thing, talking about this thing, theoretically it sounds great.
[00:08:57] But if you've never actually implemented it before, it was like, God, I understand it. But then when it gets to the code, there's a little bit of a difficulty moving through. Anyone else experience that? Yeah? Cool. So this is the learning moment, right? This is what you're here for.
[00:09:17] It's like, that's the part where you're like, this is where I haven't done this thing before, and I'm gonna grow by pushing through that limit. So that's awesome. If you're already experiencing that, your brain is gonna be like whoa through all of that, so that's exciting. All right, so where are we?
[00:09:36] We want to look at queue. We'll do this pretty quickly. It's not super, super different than before except for, in this case, we're gonna have a head and a tail. And I think that's gonna be for a different implementation. So we're gonna enqueue, so we're gonna add something to the list.
[00:10:02] So if it is not above the capacity, you're gonna add the value. And then you're gonna increment the tail. Return this._count, otherwise you reach the max capacity. Really similar to our other one. Great, so when we DQ, again, we need to save our element. We have our pointer to a head.
[00:10:29] We're gonna delete it. And then if the head is less than the tail, increment the head and return. Peak very similar. Then to the other one, count as well. It's gonna be the difference between the tail and the head. Awesome. Everything else you should spend time implementing on your own when you get a second later today, cool?
[00:10:55] And we'll go through it tomorrow morning. All right, any questions about this implementation?
>> Speaker 3: Why do we use underscore before?
>> Bianca Gandolfo: Yeah, why don't we use underscore? So that means it's basically like a private variable. We don't really have this concept of a private variable, it's basically telling other engineers, hey this is my interface that I created.
[00:11:23] Don't mess with this directly. Only mess with it in the interface that I created which is with these methods. Does that make sense? It's like saying it's private, but it's not actually private. You could change them anywhere, but it's a mark to not do that. Do you have something?
>> Speaker 2: A remark on the chat which I think you might address. Victoria's asking or saying, I don't know what O(1) means. So I'm guessing, that is what I came for was to learn what that Greek complex trig looking syntax means.
>> Bianca Gandolfo: Yep, so yep that's big on notation and this is here just for your reference.
[00:12:07] But we'll get into that, maybe by the end of the day but probably first thing tomorrow depending on how things go. Yeah, we're gonna get into that. Right now we're just one step at a time, yeah.