Master Stacks: Fun Guide to Data Structures, LIFO & Real-World Uses

Master Stacks: Fun Guide to Data Structures, LIFO & Real-World Uses

Want to nail down stacks? This beginner-friendly video breaks down the stack data structure with clear diagrams and a laid-back vibe. We’ll walk you through how stacks work, why they reverse data (hello, LIFO – Last In, First Out), and how they’re used in real-world stuff like browser history, undo features in editors, and even the call stack in your code. Whether you’re just starting out or brushing up on data structures, we keep it simple with hands-on demos of pushing, popping, and checking stack size—no boring jargon here! Subscribe for more coding tutorials to level up your skills, and let us know in the comments what you want to learn next. Check out our site via the QR code in the video for more coding goodness!

Introduction to Stack 00:00:00
Explaining Stack Concept 00:00:06
Drawing Stack Diagram 00:00:30
Adding Elements to Stack 00:00:53
Stack Rules and Operations 00:01:16
Pop Operation and Interface 00:06:09
Stack Size and Empty Check 00:07:59
Tracing Stack Operations 00:11:46
Stack as Data Reverser 00:17:44
LIFO/FILO Explanation 00:21:13
Stack Use Cases 00:24:10
Call Stack Mention 00:26:18
Conclusion and Subscribe Request 00:27:21
Outro and Website Promotion 00:28:34

Thanks for watching!

Find us on other social media here:

  • https://www.NeuralLantern.com/social

Please help support us!

  • Subscribing + Sharing on Social Media
  • Leaving a comment or suggestion
  • Subscribing to our Blog
  • Watching the main “pinned” video of this channel for offers and extras

Hello there.

In this video, I’m going to show you a data structure called a stack.

We’re going to make a diagram of it.

We’re going to explain how it works.

We’re going to look at its typical interface,

and we’re just going to kind of work through the concept

of what is a stack and what does it do and all that stuff.

Where’s your daddy and what does he do?

So for starters, what am I talking about?

What am I talking about?

Let me open up a little notepad here.

And I’m going to start drawing.

A stack is pretty simple when you first think about it.

It just takes a little bit more work to really get all the details down.

Imagine I have a data structure and maybe like at the bottom here,

there’s like some grass.

And so we can say that the data structure is empty when we first start.

Okay, so there’s kind of a stack here, but it’s empty.

So there’s nothing really on screen.

Imagine then I wanted to add integers into this stack.

So I’m going to say let’s add the integers eight and then two and then three and then four for some reason.

Okay, so how do we do this?

The first thing we do is we take the eight and we just put it on top of the stack.

You can imagine this as a stack of boxes or a stack of books or a stack of whatever.

When we add items, we’re always going to add to the top of the stack.

When the stack is empty, we’re obviously just going to add like at the very bottom, you know, at the ground floor.

But that’s sort of the top.

when we add the two here the next thing that happens is we just put the two on top of the eight

and the next thing that we do is we uh put the three on top of the two and so forth right so

it’s important to understand a few a few rules about stacks let me add that four real fast

in stacks you’re only allowed to add to the top or remove from the top you’re not allowed to add

or remove from anywhere else let me put a t for top right here so basically you’re allowed to add

So basically you’re allowed to add and remove from the top.

So if I wanted to add, let’s say a 12, the 12 would have to go on top of the four.

It couldn’t go anywhere else in this data structure.

And if I wanted to remove data from this stack, I could only remove the four.

I could not remove the three or two or the eight or anything.

You’re also not allowed to look at the middle of the stack.

You’re not allowed to look anywhere but the top.

So if I wanted to look at the three, the two or the eight, I wouldn’t be allowed to do it.

I can only look at the four.

look at the four and if I want to see that three I have to pop the four off

first in order to just see what’s under it and technically unless you have the

data you added memorized somewhere else you probably shouldn’t know what’s under

the four until you actually remove it okay so we can add and remove from the

top we can’t look in the middle we can’t look at the bottom or anything like that

and let’s do a little let’s do a little sequence of pops let me show you what

