Thursday, 18 June 2020

Arithmetic Expression Evaluation Using Stack

Arithmetic expressions can be evaluated using two stacks - one for operands and other for numeric values.

Algorithm:

  1. If element is numeric, push to numeric stack.
  2. If element is operand, push to operand stack.
  3. If element is "(", ignore. 
  4. If element is ")", pop operand from operand stack and pop two numerics from numeric stack - perform the operation and push the result back into numeric stack. 
 Following is the program based on the generic stack implementation using arrays.

No comments:

Post a Comment