Check out a free preview of the full Data Structures and Algorithms in JavaScript course:
The "Stacks Interface" Lesson is part of the full, Data Structures and Algorithms in JavaScript course featured in this preview video. Here's what you'd learn in this lesson:

The interface for a stack contains a constructor function for handling the storage along with push(), pop(), and size() methods for manipulating the stack. After introducing the interface, Bianca share an example of the methods defined in JavaScript.

Get Unlimited Access Now

Transcript from the "Stacks Interface" Lesson

[00:00:00]
>> Bianca Gandolfo: So our interface. So we have our constructor function. Inside of our constructor function we need a storage. We need to store our data whether it's a reference to our functions, whether it's a list of all of our cats or whatever it may be. We need to have a storage and then we have our methods, right.

[00:00:19] We have push, we're pretty familiar with this interface, right? And this is why it's a pretty common first data structure to encounter. Push, we're familiar with that. Push means you're gonna put a value, the top or on the back, right? Top takes it off the back and size is gonna tell you the size of our stack, right.

[00:00:42] So in the world where we are talking about stack frames, it will count the stack frames. In the world where it's an array of our cats in order from our favorite cat to our least favorite cat. Probably based on how similar they based on how similar they are to dogs.

[00:00:58] That's how I like cats. If that cats more like a dog, they're more my favorite. And so I would sort my stack like that of cat names. So we're familiar with these. And let's think about this. So, if we had to implement a stack but we didn't have an array,

[00:01:19]
>> Bianca Gandolfo: What could we use? What in the world is life for a stack outside of an array?
>> Speaker 2: Linked list?
>> Bianca Gandolfo: Hmm?
>> Speaker 2: Linked list, or I'm sorry.
>> Bianca Gandolfo: It could be a linked list, but we don't have a native linked list in JavaScript, yeah. Yeah,
>> Bianca Gandolfo: But that's good, I see where your brain is going.

[00:01:43] How do we store data in vanilla JavaScript. If we didn't have a data structure, how do we store it?
>> Speaker 3: Variable.
>> Bianca Gandolfo: In a variable. Yeah, we can store it in a variable. That's how we store data. We could hypothetically just have a string. And if our data is all strings we could just concatenate along the string and then just have methods that will only slice off the end of the string.

[00:02:11] Maybe you'll have spaces or dashes or something like that. Are we following here?
>> Speaker 4: Yeah.
>> Bianca Gandolfo: Cool. So here we have our constructor where our storage is a string. If we start off as an empty string this is our empty stack. And then we have our,
>> Bianca Gandolfo: Our different methods here, right?

[00:02:35] We're all familiar with this now. We can see that we're creating our instance. This is gonna be a weekly menu so, I don't know if you guys on Sunday or Monday kind of think about, okay this week I'm gonna have red beans. I'm from New Orleans originally, so we always have red beans on a Monday.

[00:02:52] And tomorrow's Tuesday, maybe we'll have tacos for Tuesday. Wednesday I heard that we might have Jamaican food. And so we can push our weekly menu onto our stack here and then just pop it off as we need a recipe or whatever. So here we are, we're creating our weekly menu.

[00:03:14] However, we have a problem. Our engineer did not finish implementing the methods. My God, what are we gonna do? What are we gonna do? I think we need to pace and put our hands like this. This is what we do when we don't know what to do. Hyperventilating, [NOISE].

[00:03:32] Okay, I think we're fine, gonna be fine, okay, all right. So,
>> Bianca Gandolfo: I'm gonna turn it over to you all. Yeah? We are going to do the job of our engineers. Those engineers, they're never doing their job, right, it's so annoying. We're gonna implement this, our storage is a string, yeah?

[00:03:57] So we need to push our data, you can create rules about this data if you want. It's data, maybe it has to be a string. Maybe we can't push a function on there, cuz that would get complicated. Maybe it's a number. Maybe you have to separate it with a dash, or maybe you have to separate it with, I don't know, you get to decide.

[00:04:20] And that's the cool thing about this cooking class, you have a framework here, you get to put in the ingredients. Yeah. So we're gonna take about 20 minutes.
>> Bianca Gandolfo: To implement our stack. Is everyone clear with what we have to do?
>> Bianca Gandolfo: Awesome. Okay.
>> Speaker 2: Question?
>> Bianca Gandolfo: Yeah.

[00:04:42]
>> Speaker 5: David M is asking, couldn't we use a literal using the index as it's properties?
>> Bianca Gandolfo: Like an object literal? You could, but we are gonna use a string because it's more interesting. Yeah, but that's the next step, you guys are one step ahead.
>> Speaker 2: Will you explain again what exactly we need to do?

[00:05:02]
>> Bianca Gandolfo: Yeah, absolutely. So hopefully you guys have access to the slides now. You're gonna take this code, you're gonna copy it, you're gonna put it in your text editor, or even in your JavaScript console if you like that. We're gonna fill in the blanks. So we have all these methods here, but they're totally blank, because the engineer didn't do the job, typical.

[00:05:26] So it's now up to you, you've gotta stay up late and finish up that work, so that we know what we're gonna eat next week. You know what I mean? Otherwise, we're gonna starve, so the stakes are pretty high. Cool? So we're building a stack interface. So push pushes it to the back, pop takes it off of of the back, yeah?

[00:05:49] So.
>> Speaker 2: They're asking for clarification in the chat room. You want them to use a string as the storage, correct?
>> Bianca Gandolfo: Yes.
>> Speaker 2: Not an array.
>> Bianca Gandolfo: Nope.
>> Speaker 2: Not an object.
>> Bianca Gandolfo: Yep.
>> Speaker 2: Not a file, nothing. A string.
>> Bianca Gandolfo: Nope, we're using a string. We're using a string basically as an array more or less.

[00:06:11] You can think of it that way if that makes more sense to you, David. But we're using a string for our storage for our stack.