So first off, you can imagine a stack is sort of like a vector or a list with less functionality or more restrictions.

It’s useful to add restrictions on top of more robust data structures for the purposes of, I don’t know, kindling your imagination or allowing yourself to use easier implementations or whatever.

If you’re a musician or artist, probably at some point you’ve been stumped and you’ve had writer’s block and you can’t figure out what to create.

can’t figure out what to create so uh you know i know sometimes musicians when they get writer’s

block they’ll they’ll go to the swap meet and they’ll buy like an old dusty dirty uh casio

keyboard from 1985 and then they’ll write a full song just with that one piece of equipment and it

really stirs their creativity or it sparks their creativity and then when they’re done then they’ll

probably upgrade to better equipment but anyway so there’s there’s a bunch of different reasons to

use you know lesser data structures although this is not really lesser it’s very useful

It’s very useful in its own right.

So let’s see the interface.

So what can we do in a stack?

I’m going to put S for stack.

Actually, I’m just going to type this up as code.

No need to use the pen the whole entire time.

Let’s see.

So I’m going to put S for stack.

Maybe I’ll say, you know, stack that holds integers.

And I’ll just put an S here.

If you don’t know how to code right now, at this point in your career, just bear with

me.

variable of type stack and in C++ these stacks you know stacks are a template of data structure

which means the integers that we are holding inside of the stack they could be any other data

type that we wanted to including custom classes so in C++ we would use these angle brackets to

say I want my stack to hold integers and then I’m going to say s is the variable that I’ll use for

the stack so but this is not really a programming video this is just about stacks so if I wanted to

stack to push something like onto the top of the stack i would use the the method push the method

push usually has this prototype let’s see i’ll do stack push and it usually takes in a t type element

and again the t type it’s a templated data type which means right now if i just declared a stack

that was of type integer it holds integers then the t would actually expand under the hood in c

hood in C++ anyway at compile time to be an integer. So it would be like typing int e, right?

So whenever I type t, just understand that that means one instance of a thing that you could put

into the stack. So we’ve got a push here and usually the return type is void and it takes in

one element to push. And that means if I’m going to actually call, let me put some comments here.

If I’m going to actually call push on the stack, I’ve got to give it something to push and the

type of the thing that I’m pushing has to match what the stack was declared to hold. So in this

case, it’s integers. So I’m going to put an eight to match the diagram up above. And then after that,

maybe I wanted to push another number. So it’s going to be a two. And I’m just going to match

what the diagram has up above. So eight, two, three, four. So that’s the interface for pushing

usually in C++. And other languages should be similar. The other thing you can do with stacks

We haven’t done that yet, but let me show it to you first.

POP usually comes in two forms,

depending on what implementation you’re looking at.

So sometimes POP returns nothing.

It simply removes an item from the top of the stack.

And sometimes it returns a copy of the item

that you’re actually removing.

So I’m going to put like stack here and then like stack

just to show you that it’s inside of the namespace.

Oh, I don’t know.

Maybe that’s too C++y.

I’ll get rid of that stuff.

Hold on.

hold on so we’ll maybe do this just to show you that we have three separate

parts to this little code snip so sometimes pop doesn’t give you a copy of

the thing it removes sometimes it gives you a copy of the thing it removes in

cases where pop does not give you a copy of the thing it removes then you would

probably want to call top right before you popped and top will return a copy of

Just to clarify, if I wanted to remove something and get a copy of it and I had this line 13

form of pop from whatever implementation I was using, then well, I would just call pop

and I would do something like this, you know, auto element equals, you know, the stack dot

pop.

And what would happen is under the hood, it removes the item and then it also returns

a copy to me.

Sorry for being too C++ in this video.

On the other hand, if I have the form that has void for pop,

then I would have to do something like this.

I’d have to say auto element equals stack.top just to get a copy first,

get a copy of that for before I remove it.

And then when I’m finished, then I can say s.pop just to grab a copy of it and then remove it.

So for now, we’re just going to use the form that gives you a copy of the data

at the same time that you’re removing it.

