Postfix to Prefix Conversion in Java
Postfix to Prefix conversion in Java:
As we have already discussed about infix ,prefix, and postfix operation .
In this java tutorial we will discuss how to convert an postfix to prefix operation in java.
for example :
- Input : Postfix : AB+CD-*
- Output : Prefix : *+AB-CD
Let us see how to this this operation :
Move to the given expression from left to right, one at a time
- If the character is operand do push operation to stack.
- If the character is operator then do the following
- Pop operand from the stack, say it’s p1.
- Pop operand from the stack, say it’s p2.
- do operation on p1 and p2 and push in stack.
- Once the expression end initialize the result string and pop out from the stack and add it to the result.
- show the result.
Java code :