Stack 5
Stack 5
Stacks
Stacks
Outline I
1 Stacks
Stack Operations
4 Applications of Stacks
1 Stacks
Stack Operations
4 Applications of Stacks
Stacks I
1 Stacks
Stack Operations
4 Applications of Stacks
Stack Operations I
1 Stacks
Stack Operations
4 Applications of Stacks
Operations on Stack
1 Stacks
Stack Operations
4 Applications of Stacks
The condition TOP == NULL will indicate that the stack is empty.
Stacks
Linked Representation of Stacks
6. Exit.
Stacks
Linked Representation of Stacks
5. Exit.
Stacks
Applications of Stacks
1 Stacks
Stack Operations
4 Applications of Stacks
Applications of Stacks
1 Stacks
Stack Operations
4 Applications of Stacks
Evaluation of Expressions I
Operators Precedence:
I In arithmetic expressions operators precedence is observed:
An Example:
I Evaluate: 2 ↑ 3 + 5 ∗ 2 ↑ 2 − 12/6
I Answer: 26
Stacks
Arithmetic Expressions; Polish Notation
Evaluation of Expressions II
An Important Fact:
I Parentheses’ alter the precedence of operators.
An Example:
I (A + B) ∗ C 6= A + (B ∗ C)
I (2 + 3) ∗ 7 = 35 while 2 + (3 ∗ 7) = 23
Infix Notations:
I Expressions in which operator lies between the operands
are referred to as infix notations.
I A+B, C-D, P*F, · · · all are infix notations.
I A+(B*C) and (A+B)*C are distinguished by parentheses or
by applying the operators precedence discussed above.
Stacks
Arithmetic Expressions; Polish Notation
Algorithm 1 I
Algorithm 1 II
7. Exit.
Stacks
Arithmetic Expressions; Polish Notation
Algorithm 1-Example
arithmetic infix expression Q : A + (B ∗ C − (D/E ↑ F ) ∗ G) ∗ H
Stacks
Arithmetic Expressions; Polish Notation
Algorithm 2
2. Scan P from left to right and repeat Steps 3 and 4 for each element of P until the sentinel ”)” is encountered.
6. Exit.
Stacks
Arithmetic Expressions; Polish Notation
Algorithm 2-Example
Infix to Prefix I
Stacks
Arithmetic Expressions; Polish Notation
Infix to Prefix II
Algorithm of Infix to Prefix
1. Push “)” onto STACK, and add “(“ to end of the A
2. Scan A from right to left and repeat step 3 to 6 for each element of A until the STACK is empty
a. Repeatedly pop from STACK and add to B each operator (on the top of STACK) which has same or
higher precedence than the operator.
a. Repeatedly pop from the STACK and add to B (each operator on top of stack until a left parenthesis
is encounterd)
7. Exit
Stacks
Arithmetic Expressions; Polish Notation
Expression: ( A + B ∧ C ) * D + E ∧ 5
1. Reverse the infix expression: 5 ∧ E + D * ) C ∧ B + A (
Result: + * + A ∧ B C D ∧ E 5
Stacks
Arithmetic Expressions; Polish Notation
3. If the character at P is an operator pop two elements from the Stack. Operate on these elements according
to the operator, and push the result back to the Stack.
4. Decrement P by 1 and go to Step 2 as long as there are characters left to be scanned in the expression.
6. End.
Stacks
Arithmetic Expressions; Polish Notation
Expression: + 9 * 2 6
Character Scanned Stack (Front to Back) Explanation
6 6 6 is an operand, push to Stack
2 62 2 is an operand, push to Stack
* 12 (6 * 2) * is an operator, pop 6 and 2,
multiply them and push result to
Stack
9 12 9 9 is an operand, push to Stack
+ 21 (12+9) + is an operator, pop 12 and
9 add them and push result to
Stack
Result: 21