Learning data structures is hard or is it?
Today I'm going to prove it to you that learning data structures is not that hard as you think of it.
Today I'm going to show you how you can use and build a stack. Now if you do like these type
of videos and you do like learning about data structures go ahead and give this video a LIKE
because if I get !! 50 !! likes in the next three days of the publishing of this video I will add another
data structure to the list and that is Linked List. So without further ado let's get into data
structures and we are going to talk about today about Stacks. One of the easiest data structures
that you can learn today is a Stack. Today we are going to create a Stack in Swift and we are going
to implement it into a SwiftUI app. Before we dive into the code let me just remind you about
DEV_FACTORY it is at rebeloper.com/mentoring where I can teach you a lot about SwiftUI, Swift.
Go ahead and check it out it will be a 60 minute Zoom call, one on one Zoom call with me. Go ahead and
check out rebeloper.com/mentoring. This video is sponsored by WebViewGold. With WebViewGold
you can convert your web app or website into apps for iOS and android. It just works. Download the
Xcode project, insert your website and you're done. No coding required. Optionally you can take
advantage of their many customization options like push notifications, in-app purchases, ads and
many more. Save 25% with their back-to-school sale. Link is in the description. Now let's get back to
the video. So let's just talk about the stack data structure. What I did is created a basic content
view for our app and it has this empty stack text on the top, it has a divider and two
buttons inside the horizontal stack and then we are going to add in the stacks description.
Pretty straightforward. So let's just jump right into creating our own stack. So let's create a new file
and it will be a Swift file. Let's name it stack and let's create that, okay. So before we dive into
the code let me just talk a bit about what a Stack actually is. Think about a stack of pancakes or
a stack of books or a stack of cards, a deck of cards. Each element is on top of each other.
Now the interesting part with the stack is that the stack is a LIFO data structure. That means last in,
first out. So LIFO whatever last in first out data structure. This means that whenever
we are putting a new element on top of the stack that will be available and when we are removing an
element from that array, that stack we are removing the topmost. So putting on a new stack element on
the top and then removing it from the top. Also we can check if this is empty and we may peak
the first element only. This is the interesting part. This is kind of a rule,
it's kind of forcing us to just use the first element like the top and we are picking that
top element only. Think of a good stack example is the navigation view. So a navigation view is
actually a stack of views. You are only seeing or using the top one and you are always picking that top view.
So that's also available in UIKit like UIViewController has also a navigation stack.
So let's create our very own stack. So of course we have to have foundation imported
and let's have it as universal. So we could just have a stack of strings, ints but
I'm going to use an element for that. So we can just use any type of struct.
So let's create our struct here and it will be named stack, okay and we are going
to use element right over here. So what do we need inside a stack? Well we need some sort of
a storage which will store all the elements and we shouldn't be able to get access to that storage.
We are going to get access to the top element with the peak function but that's why
we want to have this set as a private var storage and this will be an array of elements.
Yes! This is actually an array, a special kind of array. So what we want to do here because this
is private we might want to initialize it. Well we have to initialize it actually because
that is private and we are going to initialize it like so. So elements of type element array
and what we're going to do here is go storage equals elements, okay. That's perfectly
fine what I do want to add in here is another type of initialization and I will just
add that as an extension. So here we want to add in actually an array.
Now I want to have an initializer as an array literal. So let me just type that out, it's pretty
straightforward let's just have this extension of the stack and custom string convertible.
There we go and this will be an array. So again init array literal, okay and elements and element. Three dots that's an array literal
maybe we want to just add only one element and then we just, well actually the first element and
then we don't have to add those square brackets for the array implementation actually and then
again storage equals elements. Okay! So that's our two initializers and
let's see, oh it's expressible by array, sorry. Expressible by array literal, yeah. The custom
string convertible should be the description. So let's add it that in there since I have already
talked about that. So extension stack and then custom string convertible, okay.
Now this will be our description. So var description and we are going to add in this
kind of an array. I don't know if you knew this if you just add these three cotes and
in center of those three, like six cotes three and three we can add new lined text.
So what I'm going to do here is just add top and then let's just have here kind of the
bottom and then let's have some sort of map of our storage. So storage, we need that right
over there. So storage dot we want to map that and let's see I'm going to remove this.
I'm just going to type that out it's much easier. So what we want to do is just grab the element. So dollar zero
that's actually one of the elements in the storage and we want to reverse that, so reversed.
Sorry. Let's just go right over here and reversed and then joined by a separator of a new line.
So dot joined by separator and this is actually a new line and basically that's it.
This is how we are going to have that description and we can just go back into the Content View and as
you can see the stack has a description. We are going to add that later on, okay.
So let's talk about the stacks. So this is how we create a stack with a default value or value.
So with the array literal we can just have one value or we can have, well of course we can may have
more elements right over here with array literal but here with the elements we can have default
values as an array, okay. That's our storage but currently the storage is private. So we cannot
access it. So let's just add in here some functions that will push, pop, peak and check if the
stack is empty. These are the four things that stack should do. So the push should be mutating
because we are going to change the storage. So this is a mutating func and that is push and
underscore element. So that will be an element and we are going to just go storage dot append element.
Now that wasn't too hard right? So this will be really, really fun. Next up is the popping.
So whenever we want to pop the topmost element. So we are going to go with a mutating
function again and that is pop and we don't need properties here because we are just
going to pop and we are getting here back an optional element and you will see why in
just a second. So we are going to go storage dot pop last and as you can see the pop last is
returning back an actual optional element. So this is coming from an array but
most probably you don't want to use this element. So we are going to mark
it as @discardable result. This means that the pop function
you may or you may not use the return of this function. So that's our discardable
result. It's really, really handy, okay. Next up is the peak. So we want to take a
look at the top most element. So again a func and it's called peak and that is going to return us back an optional
element because if the storage is empty of course then it would return nil. So what we want to have
here is storage dot last and as you can see the last element is also an optional and finally
is this empty and that is a property is empty. Let's have this as a boolean and if we
peek and we get back a nil then the actual stack is empty. So this will return peak
equals nil. So if this is nil it will return true. If this is not nil, the peak is not nil then this
will return false and we are done with our stack. How easy was that. Now let's have a really
nice example of how the stacks work because this is okay we have our storage we can push,
we can pop, we can peek and we can check if this is empty but how do we actually can use this well
in a fairly simple example. So let's go to the Content View and as you can see I have already
set some things up and I will just build and run this until I explain what I actually want to do here.
Well I want to have a stack of int values and I'm just going to add in their random values
or let's just go by first of all by incrementing the values and then I want to pop them as they go.
So basically this is our starter project. Okay! So as a usual SwiftUI project we
want to create first our Content View Model. Okay! Let's create a new file, Swift file.
Content View Model and this Content View Model it will be a really simple Content View Model.
Again it will be a class of Content View Model and it adheres to observable object.
Observable object and here I'm just going to create our stack which will be published so whenever it changes
it will notify our Content View. So add published var stack and here goes the really
interesting part. So here we have our stack and we can just add it like int and then have
it like so. So this is also fine but we can also have it as let's say we have a zero value then
we don't have to consider adding the stack type but because this is a simple published variable
and we want no values inside the stack I would just revert that. This is the simplest way to do that, okay.
And right over here for the int we can add any type of element by the way, okay.
Let's go back to the Content View and grab access to this Content View Model. @StateObject private var view model equals our Content View Model, okay.
So now we will be able to grab access our view models.
So first of all let's see how will we actually add the topmost element into our text
here instead of empty stack. So this will be a little bit complicated, I will just
type this out because there will be ternary operator. So if the stack
is empty then we want to print out for the text empty stack.
Otherwise we want to peek at the top most element of course. So let's just type it out.
First of all viewmodel dot stack dot is empty. Is empty, there we go question mark.
Now if this is empty we want to just type out here empty stack. Otherwise we want to
peek at the stack. Viewmodel dot stack dot peak and this will return an optional but in
this case we can safely bang that out because we do know that it is not empty. So it will not return nil at all.
So yeah first of all let's just hit Command + R so we can see that empty stack is
displayed as the text, that's great, okay. So let's just add a new value right over here.
So under the pop, sorry not pop but under the push we want to go view model dot stack dot push
and as you can see we may push an int value. So currently I'm just going to peak at the topmost
element of course yeah that's what peak means and then I will just increment it by one.
So let's just have this view model dot stack dot peak and this is optional, so if there isn't any let's
just use a zero and let's increment it by one, okay. So let's build and run and see how this looks like.
So let's just tap on push here. It just pushed one, two, three, four and so forth. So let's just pop
these and that is the simplest way to do this. Under the action of our pop button we just
go view model dot stack dot pop. Hit Command + R to build and run again and now we can just
push and then we can pop. It's really, really nice. How awesome is that. Okay, but I would like you
to go even further and use the description. So let's just use the description that we already
created and I'm just going to comment this out and just use the view model dot stack dot
description and now you will see the description then the actual stack that is underneath.
Of course you cannot peak this but yeah let's just see. Let's just push, there we go. We just pushed one
and then we are pushing two. As you can see it's on top of one, three, four, five and we can pop easily
as that and once we get to the empty value we just print out empty stack for our text, okay.
So yeah this is nice. Let me just show you how you can just add in random values. So this plus one.
It's a little bit much too easier so and we just go again view model dot stack dot push
and now for the element I'm just going to go int dot random and the range would be between 1 and
let's say 100. So now we can just see that random number will be selected. 31, 71, 20 and so forth and
if we pop now we are going to pop the 57, popping 20. Popping 71 and finally popping 31 and that is stack.
It's really, really nice to have in your data structure arsenals. Now if you do like these type
of videos go ahead and check out rebeloper.com/mentoring where I can teach you a one-on-one
Zoom call anything that you want to learn about for example data structures or SwiftUI
or Swift for that matter. Also I can just bug hunt your code if you have any issues
with that. Again it's at rebeloper.com/mentoring. Stacks can be used in a variety of ways and now
you know how to build one for your own convenience and of course use it in a SwiftUI app.
Now if you do like these type of videos go ahead and make sure that you hit SUBSCRIBE
to the channel and hit that LIKE button don't forget about the 50 likes that I want for
the next video to come out about the Linked List and make sure to hit that notification bell
while you are there so you get notified of these videos. Check them out they are really really
cool and as usual I will see you in the next one.