Evaluation of postfix expression algorithm using stacks

Algorithm for Evaluation of postfix expression

Postfix expression also known as   Reverse Polish notation (RPN) is a mathematical notation in which every operator follows all of its operands, in contrast to Polish notation (PN), which puts the operator before its operands. It is also known as postfix notation. It does not need any parentheses as long as each operator has a fixed number of operands.

Evaluation of postfix expression
Title – Evaluation of postfix expression

Please go through this article before going further:

READ  Infix Prefix postfix expressions - datastructures

Algorithm

  1. Read the postfix expression and start scanning from left to right.
  2. Create an empty stack
  3. Repeat the steps 4 – 5 until you reach the end of the postfix string.
  4. If the scanned token is an operand then push it into the stack.
  5. Else if it is an operator, check if it is a unary operator than pop one element from the stack apply unary operation and push back into the stack else if it is a binary operator then pop two elements from the stacks perform the operation and push the element back into the stack.
  6. At the end when we reach the end of the postfix expression we pop the final element from the stack as the result.

OTHER RESOURCES

Related Post

Rating: 5.0. From 1 vote.
Please wait...