Infix, Prefix and Postfix Expressions:

Examples:
Infix Expression
Prefix Expression
Postfix Expression
A + B + A B A B +
A + B * C + A * B C A B C * +
(A + B) * C * + A B C A B + C *
A + B * C + D + + A * B C D A B C * + D +
(A + B) * (C + D) * + A B + C D A B + C D + *
A * B + C * D + * A B * C D A B * C D * +
A + B + C + D + + + A B C D A B + C + D +

Algorithm for evaluating postfix expression:

  1. Create an empty stack called myStack.
  2. Convert the string to a list by using the string method split.
  3. Scan the token list from left to right.
    1. If the token is an operand, convert it from a string to an integer and push the value onto the myStack.
    2. If the token is an operator, *, /, +, or -, it will need two operands. Pop the myStack twice. The first pop is the second operand and the second pop is the first operand. Perform the arithmetic operation. Push the result back on the myStack.
  4. When the input expression has been completely processed, the result is on the stack. Pop the myStack and return the value.