1 00:00:00,000 --> 00:00:07,366 [No Audio] 2 00:00:07,367 --> 00:00:10,066 Hello and welcome back again. In the previous 3 00:00:10,067 --> 00:00:12,466 tutorial, we implemented the stack abstract 4 00:00:12,467 --> 00:00:14,733 data type. This tutorial will be relatively 5 00:00:14,734 --> 00:00:16,600 short, and we will be looking at a simple 6 00:00:16,601 --> 00:00:19,366 application of stack. The idea is to reverse 7 00:00:19,367 --> 00:00:21,900 the letters inside a certain string. We will 8 00:00:21,901 --> 00:00:23,666 start with the same code that we used in the 9 00:00:23,667 --> 00:00:26,866 previous tutorial. I will also delete all the 10 00:00:26,867 --> 00:00:28,166 code in the main function. 11 00:00:29,366 --> 00:00:32,900 Next, I will assume that for the given input string, first, 12 00:00:32,933 --> 00:00:35,666 we will convert it into character vector and 13 00:00:35,667 --> 00:00:38,466 then we will reverse its elements using Stack. 14 00:00:38,966 --> 00:00:41,300 This means that, instead of 15 00:00:41,301 --> 00:00:43,866 stack containing u32 elements, we 16 00:00:43,867 --> 00:00:46,200 will be requiring a stack containing characters. 17 00:00:46,933 --> 00:00:49,066 So, let me change the implementation of the stack. 18 00:00:49,633 --> 00:00:51,689 I will change the inputs and outputs of these 19 00:00:51,690 --> 00:00:54,633 functions containing u32 type to that of char type. 20 00:00:55,566 --> 00:00:58,048 Let us first changed the new_stack function. 21 00:00:58,049 --> 00:01:01,866 [No Audio] 22 00:01:01,867 --> 00:01:03,133 Next, I will change the 23 00:01:03,134 --> 00:01:04,933 definition of the pop function. 24 00:01:06,715 --> 00:01:09,100 I also do not require the printing statement. 25 00:01:09,106 --> 00:01:11,533 [No Audio] 26 00:01:11,534 --> 00:01:14,366 This is because, I will not be implementing a menu, in 27 00:01:14,367 --> 00:01:16,133 this case, where the user will select a 28 00:01:16,134 --> 00:01:17,833 choice and then apply a certain function. 29 00:01:19,100 --> 00:01:21,951 Let us also make similar changes to the push function. 30 00:01:21,952 --> 00:01:26,500 [No Audio] 31 00:01:26,501 --> 00:01:27,733 I will comment out the print 32 00:01:27,734 --> 00:01:30,133 function inside this function also, for the 33 00:01:30,134 --> 00:01:32,633 same reason as explained a little while ago. 34 00:01:34,066 --> 00:01:35,800 Finally, we will change the definition of 35 00:01:35,801 --> 00:01:37,173 the size function also. 36 00:01:37,174 --> 00:01:41,187 [No Audio] 37 00:01:41,188 --> 00:01:42,466 Please note that, this is not 38 00:01:42,467 --> 00:01:44,433 the most efficient way of manually 39 00:01:44,434 --> 00:01:46,733 changing the data type and all the functions. 40 00:01:47,166 --> 00:01:49,300 An efficient way for this is, to make the use 41 00:01:49,301 --> 00:01:51,500 of generics, which we will cover later on in 42 00:01:51,501 --> 00:01:53,933 the course. Okay, now let us write the code 43 00:01:53,934 --> 00:01:55,200 inside the main function. 44 00:01:55,201 --> 00:01:57,366 [No Audio] 45 00:01:57,367 --> 00:02:00,366 First, I will define a variable which will hold the input string. 46 00:02:00,387 --> 00:02:02,600 [No Audio] 47 00:02:02,601 --> 00:02:04,500 To achieve the reversal of characters 48 00:02:04,501 --> 00:02:05,966 inside the string, we will push the 49 00:02:05,967 --> 00:02:08,100 individual characters in the input string to 50 00:02:08,101 --> 00:02:09,433 the stack one by one. 51 00:02:10,199 --> 00:02:11,400 Once all the characters 52 00:02:11,401 --> 00:02:13,266 are being pushed and stored on the stack, 53 00:02:13,300 --> 00:02:15,300 then we will pop all the elements from the 54 00:02:15,301 --> 00:02:17,633 stack one by one, and we'll add them to a new 55 00:02:17,634 --> 00:02:20,366 [No Audio] 56 00:02:20,367 --> 00:02:22,400 contain the reverse form of the input string, 57 00:02:22,401 --> 00:02:24,766 due to the last in first out order of stack. 58 00:02:25,233 --> 00:02:27,500 For instance, the character t will be the 59 00:02:27,501 --> 00:02:30,433 last one which is pushed, and will therefore 60 00:02:30,434 --> 00:02:33,166 be the first one to being popped 61 00:02:33,200 --> 00:02:35,866 out from the stack. The character s will be 62 00:02:35,867 --> 00:02:37,866 the next character which will be popped out, 63 00:02:37,867 --> 00:02:40,466 and this way the w will be the last character 64 00:02:40,467 --> 00:02:42,966 which will be popped out. By storing the popped 65 00:02:42,967 --> 00:02:45,566 out elements in a string, we will achieve the 66 00:02:45,567 --> 00:02:49,766 reverse of the input string. Let us code this now. 67 00:02:49,900 --> 00:02:52,333 First, I will declare a variable size_stack, 68 00:02:52,334 --> 00:02:54,471 which will store the size of the input string. 69 00:02:54,472 --> 00:03:00,233 [No Audio] 70 00:03:00,234 --> 00:03:02,033 Next, I will create a stack equal to the 71 00:03:02,034 --> 00:03:03,800 number of characters in the input string. 72 00:03:03,801 --> 00:03:06,233 So I will call the new_stack function, 73 00:03:06,234 --> 00:03:08,033 and we'll store the result on stack 74 00:03:08,066 --> 00:03:09,660 in the variable of stack. 75 00:03:09,661 --> 00:03:15,433 [No Audio] 76 00:03:15,434 --> 00:03:17,300 We will next create an empty string, which 77 00:03:17,301 --> 00:03:19,033 will store the final reverse form of the 78 00:03:19,034 --> 00:03:22,233 input string. I will call it rev_string, 79 00:03:22,234 --> 00:03:24,520 and we'll initialize it as a new empty string. 80 00:03:24,521 --> 00:03:29,033 [No Audio] 81 00:03:29,034 --> 00:03:30,866 Now we will iterate, through all the 82 00:03:30,867 --> 00:03:32,966 individual characters in the input string one 83 00:03:32,967 --> 00:03:35,800 by one, and we'll push them on to the created stack. 84 00:03:36,566 --> 00:03:39,133 So I will first include a simple 85 00:03:39,134 --> 00:03:42,000 for loop, which will iterate as many times as 86 00:03:42,001 --> 00:03:44,866 there are characters in the variable input_string. 87 00:03:45,233 --> 00:03:47,233 During each iteration, I 88 00:03:47,234 --> 00:03:49,066 will call the push function for pushing the 89 00:03:49,067 --> 00:03:50,851 characters one by one on to the stack. 90 00:03:50,852 --> 00:03:55,300 [No Audio] 91 00:03:55,301 --> 00:03:57,200 This will essentially push the character 92 00:03:57,201 --> 00:03:59,233 sequentially, one by one onto the stack. 93 00:04:00,366 --> 00:04:02,566 Now we will write the code for reversing the 94 00:04:02,567 --> 00:04:04,900 string. I will use another for loop. which will 95 00:04:04,933 --> 00:04:07,233 iterate as many times as there are elements 96 00:04:07,234 --> 00:04:09,666 in the stack. And the size function returns 97 00:04:09,667 --> 00:04:11,160 the number of elements in the stack. 98 00:04:11,161 --> 00:04:15,566 [No Audio] 99 00:04:15,567 --> 00:04:18,100 I'm going to push the popped elements from the stack 100 00:04:18,101 --> 00:04:21,833 in the string rev_string. So first, 101 00:04:21,834 --> 00:04:24,500 I will call the pop and then unwrap the 102 00:04:24,501 --> 00:04:27,166 result, and if we have some item which is 103 00:04:27,167 --> 00:04:30,466 being popped we will push that into the rev_string. 104 00:04:30,496 --> 00:04:36,166 [No Audio] 105 00:04:36,167 --> 00:04:37,900 Please note that, in this case, the push 106 00:04:37,901 --> 00:04:39,833 function will essentially add the elements to 107 00:04:39,834 --> 00:04:41,800 the string, and it is different from the push 108 00:04:41,801 --> 00:04:43,833 function that we defined for the stack. 109 00:04:44,166 --> 00:04:47,066 The rev_string, in this case, is not, is 110 00:04:47,067 --> 00:04:49,733 not a stack but a simple string, and the push 111 00:04:49,734 --> 00:04:52,500 function, when called with the string variable 112 00:04:52,501 --> 00:04:55,466 will add one character to it. The character 113 00:04:55,500 --> 00:04:57,700 we want to push, in this case is, the character 114 00:04:57,701 --> 00:04:59,333 which has been popped out of the stack. 115 00:04:59,334 --> 00:05:01,600 So therefore, we wrote the result of the pop 116 00:05:01,601 --> 00:05:04,500 operation as an input for the push function. 117 00:05:05,400 --> 00:05:07,400 Outside the loop, I will print the original 118 00:05:07,401 --> 00:05:08,766 string and the reverse string. 119 00:05:08,767 --> 00:05:14,033 [No Audio] 120 00:05:14,034 --> 00:05:15,613 Let us execute now. 121 00:05:15,614 --> 00:05:19,929 [No Audio] 122 00:05:19,930 --> 00:05:21,233 It has displayed to us the 123 00:05:21,234 --> 00:05:23,366 input string and also the reverse of the 124 00:05:23,367 --> 00:05:26,300 input string. This application was very 125 00:05:26,301 --> 00:05:28,600 simple and easy. In the next tutorial now, we 126 00:05:28,601 --> 00:05:30,466 will be looking at a bit more complex and 127 00:05:30,467 --> 00:05:32,533 complicated application of stacks called 128 00:05:32,566 --> 00:05:35,366 expression evaluation. Do come back for 129 00:05:35,367 --> 00:05:37,347 learning that and until next tutorial, 130 00:05:37,348 --> 00:05:38,741 Happy Rust Programming. 131 00:05:38,742 --> 00:05:43,733 [No Audio]