Working from left to right, one token at a time, determine whether or not the current token is an operand, an operator, or an opening or closing parenthesis. If the token is an operand, append it to the postfix expression. Operands always appear in the same order in the postfix expression as they do in the infix expression. If the token is an opening parenthesis, push it to the stack. This marks the beginning of an expression that should be evaluated separately.
|Published (Last):||18 February 2017|
|PDF File Size:||10.25 Mb|
|ePub File Size:||13.8 Mb|
|Price:||Free* [*Free Regsitration Required]|
Here the input is our postfix expression. You can get the expression from the keyboard and use the enter key to finish the expression. This element can be operator or operand. The operand needs not to be single digit. It may be of two digits or even more like 60 or etc. Therefore we will pop the two elements and apply that operator. After exiting the loop, a programmer may have only one element in the stack.
We pop this element which is the final result. As stack is a LIFO structure, so we get 2 first and then 3 as a result of pop. In case of getting the next input as operand, we will push it on the stack otherwise we will pop the two operands and apply the operator on these. So two operands will be popped from the stack i. We will apply the operator plus on these and push the result i.
The input is finished. Now we will pop the stack to get the final result i. Postfix Expression solved by Example In the earlier example, we have used the stack to solve the postfix expression. We have five columns here i. We will run our pseudo code program. In the start, we read the input as a result we get number 6. As 6 is operand, so it will be pushed on the stack. Then we have number 2 which will also be pushed on the stack.
Now 2 is the most recent element. The next element is the number 3 that will also be pushed on the stack. Now, there are three elements on the stack i. The number 3 is the most recent.
On popping, we will get the number 3 first of all. Now the else part of our pseudo code is executed. The number 3 will be stored in variable op2 and number 2 in op1. Now we will push the value i. Now we have two numbers on the stack i. The number 5 is the most recent element. As it is also an operator, so we will pop the two elements from the stack i. Now we have 5 in op2 and 6 in op1. On applying the operator - , we will get the result as 1 The result 1 will be pushed on stack.
Now on the stack, we have only one element i. Next three elements are operands so we pushed 3, 8 and 2 on the stack. The most recent element is 2. The next input is an operator in the expression i. The number 2 will be stored in op2 while number 8 in op1.
We push the result on the stack. We have, now, three elements i. We will pop the two elements i. The result 7 will be pushed on the stack.
You have noted that whenever we have an operator in the input expression, we have two or more elements on the stack. As the operators we are using are binary and we need two operands for them. It will never be the case that you want to pop two elements from the stack and there is only one or no element on the stack.
If this happens than it means there is an error in the program and you have popped more values than required. The next input element is 2 that is pushed on the stack. So we will pop the two elements, op2 will hold 2 and op1 will have the number 7. We have, now, the number 3 in the element being pushed on the stack. So we pop the two elements i. The input expression is finished, resulting in the final result i. This the tabular form of the evaluation of the postfix expression.
Convert Postfix to Infix Expression
Prefix to Postfix Conversion
Infix / Postfix converter