Evaluation of postfix expression

  Evaluation of postfix expression

================================================================================

  •  Postfix notation (also known as Reverse Polish Notation) is a way to represent an expression, where operators follow their corresponding operands. 
  •  Evaluating an expression represented as postfix notation can easily be done using the stack data structure.
postfix expression


  • Algorithm:
    1. begin
    2. x = read first symbol from postfix expression
    3. while(x)
      1. if (x is an operator)
        1. operand1: pop()
        2. operand2: pop()
        3. result = operand2 operator operand1
        4. push(result)
      2. end if
      3. else
        1. push(x)
      4. end else
      5. x = read next symbol
    4. end while
    5. end

  • eg.
    • 6 2 3 + -
    • Solution: 
      • Symbol

        Stack

        Evaluation

        6



         

        2



         

        3



         

        +



        operand1 = 3, operand2 = 2

        result = 2 + 3

        -



        operand1 = 5, operand2 = 6

        result = 6 - 5

         


Comments

Popular posts from this blog

TREE

STACK