Write C++ code to convert a given infix expression to postfix expression.
Write C++ code to convert a given infix expression to postfix expression.
The steps and table below shows how infix to postfix conversion works in
practice .
1. Search the Infix string from left to right.
2. Initialize a vacant stack.
3. If the scanned character is an operand add it to the Postfix string.
4. When the scanned character is an operator and if the stack is empty push the
character to stack.
5. If a scanned character is an Operator and the stack is not empty, compare the
precedence of the character with element on top of the stack.
6. When top Stack has higher precedence over the scanned character pop the stack
else push the scanned character to stack. Repeat this procedure till the stack is not
empty and top Stack has precedence over the character.
7. Reiterate 4 and 5 steps till all characters are scanned.
8. After all characters are scanned, we have to add any character that the stack may
have to the Postfix string.
9. When stack is not empty add top Stack to Postfix string and Pop the stack.
Trending now
This is a popular solution!
Step by step
Solved in 3 steps with 1 images