thing to keep in mind let’s see there are two other functions i wanted to mention mention real

fast so for now i’m going to say that we only have the t pop type you can also check to see if a stack

is uh is empty and you can also check its size uh usually those are functions that have those names

but i’m going to do let’s see boolean empty and then i’m going to say size type size again sorry

an unsigned 64-bit integer so just like a count or a size something that’s just

going to be a whole number and never negative so we have this little

interface that this data structure has and we have been calling push so far so

let’s go ahead and do some pops I’m gonna say auto auto elements equals s

dot pop maybe I’ll do two of those in a row maybe instead of calling that

just so that we can have different variables to put our pops in.

And then maybe after that, I’ll do some more pushes.

I’ll say, let’s push like a 15 and let’s push like a 25.

And then I think that means it’s probably time to erase that,

the top part of the diagram with the 8, 2, 3, 4

in favor of just whatever pushes and pops we’re doing.

So let me just do this real fast.

Okay, so we did our pushes.

two three four and then I’m going to try to grab a couple pieces of data with these pops

and for pop all you have to do is just to well I should have mentioned this before but first

check to make sure that there is something to pop for example if I had a blank stack let’s say I had

a blank stack right here there’s just like nothing on it and this green line is non-standard I’m just

drawing it for fun but imagine I had a blank stack with no items and I decided to call pop on it

well you’re not allowed to pop them an empty stack there’s nothing there to pop which means that’s a

stack there’s nothing there to pop which means that’s a really really naughty thing in c++ and

other languages we would say that something exceptionally bad has happened the user tried

to pop something that wasn’t there and so then we would do something called throwing an exception

at the user again this is not really a code video but if you know a little code hopefully you

understand try catch blocks and throwing exceptions when bad things happen so we would just throw an

exception at the user if you don’t know how to code at this point then just keep in mind we would

mind we would refuse to do it we would say something bad has happened we can’t do that

and then when you’re doing your pushes sometimes depending on what what you’re using to implement

the stack under the hood inside of your code you might decide that you’re using let’s say you’re

using an array under the hood which has a fixed capacity if you run out of capacity and you can’t

then you would throw if the user tried to push something into a full stack we would call that a

stack overflow and then when we tried to pop from an empty stack we would call that stack underflow

but for the purposes of this video just assume that the stack is just a diagram and it has no

capacity so we can just add as many things as we want on it or maybe we’re using a linked list

under the hood which would have an unlimited capacity or limited only by the machine’s

But anyway, so let’s go ahead and try to do this pop.

So let’s do a pop here.

Well, in the pop, we just look at the top only, right?

So we say, well, you know what?

Let’s make this more interesting.

Hang on a second.

Let’s make this more interesting.

Let’s do this.

I’m going to do auto a equals s dot top.

So I’m going to grab a value without actually removing it.

Okay.

So more interesting.

So we push the 8 and we push the 2.

So at that point in time, actually let’s trace this from scratch.

So we have like an empty stack here.

There’s nothing there.

And we’re going to push the 8.

So the 8 goes on the very top, just like we did last time.

I’m going to put an 8 there.

And then we’re going to push the 2 next.

So the 2 just kind of goes on top.

And then the next line, line 12, we’re just checking the top.

we’re allowed to actually look at the top if we want to. So since two is at the top, that means

a is equal to two. I’m going to put a two here to remind ourselves that a is now equal to two.

Then we’re going to do a push three. So I’m just going to stick a three on top of the stack.

And then we’re going to push a four, four is on top of the stack. Now, again, we’re not allowed

to look at anything in the middle, anything, anything, but the top. So for example, when the

it and remove it but now that we have a four on top we can no longer even see the three it’s not

available to us anymore so then we’re going to pop and put that value into b so um you know what’s

at the very top it’s a four so that means we’re going to actually remove the four and stick that

into b so i’m going to put four right here to remind ourselves that b is now equal to four

and uh for the purposes of this video you can just imagine that this you know the top of the stack is

of the stack is just totally gone deallocated depending on what implementation you use you

