Arithmetic Expressions :
Print this page
 

Algorithm for evaluation of an expression E which is in prefix notation :

  • We assume that the given prefix notation starts with IsEmpty() .
  • If number of symbols = n in any infix expression then number of operations performed = some constant times n.
  • here next token   function gives us the next occurring element in the expression in a left to right scan.
  • The  PUSH   function adds element x to stack Q which is of maximum length n
Evaluate (E)
Else
{
{
Top = 0;
If (x = = operand)
While (1)
PUSH (Q, top, n, x);
{
If (x = = operator)
x= next token (E)
If (x = = infinity)
Pop correct number of operands according to the operator (unary/binary) and then perform the operation and store result onto the stack
 
Print value of stack [top] as the output of the expression
}
}
}
}
 
Prev