Search

Monday, March 10, 2014
0 comments

Evaluation of POSTFIX expression using STACK

7:43 AMMonday, March 10, 2014
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:
POSTFIXVAL (P)
[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]
   STACK [TOP]<--OPERAND, TOP<--TOP+1
 Else:
  B<--STACK[TOP], TOP<--TOP-1
  A<--STACK[TOP],TOP<--TOP-1
  RES <-- A operator B
  STACK [TOP]<--RES
[End of While]
VAL<--STACK[TOP]
Write : ‘Value of POSTFIX expression is ‘, VAL
Exit.


Protected by Copyscape Online Copyright Protection Software

0 comments :

Post a Comment

 
Toggle Footer
Top