 Hello everyone, I am Mrs. Meenakshi Shrigandhi from Valchand Institute of Technology, Sholapur. Welcome to the video lecture on stack application, expression evaluation. Learning outcome, at the end of this session, students will be able to illustrate stack application for expression evaluation. Applications of stack, as we already know that stack is the default structure. That is, the last item to be added is the first to be removed. Its application can be seen in execution of sub-programs in a computer system, where a sub-program before execution of its task, it pushes its written address on the stack and after its execution of the task, it comes back to the stack and pops a written address and goes to the main function. The application of stack can also be seen in recursion. Recursion is nothing but when a function called itself. So here also, when a function before execution of its task, it says its written address on the stack and after the execution of its task, it comes back to the stack and pops a written address and goes to the main function. The application of stack can also be seen in expression evaluation of postfix expression and also can be seen in expression conversion from infix notation to postfix notation. Let's see what is infix, postfix and prefix is. It refers to the relative position of the operator with respect to the two operands. If the operator is placed in between the two operands, it is called as infix. If the operator is placed after the two operands, it is called as the postfix. If the operator is placed before the two operands, it is called as the prefix. Expression evaluation, the expression evaluation is usually done for postfix expression using stack and while we are evaluating the expression, we need to scan the expression from left to right. Let's see the algorithm for expression evaluation of postfix string. First we need to clear the stack, then we need to take the next input character from the string and place it in the symbol. While we are not reaching to the end of the input string, if the symbol is an operand, then we need to push it on the stack. If the symbol is not an operand, but if it is an operator, then in that case we need to pop the two operands from the stack and we need to perform the operation between these two operands and whatever the result we get, this need to be pushed on the stack. As we need to take the next input character, this process is repeated till we are not reaching to the end of the input. Once we come to the end of the input, then we need to pop the stack to get the result for the postfix string. Based on the algorithm, try to think an answer to evaluate the following postfix expression for phi 98 plus 46 multiplication operator plus 7 minus multiplication operator. Here in this, we are trying to evaluate the postfix expression for phi 98 plus 46 multiplication operator plus 7 minus multiplication operator. For evaluating this, we need a table consisting of five columns consisting of symbol, operand 1, operand 2, result and stack. So let's take the first input from the input string. So the symbol is 5. As it is an operand, we need to push it on the stack. So the stack consists of one element that is fine. Let's take the next symbol that is 9. It is also an operand. So it is pushed on the stack. So the stack consists of two elements that is 9 and 5, 9 being the top of the stack. The next symbol is 8. As it is an operand, it is again pushed on the stack. So the stack consists of three elements, 5, 9 and 8. 8 being the top of the stack. The next symbol is plus. Now plus is an operator. In this case, we need to pop the top two elements from the stack. So the top two elements from the stack are 9 and 8. Now we need to perform the addition operation on these two operands. The result comes to be 17. Now this result should be pushed on the stack. So the stack elements are 17 and 5, 17 being the top of the stack. The next symbol is 4. For being the operand, it is pushed on the stack. So the stack now contains three elements, 5, 17 and 4, for being the top of the stack. The next input symbol is 6. So 6 is also an operand. So it is pushed on the stack. So stack consists of four elements, that is 6, 4, 17, 5, 6 being the top of the stack. The next symbol is multiplication operator. As it is an operator, we need to pop the top two elements from the stack. Those are 6 and 4. Now perform the multiplication between these two operands. The result comes to be 24. This should be pushed on the stack. Now 24 being the top of the stack. The next symbol is plus. So again we need to pop the top two elements from the stack. Those are 17 and 24. Now perform the addition between these two operands. The result comes to be 41. Now this result should be pushed on the stack. Now the stack consists of two elements, that is 41 and 5, 41 being the top of the stack. The next symbol is the 7. As 7 is an operand, we need to push it on the stack. So the stack consists of three elements, that is 5, 41 and 7. 7 being the top of the stack. The next symbol is minus. As it is an operator, we need to pop the top two elements from the stack, that is 41 and 7. Now perform the subtraction between these two operands. Now the result comes to be 34. This should be pushed on the stack. So the stack consists of two elements, 34 and 5, 34 being the top of the stack. The next symbol is the multiplication operator. Now again, as it is an operator, pop the top two elements from the stack, that is 34 and 5. Perform the multiplication between these two operands. The result is 170. This should be pushed on the stack. So the stack now contains only one element, that is 170, which is the result for this postfix expression. Based on the previous example, try to think and answer to evaluate the following postfix expression 98 plus 382, division operator, multiplication operator 2 plus minus. Here we are trying to evaluate the postfix expression of 98 plus 382, division operator, multiplication operator 2 plus minus. Now the first symbol in this string is 9, which is an operand. So push it on the stack. The next symbol in the input string is 8, as it is an operand. Again push it on the stack. So the stack now consists of two elements, 9 and 8, 8 being the top of the stack. The next symbol is plus, as it is an operator. Of the top two elements from the stack, that is 9 and 8, perform the addition between these two operands. The result comes to be 17. Push it on the stack. So the stack now contains only one element, 17 being the top of the stack. The next symbol is 3, as it is an operand. Push it on the stack. So 3 being the top of the stack. Next symbol is 8. Again push it on the stack. So the stack consists of three elements, 17, 3 and 8, 8 being the top of the stack. Next symbol is 2, as it is an operand. Push it on the stack, 2 being the top of the stack. The next symbol is the division operator. As it is an operator, pop the top two elements from the stack, those are 8 and 2. Perform the division between these two operands. The result comes to be 4. Now push this result on the stack. So the stack now contains 17, 3 and 4, 4 being the top of the stack. The next symbol is the multiplication operator. As it is an operator, again pop the top two elements from the stack, that is 3 and 4. Perform the multiplication between these two operands. So the result comes to be 12. Push it on the stack. The next symbol is 2. As it is an operand, push it on the stack. So the stack consists of 17, 12 and 2, 2 is the top of the stack. The next symbol is plus. As it is an operator, pop the top two elements from the stack, that is 12 and 2. Perform the addition between these two operands, that is, the result comes to be 14. This should now be pushed back to the stack. So the stack elements are 17 and 14, 14 is the top of the stack. The next symbol is minus. As it is an operator, again we need to pop the top two elements from the stack, that is 17 and 14. The result comes to be 3, which is pushed on the stack. Now the stack contains only one element. This is the result for the post-fix expression. These are my references. Thank you.