Hey buddy How's it going.
This is Caleb with Deb's slopes dot com.
And in this video we're going to talk about the data structure called the stack.
And we're going to learn how to build one in swift which is very cool.
So let's go ahead and let's pull up an X code.
Click.
Get started with a playground and I'm going to go ahead and just do a blank playground and I'm going
to name this my stack.
Ok save it wherever I just said to the desktop and I'll make that full screen.
And we're going to go ahead and just actually delete all of this boilerplate code and then import Foundation.
We don't necessarily need you like it but Foundation is good enough.
And now what we're going to do is we're going to go ahead and create a struct called Stack.
All right.
And we're going to go ahead and talk about first what is a stack and how does it work.
So a stack is a data structure that is exactly what it sounds like it's a stack of pieces of data.
And if you think about books or I don't know cubes or some type of thing that can stack if you put one
on the table then put another thing on top of it and on top of it on top of it you get a stack.
And the way that a stack works is the last object you put onto the stack is the first one you can remove
easily right.
You wouldn't want to pull out the item from the bottom because then the rest of the stack would collapse.
So in a stack it works the same way.
And this is called leaf so kind and that stands for last in first out.
Kate that's what this data structure is it's last in first out the last item you add is the first one
you can remove.
So we're going to go ahead and add a private variable called array.
All right.
And it's going to be well we should declare it as an array of type String OK because our types are going
to be a stack of strings and so you know what let's actually call this a string stack for an Now just
so that we're 100 percent clear.
And I'm going to go ahead and just initialize this from the get go with just an empty array.
OK nothing there but it's of type string and that's the type it accepts.
Now we're going to use an array because an array has a very nice linear structure and we can add items
to this array just like we would add things to a stack.
So let's go ahead and let's dive into the three methods that are a part of a stack.
We have peek which allows us to look at the top item of the stack but not do anything with it we're
just going to return a value we have pop which is going to remove and return the top item of the stack.
And we also have push which allows us to add a new element on top of the stack.
So let's go ahead let's write those functions phunk peak.
OK.
And below that we have phunk pop.
And then below that we're going to do phunk push and this is where we're going to add an element.
And so I'm going to go ahead and just pass in an element of type string right because our array is of
type string.
So we have three functions here Peke.
Like I said allows us to peek at the top element of our stack.
And so that's going to return.
Well what type is our array string.
So we have a peak function that will return a string.
We have a pop function that will remove the top item and return it so we know what it was.
So we're going to return a string here as well.
So let's go ahead and let's actually begin with our push function because we're going to need to do
that first in order to actually add an item to our stack.
And so in order to add an element we're going to go ahead and call our array and instead of appending
of value because that would add it to the end.
I want to make sure that it's always being added to the top.
I'm going to call DOT insert new element at index.
OK.
So the new element is the element that we're passing in OK and we're going to add it at the index 0
so that whenever we add a new one it's at the index 0 and everything else gets pushed down.
Or I guess not pushed down because we're building on top of it.
So that is what we're going to do in order to insert a value.
But you're noticing can not use mutating member on immutable value.
OK.
At the current moment this struct is immutable meaning that we need to make our function mutating or
allowing it to modify this value.
So go ahead and type mutating at the beginning and that will allow this function to actually modify
our array variable.
Now of course these are throwing errors because it's expecting a return which we have not yet done but
now we can call push.
And that allows us to add an element to our right.
So peak is going to show us the top item and return it and we need to think though a stack it might
not have a top Ellen.
Right from the get go.
So we're going to go ahead and use guard lat so that we can be safe guard let's And let's call this
top element and we're going to say that that will be Auray dot first right now if you saw that I don't
know if you saw that there first is optional because there's not always an element at the first index.
So we're going to go ahead and say else and if there is a problem ok.
If there is a problem meaning if a write up first is nil we're going to go ahead and call fatal error
with a message and just say the stack is empty and we'll see how that works in just a second.
Then assuming that we do have a value we're going to go ahead and return return top element.
Now if we don't have a value there obviously is a better way to do this.
We could you know call some notification that would present an error saying hey that's not going to
work.
But for now we're just going to call fatal error and return that element if it exists pop is even easier
because we're going to go ahead and just return Flip's return array and we can call a function called
remove first and check out what this does it removes and returns the first element of the collection.
That's exactly what we want to do.
So it's almost sort of built into an array remove first is very similar to the pop function in a stack.
So similarly to our push function pop is also mutating our array so we need to tell the function that
it's allowed to do so.
And with that our stack is now finished.
Now what we're going to do is we're going to go ahead and play around with this so I'm going to create
an instance of my stack by typing var.
Let's make it a stack for names.
I'm going to send in names so names stack equals string stack like so.
And put some Princie is here to initialize it.
We now have a stack available to us and we can call name dot wups names stack.
Sorry dot peak pop or push.
Now if I call names stacked peak we get an error.
It's going to crash and we get our little error message we passed in the stack is empty.
So that's good.
It works.
We can call pop.
OK just like that and what it's going to do is it's going to say that it can't remove the first element
from an empty collection because we have not yet added anything.
So let's go ahead and let's call push and let's pass in the name pass in my name.
OK let's let's go ahead and.
Well now we can call names stack dot pop and we can remove that.
It will go ahead and print out the element remember because it returns it and then it removes it.
Now if I go ahead and type name stack dot may be peek.
OK.
We should get an error saying hey the stack is empty because it's empty again.
We removed it.
See that's how it works.
So very cool let's get rid of those too and let's push in some more names here.
Push let's add in Mark's name names stack names stack push and I'll add in Jacob.
And now we have three elements in our stack but we don't have a really good way of visualizing it.
And if I print it out like so we don't really get a very easy to look at understanding of what's happening.
Now I'll show you in a second we're going to actually write an extension of custom string convertible
that will print out a very nice visual representation of our stack.
But for now we can see that the first one I added is now at the bottom.
Then I added Mark that's in the second index or I guess the first index.
Then Jacob is at the zero index or the top.
So that's very cool it's working which is awesome.
So what I'm going to do is I'm going to go ahead and create an extension of string stack and it's going
to be conforming to the custom string convertible protocol.
So what we're going to do is we're actually going to modify the description property so that when we
print out our name stack we get something a little bit different that's easier to visualize.
So I'm going to go ahead and create a description here of type string and I'm going to use some curly
brackets so we can go inside and modify what this description will be composed of.
So we're going to create some dividers that will show like almost like our stack is in a box.
So let's create a top divider top divider and that's going to be equal to this one two three.
And then stack one to three.
And then the bottom divider is going to be equal to just a bunch of dashes.
One two three.
And then there's five characters in stacks so one two three four five one two three.
They're the same length now.
So what we need to do next is we need to access the array that is inside of our stack and then we need
to basically join them together with a separator.
But we're just going to actually use the new line separator like so.
So I should actually put this right here.
I have a new line.
And then for this one I'm going to do a new line here and a new line here so that their separation from
everything.
So let's go ahead and let's create the actual stack elements by typing let stack elements equals array
joined and you can see you can put in a separator there.
I'm going to go ahead and just put in that the new line syntax as a separate or so that each element
will be on its own line simulating a stack.
Pretty cool.
Next we're going to go ahead and return.
Return top divider Plus the stack elements plus the bottom divider and check it out.
When we actually print Oops that should be a plus sign.
When we print this out now you'll see that our stack is much much easier to look at which it's running.
And there we go look at that.
We've got a stack that's pretty cool.
It's running again.
Looks like it's stopped.
There we go.
So there's our stack.
It's easy to visualize Jacobs on the top.
Mark is in the middle and I'm at the bottom.
Now if I were to go ahead and type name stack dot Let's go ahead and let's pop an element off then print
out name stack.
You'll see we get a different result.
Now Mark and Caleb are here.
Mark is now at the top against.
We pulled Jacob off the top.
What else can I do.
I can well you know what let's go ahead and let's actually peek.
Stack peak and then I can print out the names stack again so we can see what's going on.
So first we get a stack here.
Jacob Mark and Caleb.
Then we peek which returns.
Jacobs That's the top element and we print it.
Nothing is modified.
Then we call names stack the top pop.
And what that does is it removes Jacob from the top.
That's pretty awesome.
So guys this is basically it.
This is how a stack works.
It's last in first out the last element you add in is the first one you're able to remove.
There is a function called peek which allows you to just look at the top element in the stack.
There's a function called Pop which returns and removes the top element in a stack and there's push
which allows you to insert a new item to the top of the stack.
This is how the stack data structure works in Iowa and swift and it's basically the same in any programming
language you might just have to change around the way you write your code.
This is Caleb with slopes com.