‫In this video, you will learn about how to use tanks in C-sharp, so how you can add data to a stack,
‫how you can edit data in a stack and so forth.
‫So first of all, let's look at how we can define a stack and we can define a stack similar to how we
‫define any collection.
‫So here you have the stack keyword and in order to use text, you need to add the following namespace.
‫So using collections generic so you can see stacks are generic collections and this means that they
‫have the advantages and disadvantages of generic collections.
‫So we need to define the type that we want to stack.
‫And this example, I'm going to stack a bunch of ints in this stack that I called Stack.
‫You could call this my stack or however you want, but basically this is going to be my stack that I
‫now want to add data to.
‫In order to add a value to your stack, you need to use this push method so inserts an object at the
‫top of the stack.
‫So in our case the object will be of type integer.
‫So you can just say push and then enter the into item that you want to add to it and let's say I as
‫the one to it.
‫So this will push one to our stack.
‫Now, if you want to see what the stack has at the top level, we can peek at it.
‫So this will return the element at the top of the stack without removing it.
‫So this is stacked peak.
‫This is the method that will give us in our case this one, because we have nothing else in this stack.
‫And basically this is the item that is at the top of the stack.
‫So if we were to add more items here, then of course, the last item that was added to the stack would
‫be the one that is going to be displayed using this right line statement.
‫So let's look at it real quick to see what we get there.
‫And you can see the top value in the stack is one.
‫Now, if we were to add another value here, so one, three, three, seven, for example, and we run
‫this again, then the item at the top of the stack will be one, three, three, seven.
‫So now let's use push in order to add a bunch of items to the stack and then also peek at them, because
‫this will show you that every single time you peek, you are only going to see the latest item that
‫was added to the stack.
‫So top value in the stack is one, then we push two to it.
‫Top value at the stack is two and so forth.
‫Now if you want to remove an item from the stack, then we can use stack dot pop.
‫This will remove the item and pop will return the item.
‫So in this case it will return an integer so we could store this integer here int my stack item for
‫example.
‫And I can then for example.
‫This played here.
‫And let's call this one hop item.
‫Now let's run it again.
‫And you will see that the three will be popped.
‫Now, if we were to peak once again.
‫So here, let's pick once again.
‫After we pop the item and you will see that it will be, in fact, two and not three anymore.
‫So that we are item is three and the top value in the stack is going to be two.
‫So you can store the item using pop and you can then use it, but it's not going to be in the stack
‫anymore.
‫So this is how you remove an item from the stack.
‫Now you have to be super careful with this, however, because if you try to do it here at the top where
‫we have no items in the stack, this will cause an error.
‫So let's run this real quick.
‫And you could see nothing happens here and our application is in fact crashing.
‫So we get a invalid operation exception here because we cannot pop from an empty stack.
‫There's nothing to pop from.
‫So it's always useful to check first.
‫So only if the stack count.
‫So the amount of items is greater than zero is when I want to execute this.
‫So that's something that you might want to take into consideration when using stacks that you always
‫check first if there is anything to pop, if you want to pop an item or delete an item from the stack.
‫So for example, we could create a wide loop that will delete items from the stack until there are no
‫more items left.
‫So please go ahead and try this for yourself.
‫Create a little while loop.
‫That will do exactly what I said.
‫So basically we'll get rid of all the items and then stop executing once it hopped all of the items
‫from the stack.
‫Okay.
‫I hope you tried it.
‫Basically, we're going to check.
‫Is the step count greater than zero?
‫If that's the case, then pop the item from the stack.
‫Also, we can display it directly on our console and we're also going to display the remaining amount
‫of items at the same time.
‫So let's run this real quick and we will see how it removes the three.
‫Then there are only two items left, then it removes the two one item left, and then it removes the
‫one and no items left.
‫So as we can see, the elements we pop out of the stack is in the opposite way.
‫We push them in, so it's in reverse.
‫So that means that we are using the leaf concept here last in, first out, which basically means that
‫the last element that entered the stack will be the first element to exit the stack.
‫So let's have a look at reversing an array.
‫So now you see how stacks work, how you can get rid of items or you can add items and so forth.
‫So let's consider this array that we have here.
‫So it's a numbers array with a bunch of integers.
‫So now I would like to reverse them.
‫So let's go ahead and define a stack that will take care of that.
‫So first of all, we need to define a new stack.
‫And actually you could try this for yourself.
‫So try to build this little application yourself where the numbers are going to be reversed.
‫So the numbers inside of this numbers array should be reversed and should just be displayed in reverse
‫order.
‫So you don't need to store them in a separate array.
‫You shall just display them in reverse order in your console.
‫Okay.
‫So first of all, I'm going to display the items that I have in my numbers array.
‫So here the numbers in the ray are we're using a for each loop that displays the numbers and we are
‫also adding them to the stack this way.
‫So we're pushing them to the stack.
‫This is one way to do it.
‫Of course, there are other ways you could have done this, but that's just how we do it.
‫We display the number and then an empty space, and then we add that item to our stack.
‫So this my stack that we created, and then we just need to make sure that we are displaying the numbers
‫in reverse.
‫So here I'm using console right line to create a line break.
‫And then I'm saying the numbers in reverse are and then we display the numbers.
‫Therefore we check if the stack has still items in it and as long as the count is greater, zero.
‫We know that there are still items in the stack and we pop the last item.
‫We store it as an integer and then we display it.
‫Now, of course, you could have skipped this step and directly done it like this where you pop the
‫item straight away.
‫That would have been an alternative way, but now this is a way where you could then reuse this item
‫because now you have this number, even though in our particular case, of course, we're not using
‫number anywhere, but in case you wanted to use it, you know how to store it somewhere.
‫All right.
‫So now let's run the application and actually see if that worked out.
‫So you can see this is our array and you see this is the reverse version of it.
‫So two six, 743, two, eight.
‫All right.
‫And that's basically how you can reverse and race.
‫So if you ever get the assignment, reverse an array, now you know how to do it.
‫So text are an amazing tool for that.
‫But of course, texts are very useful in many other situations as well whenever you want to use a leaf
‫or system.
‫So the last in first out system, there are plenty of applications out there, real world applications
‫as well where you will actually need this system.
‫All right.
‫So that's it for this video.
‫See you in the next one where we're going to look at Qs.