---
# System prepended metadata

title: Algorithm - Practice

---

# Algorithm - Practice

## Expression Evaluation 
1. While there are still tokens to be read in,
1.1 Get the next token. 
1.2 If the token is:
&nbsp;&nbsp;1.2.1 A number: push it onto the value stack.
&nbsp;&nbsp;1.2.2 A variable: get its value, and push onto the value stack.
&nbsp;&nbsp;1.2.3 A left parenthesis: push it onto the operator stack.
&nbsp;&nbsp;1.2.4 A right parenthesis:
&nbsp;&nbsp;&nbsp;&nbsp;1.2.4.1 While the thing on top of the operator stack is not a left parenthesis,
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;1.2.4.1.1 Pop the operator from the operator stack.
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;1.2.4.1.2 Pop the value stack twice, getting two operands.
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;1.2.4.1.3 Apply the operator to the operands, in the correct order.
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;1.2.4.1.4 Push the result onto the value stack.
&nbsp;&nbsp;&nbsp;&nbsp;1.2.4.2 Pop the left parenthesis from the operator stack, and discard it.
&nbsp;&nbsp;1.2.5 An operator (call it thisOp):
&nbsp;&nbsp;&nbsp;&nbsp;1.2.5.1 While the operator stack is not empty, and the top thing on the operator stack has the same or greater precedence as thisOp,
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;1.2.5.1.1 Pop the operator from the operator stack.
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;1.2.5.1.2 Pop the value stack twice, getting two operands.
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;1.2.5.1.3 Apply the operator to the operands, in the correct order.
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;1.2.5.1.4 Push the result onto the value stack.
&nbsp;&nbsp;&nbsp;&nbsp;1.2.5.2 Push thisOp onto the operator stack.
2. While the operator stack is not empty,
2.1 Pop the operator from the operator stack.
2.2 Pop the value stack twice, getting two operands.
2.3 Apply the operator to the operands, in the correct order.
2.4 Push the result onto the value stack.
3. At this point the operator stack should be empty, and the value
   stack should have only one value in it, which is the final result.

ref:
[https://www.geeksforgeeks.org/stack-set-2-infix-to-postfix/](https://www.geeksforgeeks.org/stack-set-2-infix-to-postfix/)
[https://www.geeksforgeeks.org/stack-set-4-evaluation-postfix-expression/?ref=lbp](https://www.geeksforgeeks.org/stack-set-4-evaluation-postfix-expression/?ref=lbp)
[https://www.geeksforgeeks.org/expression-evaluation/?ref=gcse](https://www.geeksforgeeks.org/expression-evaluation/?ref=gcse)