might want to think about crossing it out maybe there’s like some junk data there now but for the

purposes of this video in this diagram we’re just saying it’s gone so it’s just totally gone

okay then let’s do another pop so we’re going to pop and that’s going to become c so obviously the

top is uh is now the three so that means the three is going to go into the c variable so here’s a

c variable so here’s a three right there then the three gets deallocated

and now the top is the two so then we’ll push a 15 and we’ll push a 25 so again we just put those

new items on top of the stack even if there was stuff uh there before it’s gone now so we’re just

be a 15 and a 25 on top of the stack so we got this and then 25 and then we’re done adding our

stuff so uh the other functions that we talked about are empty and size uh at the very beginning

when we had just an empty stack like that if we were to call on the empty function it would tell

us true the stack is empty and then every step after that where we had some data in the stack

data in the stack like as soon as we even added that eight at the very beginning from that point

forward then empty would have returned false to us saying no the stack is uh it’s not empty

the size would have changed uh during every step of the way so you know at line 10 the size would

have been one after we’re finished with line 10 after line 11 the size would have been 2. 12

wouldn’t have changed the size so it still would have been 2. 13 would have upgraded the size to

have upgraded the size to four once we popped after that pop was finished it would have been

three again then the second pop would have brought it back down to two and then those two pushes

would have put it back up to three and then four and so then the final size whoops that was horrible

the final size of the stack is four and you can tell that there’s just four items

another way to double check yourself if you’re kind of trying to trace this on your own is just

count the number of pushes and subtract the number of pops so how many pushes do we have one two three

2, 3, 4, 5, 6.

So that’s like 6 minus however many pops we saw.

We see 2 pops.

So 6 minus 2 is equal to 4.

So, you know, when you’re writing this sort of thing down on,

like as a diagram to practice,

then you know it’s a 4 just by looking.

But what if you did something wrong?

It’s a great idea to double check yourself at all times.

So 6 minus 2 is 4.

So we know that the size of the stack is 4.

Looks pretty good.

Looks pretty good and we know the values of A, B, and C and you know one of those was

top two of those were pops.

And yeah, okay.

So notice something though that’s kind of peculiar about the data that came out.

If we added the data, like I guess while we were adding the data, we added first an eight

and then a two and then a three and then a four.

I’m just looking at the pushes right now and then a 15 and then a 25, right?

So we added the data in that order.

order. But then when we grab the data out, let me put this in, but maybe like a red bracket

or something. When we grab data out of it, the pops and the tops gave us two and then

a, okay, nevermind. That was not a pop. It gave us a four and then a three if we’re just

look at the, look at the first two items. Sorry, sorry, sorry. Look at the last two items right

before, and I didn’t draw this very well. Imagine that we had just pushed the eight and the two and

the three and the four only forget about these other pieces of data. So at this point, when we

start calling our pops, we have only an eight, two, three, four stack, right? So the data that

has a four and then a three if we’re removing data from the stack.

So notice how the four and the three are backwards.

Notice also like if we kept popping out data, you know, we got the four and then we got

the three, the next, what’s the next thing that would come out?

It would be the 25, right?

And then it would be the 15.

And then it would be the two.

And then it would be the eight.

So if you really think about it, the stack is reversing our input data.

We added a three and then a four, but we received a four and then a three.

And of course, it’s a little bit muddled because we have some pushes in between some tops and pops and stuff.

But just looking at what goes into the stack, it’s backwards, right?

Eight, two, 15, 25.

Let me finish this push sequence up here.

So we added three and four, and then we added a 15.

And then, oh, my penmanship, dude.

shit dude 15 and then we added a 25 so if you look at the data that comes out this is like a 15 and a

25 backwards and then the three and the four are backwards and then the two and the eight are also

backwards and so one thing that a stack does is it reverses data let me give you a cleaner example

with no pops in between the pushes just to show you what we’re talking about a little bit more

bit more clearly okay so i’m going to do maybe like another code page here whoops do that and

then i’m going to say got our stack and we’re just going to push in some data

and then we’ll just say s.pop and maybe i’ll just do that however many times

