Question: Algorithm for Evaluation of Postfix Expression Create an empty stack and start scanning the postfix expression from left to right. If the element is an




Algorithm for Evaluation of Postfix Expression Create an empty stack and start scanning the postfix expression from left to right. If the element is an operand, push it into the stack. If the element is an operator O, pop twice and get A and B respectively. Calculate BOA and push it back to the stack. When the expression is ended, the value in the stack is the final answer. Evaluation of a postfix expression using a stack is explained in below example: . 2 10 + 96-1 Pop 6 push2 push 10 pop 10 pop 2 push 9 push 6 pop 9 pop3 pop 12 pop answer: 4 push 2 + 10 = 12 push 9 - 6 = 3 push 12 / 3 = 4 10 9 3 2 12 12 12 4 OPERATOR () $$ INFIX FORMAT (OPERAND OPERATOR OPERAND) 2 3 ___> 5 OPERAND OPERATOR OPERAND EX2 3=5+2 ] 15 + 2 EX3 4 * (2 +3) ~ 1 2 ) 3 PREFIX COPERATOR OPERAND OPERAND) , EX1 2 + 3 + 23 EX3 32 3 + 4*3/2 3+ 4_3*/2 3+ 43*2) 34 32/ + EX2 35 +1 57 =1t Hait 1 POSTFIX (OPERAND OPERAND OPERATOR) EX1 3 35+
Step by Step Solution
There are 3 Steps involved in it
Get step-by-step solutions from verified subject matter experts
