# Infix to Postfix/Prefix converter

We have two converters. The first converter converts infix to postfix expression. And the second one converts infix to prefix expression. It avoids the problem of operator precedence and association while making calculations in programming languages.

You will get step by step conversion for your infix expression to postfix or prefix form.

## Infix to Postfix converter

The following converter converts an infix expression to a postfix expression.

Change the expression and converter will convert infix to postfix step by step.

Infix:

Postfix:

## Step by step output for "" expression

Input StringOutput StackOperator Stack

## Infix to Prefix converter

The following converter converts an infix expression to a prefix expression.

Change the expression and converter will convert infix to prefix step by step.

Infix:

Prefix:

## Step by step output for "" expression

• 1. Reversed string:
• 2. Postfix of :     (see table)
• 3. Reversed string of :

## Infix, Postfix, Prefix

Infix

Infix expression is the normal expression that consists of operands and operators. For example, A+B

Postfix

Postfix expression consists of operands followed by operators. For example, AB+

Prefix

Prefix expression consists of operators followed by operands. For example, +AB

## Algorithm to convert infix to postfix expression

In this case, We use the stacks to convert infix to postfix. We have operator's stack, output's stack and one input string. Operator's stack works as FILO(First In Last Out). Output's stack works as FIFO (First In First Out).

The following algorithm converts infix to postfix.

• Scan input string from left to right character by character.
• If the character is an operand, put it into output stack.
• If the character is an operator and operator's stack is empty, push operator into operators' stack.
• If the operator's stack is not empty, there may be following possibilites.
• If the precedence of scanned operator is greater than the top most operator of operator's stack, push this operator into operand's stack.
• If the precedence of scanned operator is less than or equal to the top most operator of operator's stack, pop the operators from operand's stack untill we find a low precedence operator than the scanned character. Never pop out ( '(' ) or ( ')' ) whatever may be the precedence level of scanned character.
• If the character is opening round bracket ( '(' ), push it into operator's stack.
• If the character is closing round bracket ( ')' ), pop out operators from operator's stack untill we find an opening bracket ('(' ).
• Now pop out all the remaining operators from the operator's stack and push into output stack.

## Algorithm to convert infix to prefix expression

In this case, we have operator's stack, output's stack and one input string. Operator's stack works as FILO(First In Last Out). Output's stack works as FIFO (First In First Out).

The following algorithm must be followed for infix to prefix conversion.

• Reverse the input string.
• Convert the reversed string into infix expression.
• Now reverse the resulting infix expression obtained from the previous step. The resulting expression is prefix expression