Stack Application of in Expression Evaluation
Stack Application of in Expression Evaluation
Operator: ( ), { }. ^,*,/,+,-
1. Prefix: in the stack notation, prefix means operator exists before the operands
2. Infix: in the stack notation, infix means the operator between the operands
3. Postfix: in the stack notation, postfix means operator exists after the operands
Application of stack:
Example problems:
231*+9-
Steps:
In the expression, first character is ‘2’ operand, then push(2)
3
2
1
3
2
In the expression, 4th character is ‘*’ operator, then pop top most 2 elements, consider op2 = 1,
op1 =3, Perform op1 * op2 1 *3 = 3, push the result on the stack [push (3)]
3
2
In the expression, 5th character is ‘+’ operator, then pop top most 2 elements, consider op2 = 3,
op1 =2, Perform op1 + op2 3 + 2 = 5, push the result on the stack [push (5)]
9
5
In the expression, 7th character is ‘-’ operator, then pop top most 2 elements, consider op2 = 9,
op1 =5, Perform op1 + op2 5 - 9 = -4 , push the result on the stack [push (-4)]
-4
Example:1
INFIX NOTATION : (A + B / C * D -)
Example:2
INFIX NOTATION :((A + B) – C * (D / E)) + F