Tuesday, September 22, 2020

Transforming Infix Expression into Postfix Expressions:

 Let Q be an arithmetic expression written in infix notation. This algorithm transforms the infix expression into postfix expressions.


POLISH(Q, P)

Support Q is an arithmetic expression written in infix notation.  This algorithm finds the equivalent postfix expression P.

  1. Push “(” Onto STACK, and add “)”  to the end of Q.

  2. Scan Q from left to right and repeat step 3 to 6 for each element of Q until the stack is empty:

  3.  If an operand is encountered,  add it to P.

  4.  If left parenthesis is encountered,  push it onto  STACK.

  5.  If an operator is encountered, then:

    1. Repeatedly pop from stack and add to P  each operator (on the top of the STACK) which has the same precedence or higher precedence than the operator

    2.  Add operator to STACK

[End of If structure]

  1.  If a right  parenthesis encounter, then :

    1.  Repeatedly pop from the stack and add it to P each operator (on the top of the stack) until a left parenthesis is encountered.

    2.  Remove the left [Do not add the left parenthesis to P].

  [End of if  structure]

  [End of step 2 loop]

  1. Exit






No comments:

Post a Comment