Q. Write down an algorithm to convert an infix expression into the postfix expression.
Ans.
Algorithm to convert infix expression to post fix expression is given as follows.
1. opstk = the empty stack;
2. while (not end of input) {
3. symb = next input character;
4. if (symb is an operand) add symb to postfix string
5. else {
6. while (!empty (opstk) and prcd (top (opstk), symb)>0){
7. topsymb = pop(opstk);
8. add topsymb to the postfix string; }/*end while*/
9. if (empty (opstk) || symb! = ')' ) push (opstk, symb); else /*pop the open parenthesis and discard it */
topsymb = pop(opstk);
} /* end else */
}/* end while */
/* output any remaining operator */
10.while (!empty (opstk)){
11. top symb = pop (opstk);
12. add topsymb to the postfix string;
} /* end while * /
/*output any remaining operator*/