Evaluation of POSTFIX expression using STACK

March 10, 2014 0 Comments

Evaluation of POSTFIX expression using STACK :

To evaluation the value of a POSTFIX expression STACK can be used in the following way:
Procedure: Add #, a pound symbol, a sentinel, a terminating symbol at the end of the POSTFIX expression. Scan the expression from left to right till #.If the character scanned character is an operand PUSH it on to STACK. If the scanned character is any operator, * , POP STACK once call it B and POP again and call it A, find A  *   B, and PUSH the result on to STACK. If the character scanned is #, then POP the STACK and it is the value of the POSTFIX expression.. For example consider a POSTFIX expression, 6 3 2| *3 6 +/.

The value of POSTFIX expression is 6.
Algorithm to evaluate the value of POSTFIX expression:
[P is a POSTFIX expression]
Add # at the end of P
Repeat Scanning P from left to right While scanned character
< >  #
  If Scanned Character = operand Then:
    [PUSH it on to STACK]
  RES <-- A operator B
[End of While]
Write : ‘Value of POSTFIX expression is ‘, VAL

Jackrin Reacher

Some say he’s half man half fish, others say he’s more of a seventy/thirty split. Either way he’s a fishy bastard. Google