 Welcome to this session in the course of data structures and algorithms. We will continue our discussion on use of stacks. In this session, we will very briefly look at the variety of applications for which stacks can be used. Please remember, we have already seen one special application of using stacks for text editing. There are many applications which stacks can be used for. Here is a list of such applications. Recall that we have to handle computational expressions in many computational problems. There are different ways in which such expressions can be written and depending upon the way in which the expression is written, the evaluation of the expression is to be carried out in a certain fashion. There are in fix expression style of writing which we commonly write. We shall see what it is in this session. There are also post fix and prefix ways of writing the expressions. There are many other applications such as backtracking applications. Note that if we are doing a maze traversal, many times when we hit a dead end, we have to backtrack and go through some other path. Exactly if we are implementing a program to play chess, we will have to examine various alternatives. When we examine a particular move and its consequences and we find that that particular move is not good, we have to backtrack and go to the previous position. In all such cases of backtracking, stacks can be effectively used. Memory management is an intrinsically difficult task which the operating systems have to perform. Curiously, stacks also find a way to implement such memory management functions with gradients. In short, there are many applications for which stacks can be used. In this particular session, we are going to look at the expression handling capability using stacks. This is what we shall be studying in the next few sessions, seeing how the STL stack class library can be used to implement the expression evaluation functionality. We begin with the basics. The in-fix expression or the expressions as we write them are normally used in arithmetic, where the operator is placed between the operands. So, a plus b for example. Now, whenever we have a sequence of operations, parenthesis are used to define the priority of the operation. Here is an example, a plus b is an expression. A plus b star a minus b is an expression in which clearly a plus b and a minus b needs to be evaluated first and then the result has to be multiplied. Here is another example with greater priority being indicated for some operations. For example, d star is to be done first, then this is to be followed by c plus, this value minus f. Obviously, there is no parenthesis here. So, there is a defined priority order on all the operators, which is to be implemented by us. For example, whenever there are operators, the same priority, we assume that the operations are performed from left to right. In short, this is the way we write expressions and this is the way we evaluate expressions. Now, when a computer has to evaluate these expressions, there could be problems in deciphering various opening brackets, closing brackets and matching those parenthesis. This activity is called parsing. A much easier way for the computers to evaluate such expressions is to rewrite these expressions in different forms. Let us examine these forms. First is a post fix expression notation. It is a mathematical notation, where the operator is placed after all its operands. There are no need of parenthesis in such a notation. Here is an example. If I have a plus b as an infix expression, I will write it as a b plus means the same thing. If I have a plus b in parenthesis multiplied by a minus b in parenthesis in infix expression, I will write it as a b plus a b minus star. Notice what it means. a b plus means if a and b to be added. This will result in something say called c, but then I have followed by a b again. a b minus is forming a proper post fix composition. So, it will subtract b from a. And result c, which I had obtained, the result d, which I had obtained here, will be multiplied because of this operation star. In exactly the same way, you can analyze that this infix operation will actually be equivalent to this post fix operation. How do we evaluate post fix expressions? Well, post fix expressions are evaluated by using the same logic that I just explained. For example, if a b plus a b minus star. So, it will first evaluate a b plus. Assume that a is 5, b is 3. It will evaluate 5, 3 plus. It will get the result 8. It will then evaluate 5, 3 minus. It will get the result 2. And then it will reduce this expression to 8, 2 star, which means 16, which is the answer. Here is a slightly larger example with values of a, b, c, d, e, f and g given as 4, 2, 5, 6, 7 and 3. You can convince yourself by going through these steps. This is the post fix expression, for example, which was shown in the previous slide. So, here if I replace all the values, this is how the expression will look like. In evaluating this expression, first this expression will be evaluated. 4 raise to 2 will result in 16. Then you have 5, 6, 7 star. 6, 7 star will be evaluated. Next, you will get 42. The entire expression now reduces to this part. Notice that in this part, 5, 42 plus is the valid combination of operators and operator. So, this will be resulting in 47. Now, I have this resulting expression. In this, I notice 47, 8 minus from the correct combination of operators and operator. This will result in a value 39. And I will have 16, 39 star to be the next operator. I will get 6, 24. The final expression reduces to 6, 24, 3 slash and this will be evaluated to get the answer 208. Notice that in this entire assessment, we did not require any parenthesis to define the priority of activity. We will quickly look at how the prefix expressions are evaluated, just like we saw the post fix expression evaluation. We will just look at one example. The first example where we assume a is 5 and b is 3. So, our expression was star plus a b minus a b. So, look at what will happen when the values are substituted. I get star plus 5, 3 minus 5, 3. Clearly, this plus 5, 3 is to be first executed. I will get the value 8. Minus 5, 3 will be executed. So, 5 minus 3 will be 2. And now, I will have this reduced expression star 8, 2. 8 and 2 will be multiplied getting an answer. You can satisfy yourself that in exactly the same fashion, these shall be the steps in which this more complex expression will be evaluated. We have now seen how prefix expression and post fix expressions can be evaluated. We have also seen our normal form of writing in fix expressions. In subsequent sessions, we shall see C plus plus implementation using the stack class on how the prefix and post fix expression evaluation is handled. For the time being, I request you to look at the following references. We have already indicated some of these references earlier, the stack reference particularly. But the in fix notation, the reverse polish notation and the polish notation are useful references for understanding more about the post fix and prefix kind of expression writing that we have shown in these examples. Of course, those of you are not interested in going through these details can skip these. If you have understood the in fix and post fix basic principles is good enough for us to understand the subsequent implementation that we shall discuss in the next few sessions. Thank you very much.