One of the most important applications of the Stack is the arithmetic expressions’ conversion into the programming languages of high-level into a form that the machine can read. Because the computer system can understand as well as work only on the binary language, the system assumes that the arithmetic operation is able to take place only in two operands, for example, A+B, D/A, and others. But in the usual form, one arithmetic expression can consist of two or more operators plus two operands for example (A+B)*C(D/(J+D)).

Infix Expression

This expression follows this scheme that is an is being preceded as well as succeeded by the . This kind of expression is known as infix expression. For example, A+B, etc.

Postfix Expression

This expression follows this scheme that is an is being succeeded by two . For example, AB+, etc.

How to convert the Infix Expression To a Postfix Expression
Let, X, be one arithmetic expression that is written in the infix notation. Now, this algorithm finds or needs an equivalent postfix notation Y.
  • Push “(“onto Stack, and add “)” to the end of X.
  • Scan X starting from left moving to the right. Also, keep repeating Step 3 to Step 6 for every single element of X till the Stack gets empty.
  • If the operand is met, add the operand to Y.
  • If one left parenthesis is confronted, push the left parenthesis onto Stack.
  • If the operator is met, then:
    • Regularly jump from Stack then add to Y every single operator (that too on the top of the Stack) that has the exact same precedence or higher precedence than the operator.
    • Add the operator to the Stack.
      [here comes the end of If]
  • Now, If one right parenthesis is confronted, then:
    • Repeatedly jump from Stack then add to Y every single operator (that too on the top of the Stack) until one left parenthesis is met with.
    • Then, remove the left Parenthesis.
      [insert the parenthesis for End of If]
      [here also insert the parenthesis for End of If]
  • Finally, END.

 

BY Best Interview Question ON 26 Jan 2020