Professional Documents
Culture Documents
An arithmetic expression can be written in three different but equivalent notations, i.e.,
without changing the essence or output of an expression. These notations are −
Infix Notation
Prefix (Polish) Notation
Postfix (Reverse-Polish) Notation
It is easy for humans to read, write, and speak in infix notation but the
same does not go well with computing devices
For example −
Here, both + and − are left associative, so the expression will be evaluated
as (a + b) − c
5 3 + 6 2 / *3 5 * + Ans: 39
Ans: -4
2 3 1*+ 9 -
Ans: 59
123*+45*6+2*+
Step 3 − If we have a closing parenthesis ")" we keep popping out elements from the top of the stack and append
them to our postfix expression until we encounter an opening parenthesis. We pop out the left parenthesis
without appending it
Step 5 − When the last token of infix expression has been scanned, we pop the remaining elements from stack
and append them to our postfix expression
DR. SHWETA SHARMA, PEC CHANDIGARH 16
Example Element Stack contents Postfix Expression
K K
+ +
Infix expression: L + KL
- - KL+
K + L - M*N + (O^P) * W M - KL+M
* -* KL+M
N -* KL+MN
+ + KL+MN*-
( +( KL+MN*-
Postfix expression: O +(^ KL+MN*-O
^ +(^ KL+MN*-O
KL+MN∗−OP^W*+ P +(^ KL+MN*-OP
) + KL+MN*-OP^
* +* K L + M N* - O P ^
W +* K L + M N* - O P ^ W
KL+MN∗−OP^W*+
DR. SHWETA SHARMA, PEC CHANDIGARH 17
Questions on Conversion to Postfix Expression
Infix Notation Post-fix Notation
x^y/(5*z)+2 xy^5z*/2+
( (a+(b*c)) - (d^(e^f)) )
abc*+ def^^-