1 00:00:00,950 --> 00:00:06,590 ‫In this video, you will learn about how to use tanks in C-sharp, so how you can add data to a stack, 2 00:00:06,590 --> 00:00:09,740 ‫how you can edit data in a stack and so forth. 3 00:00:09,890 --> 00:00:17,330 ‫So first of all, let's look at how we can define a stack and we can define a stack similar to how we 4 00:00:17,330 --> 00:00:18,940 ‫define any collection. 5 00:00:18,950 --> 00:00:27,320 ‫So here you have the stack keyword and in order to use text, you need to add the following namespace. 6 00:00:27,320 --> 00:00:33,440 ‫So using collections generic so you can see stacks are generic collections and this means that they 7 00:00:33,440 --> 00:00:36,530 ‫have the advantages and disadvantages of generic collections. 8 00:00:36,920 --> 00:00:40,160 ‫So we need to define the type that we want to stack. 9 00:00:40,160 --> 00:00:45,770 ‫And this example, I'm going to stack a bunch of ints in this stack that I called Stack. 10 00:00:46,040 --> 00:00:50,840 ‫You could call this my stack or however you want, but basically this is going to be my stack that I 11 00:00:50,870 --> 00:00:53,390 ‫now want to add data to. 12 00:00:53,420 --> 00:00:59,690 ‫In order to add a value to your stack, you need to use this push method so inserts an object at the 13 00:00:59,690 --> 00:01:01,280 ‫top of the stack. 14 00:01:01,430 --> 00:01:04,010 ‫So in our case the object will be of type integer. 15 00:01:04,010 --> 00:01:11,300 ‫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 16 00:01:11,300 --> 00:01:12,680 ‫the one to it. 17 00:01:12,680 --> 00:01:15,740 ‫So this will push one to our stack. 18 00:01:18,490 --> 00:01:25,420 ‫Now, if you want to see what the stack has at the top level, we can peek at it. 19 00:01:25,420 --> 00:01:29,620 ‫So this will return the element at the top of the stack without removing it. 20 00:01:29,620 --> 00:01:31,230 ‫So this is stacked peak. 21 00:01:31,240 --> 00:01:38,110 ‫This is the method that will give us in our case this one, because we have nothing else in this stack. 22 00:01:38,110 --> 00:01:42,100 ‫And basically this is the item that is at the top of the stack. 23 00:01:42,670 --> 00:01:48,190 ‫So if we were to add more items here, then of course, the last item that was added to the stack would 24 00:01:48,190 --> 00:01:52,930 ‫be the one that is going to be displayed using this right line statement. 25 00:01:52,930 --> 00:01:56,770 ‫So let's look at it real quick to see what we get there. 26 00:01:56,770 --> 00:02:00,070 ‫And you can see the top value in the stack is one. 27 00:02:00,190 --> 00:02:06,970 ‫Now, if we were to add another value here, so one, three, three, seven, for example, and we run 28 00:02:06,970 --> 00:02:11,980 ‫this again, then the item at the top of the stack will be one, three, three, seven. 29 00:02:12,730 --> 00:02:21,700 ‫So now let's use push in order to add a bunch of items to the stack and then also peek at them, because 30 00:02:21,700 --> 00:02:27,250 ‫this will show you that every single time you peek, you are only going to see the latest item that 31 00:02:27,250 --> 00:02:28,690 ‫was added to the stack. 32 00:02:28,750 --> 00:02:32,620 ‫So top value in the stack is one, then we push two to it. 33 00:02:32,890 --> 00:02:35,290 ‫Top value at the stack is two and so forth. 34 00:02:37,760 --> 00:02:45,290 ‫Now if you want to remove an item from the stack, then we can use stack dot pop. 35 00:02:45,860 --> 00:02:49,880 ‫This will remove the item and pop will return the item. 36 00:02:49,880 --> 00:02:57,800 ‫So in this case it will return an integer so we could store this integer here int my stack item for 37 00:02:57,800 --> 00:02:58,580 ‫example. 38 00:02:58,580 --> 00:03:01,340 ‫And I can then for example. 39 00:03:02,220 --> 00:03:03,510 ‫This played here. 40 00:03:04,190 --> 00:03:06,920 ‫And let's call this one hop item. 41 00:03:07,550 --> 00:03:08,570 ‫Now let's run it again. 42 00:03:08,570 --> 00:03:12,470 ‫And you will see that the three will be popped. 43 00:03:12,890 --> 00:03:15,500 ‫Now, if we were to peak once again. 44 00:03:15,830 --> 00:03:18,230 ‫So here, let's pick once again. 45 00:03:18,890 --> 00:03:24,740 ‫After we pop the item and you will see that it will be, in fact, two and not three anymore. 46 00:03:27,750 --> 00:03:32,190 ‫So that we are item is three and the top value in the stack is going to be two. 47 00:03:32,490 --> 00:03:39,300 ‫So you can store the item using pop and you can then use it, but it's not going to be in the stack 48 00:03:39,300 --> 00:03:39,870 ‫anymore. 49 00:03:39,870 --> 00:03:42,870 ‫So this is how you remove an item from the stack. 50 00:03:44,870 --> 00:03:50,000 ‫Now you have to be super careful with this, however, because if you try to do it here at the top where 51 00:03:50,000 --> 00:03:53,780 ‫we have no items in the stack, this will cause an error. 52 00:03:53,810 --> 00:03:55,220 ‫So let's run this real quick. 53 00:03:56,060 --> 00:04:00,980 ‫And you could see nothing happens here and our application is in fact crashing. 54 00:04:00,980 --> 00:04:06,640 ‫So we get a invalid operation exception here because we cannot pop from an empty stack. 55 00:04:06,650 --> 00:04:08,150 ‫There's nothing to pop from. 56 00:04:08,150 --> 00:04:11,570 ‫So it's always useful to check first. 57 00:04:11,570 --> 00:04:15,800 ‫So only if the stack count. 58 00:04:15,800 --> 00:04:22,430 ‫So the amount of items is greater than zero is when I want to execute this. 59 00:04:22,580 --> 00:04:27,890 ‫So that's something that you might want to take into consideration when using stacks that you always 60 00:04:27,890 --> 00:04:34,580 ‫check first if there is anything to pop, if you want to pop an item or delete an item from the stack. 61 00:04:37,020 --> 00:04:43,920 ‫So for example, we could create a wide loop that will delete items from the stack until there are no 62 00:04:43,920 --> 00:04:45,130 ‫more items left. 63 00:04:45,150 --> 00:04:48,240 ‫So please go ahead and try this for yourself. 64 00:04:48,630 --> 00:04:50,670 ‫Create a little while loop. 65 00:04:50,910 --> 00:04:53,150 ‫That will do exactly what I said. 66 00:04:53,160 --> 00:04:59,250 ‫So basically we'll get rid of all the items and then stop executing once it hopped all of the items 67 00:04:59,250 --> 00:05:00,030 ‫from the stack. 68 00:05:01,510 --> 00:05:01,980 ‫Okay. 69 00:05:02,020 --> 00:05:03,550 ‫I hope you tried it. 70 00:05:03,580 --> 00:05:05,710 ‫Basically, we're going to check. 71 00:05:05,950 --> 00:05:07,720 ‫Is the step count greater than zero? 72 00:05:07,750 --> 00:05:11,300 ‫If that's the case, then pop the item from the stack. 73 00:05:11,320 --> 00:05:19,900 ‫Also, we can display it directly on our console and we're also going to display the remaining amount 74 00:05:19,900 --> 00:05:21,280 ‫of items at the same time. 75 00:05:21,280 --> 00:05:25,960 ‫So let's run this real quick and we will see how it removes the three. 76 00:05:25,990 --> 00:05:30,790 ‫Then there are only two items left, then it removes the two one item left, and then it removes the 77 00:05:30,790 --> 00:05:33,070 ‫one and no items left. 78 00:05:33,700 --> 00:05:38,860 ‫So as we can see, the elements we pop out of the stack is in the opposite way. 79 00:05:38,860 --> 00:05:41,800 ‫We push them in, so it's in reverse. 80 00:05:41,890 --> 00:05:49,240 ‫So that means that we are using the leaf concept here last in, first out, which basically means that 81 00:05:49,240 --> 00:05:54,130 ‫the last element that entered the stack will be the first element to exit the stack. 82 00:05:54,700 --> 00:05:58,270 ‫So let's have a look at reversing an array. 83 00:05:58,270 --> 00:06:04,380 ‫So now you see how stacks work, how you can get rid of items or you can add items and so forth. 84 00:06:04,390 --> 00:06:07,930 ‫So let's consider this array that we have here. 85 00:06:07,930 --> 00:06:10,690 ‫So it's a numbers array with a bunch of integers. 86 00:06:10,870 --> 00:06:13,480 ‫So now I would like to reverse them. 87 00:06:13,750 --> 00:06:19,840 ‫So let's go ahead and define a stack that will take care of that. 88 00:06:19,840 --> 00:06:22,240 ‫So first of all, we need to define a new stack. 89 00:06:22,240 --> 00:06:24,910 ‫And actually you could try this for yourself. 90 00:06:24,910 --> 00:06:31,810 ‫So try to build this little application yourself where the numbers are going to be reversed. 91 00:06:31,810 --> 00:06:38,020 ‫So the numbers inside of this numbers array should be reversed and should just be displayed in reverse 92 00:06:38,020 --> 00:06:38,710 ‫order. 93 00:06:38,710 --> 00:06:41,530 ‫So you don't need to store them in a separate array. 94 00:06:41,530 --> 00:06:45,910 ‫You shall just display them in reverse order in your console. 95 00:06:46,030 --> 00:06:46,290 ‫Okay. 96 00:06:46,300 --> 00:06:52,030 ‫So first of all, I'm going to display the items that I have in my numbers array. 97 00:06:52,030 --> 00:06:59,410 ‫So here the numbers in the ray are we're using a for each loop that displays the numbers and we are 98 00:06:59,410 --> 00:07:01,840 ‫also adding them to the stack this way. 99 00:07:02,470 --> 00:07:05,080 ‫So we're pushing them to the stack. 100 00:07:05,380 --> 00:07:06,580 ‫This is one way to do it. 101 00:07:06,580 --> 00:07:10,570 ‫Of course, there are other ways you could have done this, but that's just how we do it. 102 00:07:10,840 --> 00:07:16,840 ‫We display the number and then an empty space, and then we add that item to our stack. 103 00:07:16,840 --> 00:07:24,880 ‫So this my stack that we created, and then we just need to make sure that we are displaying the numbers 104 00:07:24,880 --> 00:07:25,690 ‫in reverse. 105 00:07:25,690 --> 00:07:28,690 ‫So here I'm using console right line to create a line break. 106 00:07:28,690 --> 00:07:34,360 ‫And then I'm saying the numbers in reverse are and then we display the numbers. 107 00:07:34,360 --> 00:07:41,110 ‫Therefore we check if the stack has still items in it and as long as the count is greater, zero. 108 00:07:41,110 --> 00:07:46,030 ‫We know that there are still items in the stack and we pop the last item. 109 00:07:46,030 --> 00:07:49,510 ‫We store it as an integer and then we display it. 110 00:07:49,840 --> 00:07:55,690 ‫Now, of course, you could have skipped this step and directly done it like this where you pop the 111 00:07:55,690 --> 00:07:56,980 ‫item straight away. 112 00:07:57,010 --> 00:08:03,250 ‫That would have been an alternative way, but now this is a way where you could then reuse this item 113 00:08:03,250 --> 00:08:08,260 ‫because now you have this number, even though in our particular case, of course, we're not using 114 00:08:08,260 --> 00:08:13,390 ‫number anywhere, but in case you wanted to use it, you know how to store it somewhere. 115 00:08:13,570 --> 00:08:14,020 ‫All right. 116 00:08:14,020 --> 00:08:18,010 ‫So now let's run the application and actually see if that worked out. 117 00:08:18,070 --> 00:08:22,300 ‫So you can see this is our array and you see this is the reverse version of it. 118 00:08:22,300 --> 00:08:26,590 ‫So two six, 743, two, eight. 119 00:08:27,190 --> 00:08:27,520 ‫All right. 120 00:08:27,520 --> 00:08:30,010 ‫And that's basically how you can reverse and race. 121 00:08:30,010 --> 00:08:34,720 ‫So if you ever get the assignment, reverse an array, now you know how to do it. 122 00:08:34,720 --> 00:08:36,550 ‫So text are an amazing tool for that. 123 00:08:36,550 --> 00:08:43,660 ‫But of course, texts are very useful in many other situations as well whenever you want to use a leaf 124 00:08:43,660 --> 00:08:44,140 ‫or system. 125 00:08:44,140 --> 00:08:50,380 ‫So the last in first out system, there are plenty of applications out there, real world applications 126 00:08:50,380 --> 00:08:53,440 ‫as well where you will actually need this system. 127 00:08:54,070 --> 00:08:54,490 ‫All right. 128 00:08:54,490 --> 00:08:56,230 ‫So that's it for this video. 129 00:08:56,260 --> 00:08:59,290 ‫See you in the next one where we're going to look at Qs.