Arithmetic expressions can be evaluated using two stacks - one for operands and other for numeric values.
Algorithm:
- If element is numeric, push to numeric stack.
- If element is operand, push to operand stack.
- If element is "(", ignore.
- 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.
No comments:
Post a Comment