|
ALGORITHM : Infix
to Prefix
STEP 1 : Read the given infix expression into string called infix.
STEP 2 : Reverse the infix string and read one character at a time and perform the following operations :
If the read character is an operand, then add the operand
to the prefix string.
If the read character is not an operand, then check
If the stack is not empty and precedence of the top of the
stack operator is higher than the read operator,
then pop the operator from stack and add this
operator to the prefix string.
Else push the operator onto the stack.
STEP 3 : Repeat
STEP 2 till all characters are processed from the input string.
STEP 4 : If stack is not empty, then pop the operator from stack and add this operator to the prefix string.
STEP 5 : Repeat
STEP 4 till all the operators are popped from the stack.
STEP 6 : Reverse the prefix string and display the result of the given infix expression or the resultant prefix expression stored in a string called prefix from this algorithm.
|