six two three four five six so now just a very quick trace because this is basically the same

intermixed. So I’m going to do like the bottom of the stack here. And then, well, I mean like an

empty stack. That’s not really a bottom of a stack. So we’re going to push an eight.

And then we’re going to push a two. And then we’re going to push a three. And then we’re

going to push a four. Then a 15. Got to work on my fives, dude. Okay. And then 25.

Okay, one more time.

Oh yeah, I guess I got to slow down.

So what’s going to come out?

Remember the top is the only place that will give us data.

So if we start popping all these one by one, we’re going to end up with a 25 for that first

pop.

Maybe I should write it down over here.

Do like a 25 and maybe I’ll add some line breaks in the code here so that it’s easier

for me to write with a pen.

Do this and that.

Okay, so the 25, I’m just going to do a red line through it to designate or to denote

deallocation or just like gone-ness.

Then the top is at the 15.

We do another pop.

It’s going to be 15.

Oh, I did my first perfect five today.

And then the top of the stack is now at the four.

So then the four is going to pop out next.

And then the three is going to pop out next.

The top is now at the three.

the three so the three is coming out for that pop we deallocate the three the top goes down one and

then we pop it becomes the two that’s deallocated now and then we grab the eight because that’s

where the top is so now we’ve grabbed all of our data out and I just want you to look at this one

more time notice the data is eight that the data that went in was eight two three four fifteen twenty

The data that came out was backwards 25, 15, 4, 3, 2, 8 or 8, 2, 3, 4, 15, 25.

If you kind of like look up.

So a stack is a data reverser.

A stack is also known as something called a, let’s see,

last in first out data structure, LIFO,

or first in last out, phylo data structure.

What that basically means is just it’s reversing the data or priority goes to the item that is the youngest.

So if you looked at the stack at the point where we were about to remove the 25,

25 was the most recently added or the youngest item or the item with, I guess, the timestamp, the furthest in the future,

however you want to look at it.

And that’s the item that came out.

So if you think about it, it was the last item in.

And therefore, when we did another pop, it was the first item out.

So LIFO or PHILO.

and uh you know this eight down here that was obviously the last item that we were able to grab

out of it so when we said you know hop our final pop before the stack became empty uh the eight was

definitely the first item in so first in was the last out if you really want to you can kind of like

jumble these uh these acronyms uh i think most like people just use lipo or phylo

life if you say something to me that I understand I think it’s probably fine

but it’s it’s non-standard I like to say you know foley first out last in that

definitely makes sense it’s just not very common another thing that I like to

do for fun is also I’ll mix up the phylo and I’ll say lo-fi because last out is

first in and now I’m cooler than other computer people because I use the word

lo-fi I’ve now got like a giant mustache and like a little like swap meet shirt

like swap meat shirt and I have craft beers and I have work boots on and I’d

roll up my jeans and my, my, my, my, my sleeves are rolled up and all that stuff.

You know what I’m saying?

I’m cooler than everybody else now because I used loaf pie anyway.

So we used integers.

It’s just important to understand that in modern coding,

I think I might’ve mentioned this before with the T’s,

these stacks are templated data structures,

we can hold another data type besides integers as long as we declare what the stack will hold in

advance. So instead of integers, you could imagine there’s like a custom class, like my class,

every single, you know, item in the stack is actually a full instance of a my class object,

or, you know, floats or strings or just, you know, any data structure you want,

you could put another data structure inside of a stack. People do that sometimes just for fun,

sometimes just for fun you could take a vector and stick it inside of a stack a list put it inside

of a stack you know whatever you want to do just to show that you understand everything

um so again by the way the stack is empty if we tried to pop from it at this point

this would be called a stack underflow we would probably want to throw an exception at the user

or just say we’re not allowed to do it let’s see um so i guess now i should tell you some

common uses for stacks i’m looking at my notes right here uh obviously our stack has reversed

has reversed the data. So stacks are kind of good for data reversing. Imagine if you had like a word

and you wanted to detect if the word was a palindrome or not, you would just like put all

