Infix to Postfix conversion [Resolved]

Posted by Crniranjanraj under Others on 10/5/2015 | Points: 10 | Views : 722 | Status : [Member] | Replies : 1
Convert the infix expression a+b*c/d into post fix expression




Responses

Posted by: Rajnilari2015 on: 10/11/2015 [Member] [Microsoft_MVP] [MVP] Platinum | Points: 50

Up
1
Down

Resolved
Algorithm
-----------

Step 1: If Entered Character is Alphabet then Print Alphabet as Output
Step 2: If Entered Character is Digit then Print Digit as Output
Step 3: If Entered Character is Opening Bracket THEN
a) Push '(' Onto Stack
b) If any Operator Appears before ')' then Push it onto Stack.
c) If Corresponding ‘)’ bracket appears then Start Removing Elements [Pop] from Stack till '(' is removed.
Step 4: If Entered Character is Operator THEN
a) Check Whether There is any Operator Already present in Stack or not.
b) If Stack is Empty then Push Operator Onto Stack.
c) If Present then Check Whether Priority of Incoming Operator is greater than Priority of Topmost Stack Operator.
d) If Priority of Incoming Operator is Greater then Push Incoming Operator Onto Stack.
e) Else Pop Operator From Stack again goto Step 4.
Given
Infix :
a+b*c/d

Output
Postfix :
a b c * d / +

Explanation
Step 1: First Character is Alphabet (i.e. 'a'). Display It in Postfix string. (Current Output in Postfix string: a)
Step 2: Second character is Operator(i.e. '+') and stack is empty, push '+' to stack. (Current Output in Postfix string: a)
Step 3: Third Character is Alphabet (i.e. 'b'). Display It in Postfix string. (Current Output in Postfix string: ab)
Step 4: Fourth character is Operator(i.e. '*') and stack has already '+' as the top element of the stack .But '+' has lower precedence than '*' so push '*' to stack(Stack status *+). (Current Output in Postfix string: ab)
Step 5: Fifth Character is Alphabet (i.e. 'c'). Display It in Postfix string. (Current Output in Postfix string: abc)
Step 6: Sixth character is Operator(i.e. '/') and stack has already '*+'. Since '*' has a higher precedence than '/', this will be poped out from stack and will be added to Postfix string. Since '/' has higher precedence that '+', hence it will be added to stack (Stack status /+). (Current Output in Postfix string: abc*)
Step 7: Seventh Character is Alphabet (i.e. 'd'). Display It in Postfix string. (Current Output in Postfix string: abc*d)
Step 8: Now all characters have been scanned so we must pop the remaining elements from the stack and add it to the Postfix string. So the final Postfix string will be abc*d/+

Hope this helps


--
Thanks & Regards,
RNA Team

Crniranjanraj, if this helps please login to Mark As Answer. | Alert Moderator

Login to post response