Search code examples
data-structuresstackpostfix-notationinfix-notationprefix-notation

Stack And Arithmetic Evaluation


A stack is said to be the ideal data structure for Arithmetic Evaluation. Why is it so?
Why do we even need a data structure for Arithmetic Evaluation? I've been studying about this for some time now and still confused. I don't understand what is the use of Prefix and Postfix expressions because the Infix expression is quite readable.


Solution

  • Answer for the part of why postfix/prefix over infix is quite well explained here.As a summary infix is readable but not easily parsed
    As for why stack is used here is:
    1: push,pop in O(1) time are quite useful for evaluation.
    2: push: add the operand on stack.
    3: pop: remove the operand and evaluate expression(binary) 4: final result is the only one left on stack after parsing