the letters of the word into your stack and then grab them back out and see if the word was still

in the same order. If it was, then your word was probably a palindrome. If you don’t remember what

a palindrome is, I’m going to write down the word radar in reverse from backwards, from back to

front, right? It’s the same word. So that’s a palindrome. You could also use stacks kind of

as a trail of breadcrumbs. Like you could imagine that early implementations of browsers

had your browsing history in a stack somewhere so that every time you visited a webpage, then

some kind of block of information, maybe like a custom class instance was put onto a stack

somewhere and it contained information about like when you visited the webpage, what the URL was,

it is that the browser wants to remember and then later when you hit the back button then you can

imagine the browser is popping from the stack in order to go backwards in your history or your

spotify playlist or whatever it is you’re doing honestly you know these systems are much more

advanced than just using you know vanilla stacks now but you could imagine doing something on your

own for fun or just what it might have been like in the very beginning i’m going to erase fully

because that’s just that’s just that’s just cringe lo-fi i think is way cooler um so trail of bread

So a trail of breadcrumbs, like browser history, you could also imagine undo history, like

if you had a, I don’t know, like a text editor of some sort or like a image editor of some

sort, every major change that you did to it, maybe the program under the hood is adding

your change to the stack.

And that makes it easier to sort of reverse your changes as those changes are popped.

And there’s like a sort of mathematical uses like balancing parentheses and things, but

balancing parentheses and things, but I’m not going to talk about that too much.

And one of the most important uses for stacks inside of your computer is actually the call

stack. I’m going to talk about that in a future video. But again, remember each, you know, little

item on the stack, it could be a different data structure on its own. It doesn’t have to be an

integer. So imagine that I bundled up a bunch of information about a function call, and I called it

I just stuck a call frame on the stack.

That’s basically the call stack.

But that’s for another video.

Anyway, so let’s see.

Is there anything else that I wanted to show you?

I think we’re actually done just talking about the basics of stacks.

I’m not talking about time complexities or anything like that in this video.

Check a future video if you’re interested in time complexities per certain implementations

and so forth.

But for now, this is how a stack works.

or LIFO or LOFI data structure.

Nobody’s going to say LOFI but me, just FYI.

But maybe everybody will start doing it now.

I don’t know.

If it becomes a trend, okay?

You heard it here first.

Give everybody this link.

All right, I hope you feel like an expert on stacks

at this point.

I hope you had a little bit of fun

and learned a little bit of something, some stuffs.

I’ll see you in the next video.

hey everybody thanks for watching this video again from the bottom of my heart I really

appreciate it I do hope you did learn something and have some fun if you could do me a please a

small little favor could you please subscribe and follow this channel or these videos or whatever it

is you do on the current social media website that you’re looking at right now it would really mean

the world to me and it’ll help make more videos and grow this community so we’ll be able to do

videos, longer videos, better videos, or just I’ll be able to keep making videos in general. So please

do me a kindness and subscribe. You know, sometimes I’m sleeping in the middle of the night

and I just wake up because I know somebody subscribed or followed. It just wakes me up

and I get filled with joy. That’s exactly what happens every single time. So you could do it as

a nice favor to me or you could you could troll me if you want to just wake me up in the middle

of the night, just subscribe and then I’ll just wake up. I promise that’s what will happen.

Also, if you look at the middle of the screen right now, you should see a QR code which you can scan in order to go to the website

which I think is also named somewhere at the bottom of this video and

It’ll take you to my main website where you can just kind of like see all the videos

I published and the services and tutorials and things that I offer and all that good stuff and

If you have a suggestion for

Clarifications or errata or just future videos that you want to see

please leave a comment or if you just want to say hey what’s up what’s going on you know just send

me a comment whatever i also wake up for those in the middle of the night i get i wake up in a cold

sweat and i’m like it would really it really mean the world to me i would really appreciate it so

again thank you so much for watching this video and um enjoy the cool music

as as i fade into the darkness which is coming for us all

Thank you.

Comments

No comments yet. Why don’t you start the discussion?

Leave a Reply