In this episode we will learn how to implement one of the most simple yet powerful data obstructions
stack Study Finds to Operation PUSH AND POP.
So let's say this is our stock.
We will have different states every number represent a change in the state.
So the push Operation pushes a value on our stock.
So in this case we push to the top of one three to four and so on.
Numbers doesn't have to be in order.
Those values are whatever we want.
Now the pope operation does the side takes value from the stock.
So it is the state of our stock.
Five four three two one.
And we want to get the last element we need to pop all the previous elements.
And until we get into the element we want this structure doesn't seem to be very useful in the beginning
but every compiler you use every programming language every operating system uses these internally so
let's start by creating our data structure what is going to be call stack if you remember the previews
episode when we learn how to implement linguist we could use one of those as the underlying data structures
but for simplicity we will use the slides which are built in the goal language so let's call these items
and there's gonna be an array of integers if you will like to change data type.
We should do it here like strings or load 64 or whatever.
For this example we will use it.
So as we said before this tax implements two operations the first one is pop or push which receives
some item and doesn't return anything and the other operation is pop which doesn't receives any argument
but returns an item let's just return 0 4 now.
Okay.
So following the image the first thing we need to do is initiate our stock and then let's push some
items.
Right.
So we'll replace some items.
Let's now pop them and print them.
So dream line pop doesn't receives any argument.
We don't use.
So we push for items and we're popping for items as well.
So let's execute these and see what happens we does receive zeros.
And this is because the functions it's always returning 0.
So the first thing we need to do is to push some items into the stack.
So we'll do it by these items is gonna be equal to I to append items and the item.
Right.
And this is my error.
OK because item is part of the stack.
So what we have here is that every time we go push with an item stack dot items which is this one is
gonna be equals to itself but with a new item append that this is what append does appends a new item
which is the same that we saw in the image.
Okay so push is done.
Let's execute this.
We still have zeros of course because we haven't changed the implementation which is the function that
returns the actual value.
So you remember that we saw that you can do these were in this case X this is gonna be equal to one
and Y is gonna be equal to.
So we're going to do something very similar we'll do item and items are gone and B equals two items.
So now we're in the last element because pops takes the last element from our stack.
So to get the last one we need to know the size of the items is going to be right.
Stack of items is going to return the size.
In this case one two three four because the size is for even if Ds are 14 the size going to be four
it the fourth element but if you remember arrays are index it started by C.
So what we need to do is zero one two three to four do that we remove one element.
Okay.
Now items is going to be equals two items starting from zero ending in its size miners once again.
So why would return this item would correspond to this element which is the last one in the new items.
There's going to be equal.
All the elements starting from the first one ending in the last one.
And finally we assign the new items to the one from the stack Okay so let's execute these now and let's
see what we'll get.
Okay.
So we've got four three two one which is we push one push to push three push for the top four times
which is gonna be backwards.
You're gonna get four three two one.
Now what happens if we put another element.
What do you think will happen.
Let's figure that out.
We have an error and the error says slides bounce out of range which means that we're trying to reach
an element which is not part of our slide.
So we need to other conditions here which is one of the if items is equal to zero which means our items
are empty.
Let's return minus 1 Okay.
So if we saw minus one this means that we're popping up over a stack which is empty.
Let's execute this
yeah.
Sorry we move this.
Let's add some extra cops and we got the four numbers that we pushed previously.
Plus two extra minus one.
Now we can do a small improvement in our code because if you take a look we're calling line item line
item and line item three times.
So we can simplify these by saying let's replace these by left and the left is going to be the size
of our items.
This is creating the variable.
Let's make sure this is still working and it is.
So to recap we've seen this stock data obstruction that it defines to Operation Push and Pull pull is
very simple because we rely on the built in slices that the language brings is kind of more complex
of what we need is First we calculate the size of the current pack if the stack is empty we return to
Santina which is a minus one.
We can do whatever we want here that we need to make sure that this is happening and then we see the
double assignment which is one variable here and one variable here the first one is an element it's
an element from the array and the second one is a slice which is a sub array.
Both points to the last element when we got the new slice.
We assign it to our own and we return the item.
So this is pretty much it.
As I told you you might never use this data structure your production code but it's very good to know
how to do it because it will give us some practice around what we've already learned.
So this is it by.