0 ratings0% found this document useful (0 votes) 18 views7 pagesInto Post
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content,
claim it here.
Available Formats
Download as PDF or read online on Scribd
How to convert an Infix expression to a Postfix expression?
To convert infix expression to postfix expression, use the stack data structure.
Scan the infix expression from left to right. Whenever we get an operand, add it to
the postfix expression and if we get an operator or parenthesis add it to the stack
by maintaining their precedence.
Below are the steps to implement the above idea:
1. Scan the infix expression from left to right.
2. If the scanned character is an operand, put it in the postfix expression.
3. Otherwise, do the following
+ If the precedence and associativity of the scanned operator are greater than the
precedence and associativity of the operator in the stack [or the stack is empty or
the stack contains a ‘(' J, then push it in the stack. ['’ operator is right associative
and other operators like ‘+'/—',*' and ‘/' are left-associative].
© Check especially for a condition when the operator at the top of the stack
and the scanned operator both are ‘*". In this condition, the precedence of
the scanned operator is higher due to its right associativity. So it will be
pushed into the operator stack.
© Inall the other cases when the top of the operator stack is the same as.
the scanned operator, then pop the operator from the stack because of
left associativity due to which the scanned operator has less precedence.
+ Else, Pop all the operators from the stack which are greater than or equal to in
precedence than that of the scanned operator.
© After doing that Push the scanned operator to the stack. (If you encounter
parenthesis while popping then stop there and push the scanned operator
in the stack.)
4. If the scanned character is a ‘(', push it to the stack.
5. If the scanned character is a)’, pop the stack and output it until a ‘(’ is encountered,
and discard both the parenthesis.
6. Repeat steps 2-5 until the infix expression is scanned.7. Once the scanning is over, Pop the stack and add the operators in the postfix
expression until it is not empty.
8. Finally, print the postfix expression.
Illustration:
Follow the below illustration for a better understanding
Consider the infix expression exp = “a+b*c+d”
and the infix expression is scanned using the iterator i, which is initialized as i = 0.
1st Step: Here i = 0 and expli = ‘a’ i.e., an operand. So add this in the posttix
expression. Therefore, postfix = “a”
Pe ete
Add ‘ain the postfix
2nd Step: Here i = 1 and exp[i] = ‘+’ i.e., an operator. Push this into the
stack. postfix = “a” and stack = {4}.Add ‘+' into
stack
feiss
Push ‘+"in the stack
3rd Step: Now i = 2 and expfi] = ‘b’ ie., an operand. So add this in the postfix
expression. postfix = “ab” and stack = {+}.
postfix = "ab"
PPI aul ence
Add ‘bin the postfix
4th Step: Now i = 3 and exp[i] = *’ie., an operator. Push this into the
stack. postfix = “ab” and stack = {+, *}.Add ™ into stack
Postfix = "ab"
* has higher precedence. Push it into stack
Push "in the stack
5th Step: Now i= 4 and expfi] = ‘c’.e., an operand. Add this in the postfix
expression. postfix = “abe” and stack = {+, *}
Pie ete
‘Add ‘c'in the postfix
6th Step: Now i= 5 and exp[i] = ‘+’ i.e., an operator. The topmost element of the
stack has higher precedence. So pop until the stack becomes empty or the top
element has less precedence. *’ is popped and added in postfix. So postfix =
“abc*” and stack = {+}.Add to postfix
Pre ed ecko uky
Pop and add in postibe
Now top element is ‘+' that also doesn't have less precedence. Pop it. postfix =
“abot”.
Add to postfix
eee cae
Pop ‘*" and add it in postfix
Now stack is empty. So push ‘+’ in the stack. stack = {+}.Add ‘+' into
stack
postfix = "abc*+"
feiss
Push ‘+"in the stack
7th Step: Now i = 6 and exp[i] = ‘d’i.e., an operand. Add this in the postfix
expression. postfix = “abe*+d”.
EUPruR Macnee
Add ‘an the postfix
Final Step: Now no element is left. So empty the stack and add it in the postfix
expression. postfix = “abc*+d+".Add to postfix
“abc*+d+"
Bee
Pop '+" and add itn postfix