[No Audio]
Hello and welcome back again. In the previous
tutorial, we implemented the stack abstract
data type. This tutorial will be relatively
short, and we will be looking at a simple
application of stack. The idea is to reverse
the letters inside a certain string. We will
start with the same code that we used in the
previous tutorial. I will also delete all the
code in the main function.
Next, I will assume that for the given input string, first,
we will convert it into character vector and
then we will reverse its elements using Stack.
This means that, instead of
stack containing u32 elements, we
will be requiring a stack containing characters.
So, let me change the implementation of the stack.
I will change the inputs and outputs of these
functions containing u32 type to that of char type.
Let us first changed the new_stack function.
[No Audio]
Next, I will change the
definition of the pop function.
I also do not require the printing statement.
[No Audio]
This is because, I will not be implementing a menu, in
this case, where the user will select a
choice and then apply a certain function.
Let us also make similar changes to the push function.
[No Audio]
I will comment out the print
function inside this function also, for the
same reason as explained a little while ago.
Finally, we will change the definition of
the size function also.
[No Audio]
Please note that, this is not
the most efficient way of manually
changing the data type and all the functions.
An efficient way for this is, to make the use
of generics, which we will cover later on in
the course. Okay, now let us write the code
inside the main function.
[No Audio]
First, I will define a variable which will hold the input string.
[No Audio]
To achieve the reversal of characters
inside the string, we will push the
individual characters in the input string to
the stack one by one.
Once all the characters
are being pushed and stored on the stack,
then we will pop all the elements from the
stack one by one, and we'll add them to a new
[No Audio]
contain the reverse form of the input string,
due to the last in first out order of stack.
For instance, the character t will be the
last one which is pushed, and will therefore
be the first one to being popped
out from the stack. The character s will be
the next character which will be popped out,
and this way the w will be the last character
which will be popped out. By storing the popped
out elements in a string, we will achieve the
reverse of the input string. Let us code this now.
First, I will declare a variable size_stack,
which will store the size of the input string.
[No Audio]
Next, I will create a stack equal to the
number of characters in the input string.
So I will call the new_stack function,
and we'll store the result on stack
in the variable of stack.
[No Audio]
We will next create an empty string, which
will store the final reverse form of the
input string. I will call it rev_string,
and we'll initialize it as a new empty string.
[No Audio]
Now we will iterate, through all the
individual characters in the input string one
by one, and we'll push them on to the created stack.
So I will first include a simple
for loop, which will iterate as many times as
there are characters in the variable input_string.
During each iteration, I
will call the push function for pushing the
characters one by one on to the stack.
[No Audio]
This will essentially push the character
sequentially, one by one onto the stack.
Now we will write the code for reversing the
string. I will use another for loop. which will
iterate as many times as there are elements
in the stack. And the size function returns
the number of elements in the stack.
[No Audio]
I'm going to push the popped elements from the stack
in the string rev_string. So first,
I will call the pop and then unwrap the
result, and if we have some item which is
being popped we will push that into the rev_string.
[No Audio]
Please note that, in this case, the push
function will essentially add the elements to
the string, and it is different from the push
function that we defined for the stack.
The rev_string, in this case, is not, is
not a stack but a simple string, and the push
function, when called with the string variable
will add one character to it. The character
we want to push, in this case is, the character
which has been popped out of the stack.
So therefore, we wrote the result of the pop
operation as an input for the push function.
Outside the loop, I will print the original
string and the reverse string.
[No Audio]
Let us execute now.
[No Audio]
It has displayed to us the
input string and also the reverse of the
input string. This application was very
simple and easy. In the next tutorial now, we
will be looking at a bit more complex and
complicated application of stacks called
expression evaluation. Do come back for
learning that and until next tutorial,
Happy Rust Programming.
[No Audio]