STACKS
STACKS
Reading ref :
LN 2.3.1, LN 2.2.1, ULR 2.3.3
STACKS
• Everyday examples
TOP
C C
B B
A A
POP
TOP
C
TOP
B B
A A
• List Implementation
• 3*(4+5).
• Item Read Expression Parsed So Far Comments
3 3
* 3*
( 3*(
4 3*(4 You can’t evaluate 3*4
because of the parenthesis.
+ 3*(4+
5 3*(4+5 You can’t evaluate 4+5 yet.
) 3*(4+5) When you see the )you can evaluate 4+5.
3*9 After you’ve evaluated 4+5, you can
evaluate 3*9.
27
End Nothing left to evaluate.
STACKS
Parsing Arithmetic Expressions
• To translate infix to postfix notation, you follow a
similar set of rules to those for evaluating infix.
However, there are a few small changes.
• You don’t do any arithmetic.
• The idea is not to evaluate the infix expression,
but to rearrange the operators and operands into
a different format: postfix notation.
• The resulting postfix expression will be
evaluated later.
Translating A+B*C to Postfix