Lecture 09 - Applications of Stack
Lecture 09 - Applications of Stack
Lecture # 09
September 18, 2023 Dr. Rabia Maqsood
Fall 2023 [email protected]
FAST – NUCES, CFD Campus
TODAY’S TOPICS
q Applications of Stack (LIFO)
q Parenthesis matching
q Function calls
q Postfix calculator (a.k.a reverse polish)
q Recursion: basics
A stack is useful for checking symbol balance. When a closing symbol is found it must match the most
recent opening symbol of the same type.
Algorithm?
At the end of the file if the stack is not empty report an error
In Assembly language course, you will see how stacks are implemented in
hardware on all CPUs to facilitate function calling
What about 1 - 2 - 4 ^ 5 * 3 * 6 / 7 ^ 2 ^ 3
Example:
(3 + 4) × 5 – 6 = 29
3+4 × 5–6 = 17
3 + 4 × (5 – 6) = –1
(3 + 4) × (5 – 6) = –7
Example: 3 2 * 1 + is postfix of 3 * 2 + 1
http://www.audiovis.nac.gov.pl/
Parsing reads left-to-right and performs any operation on the last two operands:
3 4 + 5 × 6 –
7 5 × 6 –
35 6 –
29
1
CS 2001 - FALL 2023 19
REVERSE-POLISH NOTATION
Push 1 onto the stack
1 2 3 + 4 5 6 × – 7 × + – 8 9 × +
2
1
CS 2001 - FALL 2023 20
REVERSE-POLISH NOTATION
Push 3 onto the stack
1 2 3 + 4 5 6 × – 7 × + – 8 9 × +
3
2
1
CS 2001 - FALL 2023 21
REVERSE-POLISH NOTATION
Pop 3 and 2 and push 2 + 3 = 5
1 2 3 + 4 5 6 × – 7 × + – 8 9 × +
5
1
CS 2001 - FALL 2023 22
REVERSE-POLISH NOTATION
Push 4 onto the stack
1 2 3 + 4 5 6 × – 7 × + – 8 9 × +
4
5
1
CS 2001 - FALL 2023 23
REVERSE-POLISH NOTATION
Push 5 onto the stack
1 2 3 + 4 5 6 × – 7 × + – 8 9 × +
5
4
5
1
CS 2001 - FALL 2023 24
REVERSE-POLISH NOTATION
Push 6 onto the stack
1 2 3 + 4 5 6 × – 7 × + – 8 9 × +
6
5
4
5
1
CS 2001 - FALL 2023 25
REVERSE-POLISH NOTATION
Pop 6 and 5 and push 5 × 6 = 30
1 2 3 + 4 5 6 × – 7 × + – 8 9 × +
30
4
5
1
CS 2001 - FALL 2023 26
REVERSE-POLISH NOTATION
Pop 30 and 4 and push 4 – 30 = –26
1 2 3 + 4 5 6 × – 7 × + – 8 9 × +
–26
5
1
CS 2001 - FALL 2023 27
REVERSE-POLISH NOTATION
Push 7 onto the stack
1 2 3 + 4 5 6 × – 7 × + – 8 9 × +
7
–26
5
1
CS 2001 - FALL 2023 28
REVERSE-POLISH NOTATION
Pop 7 and –26 and push –26 × 7 = –182
1 2 3 + 4 5 6 × – 7 × + – 8 9 × +
–182
5
1
CS 2001 - FALL 2023 29
REVERSE-POLISH NOTATION
Pop –182 and 5 and push –182 + 5 = –177
1 2 3 + 4 5 6 × – 7 × + – 8 9 × +
–177
1
CS 2001 - FALL 2023 30
REVERSE-POLISH NOTATION
Pop –177 and 1 and push 1 – (–177) = 178
1 2 3 + 4 5 6 × – 7 × + – 8 9 × +
178
CS 2001 - FALL 2023 31
REVERSE-POLISH NOTATION
Push 8 onto the stack
1 2 3 + 4 5 6 × – 7 × + – 8 9 × +
8
178
CS 2001 - FALL 2023 32
REVERSE-POLISH NOTATION
Push 1 onto the stack
1 2 3 + 4 5 6 × – 7 × + – 8 9 × +
9
8
178
CS 2001 - FALL 2023 33
REVERSE-POLISH NOTATION
Pop 9 and 8 and push 8 × 9 = 72
1 2 3 + 4 5 6 × – 7 × + – 8 9 × +
72
178
CS 2001 - FALL 2023 34
REVERSE-POLISH NOTATION
Pop 72 and 178 and push 178 + 72 = 250
1 2 3 + 4 5 6 × – 7 × + – 8 9 × +
250
CS 2001 - FALL 2023 35
REVERSE-POLISH NOTATION
Thus
1 2 3 + 4 5 6 × – 7 × + – 8 9 × +
evaluates to the value on the top: 250
The equivalent in-fix notation is
((1 – ((2 + 3) + ((4 – (5 × 6)) × 7))) + (8 × 9))
Algorithm:
Given a proper postfix expression:
get the next token
if it is an operand push it onto the stack
else if it is an operator
pop the stack for the right hand operand
pop the stack for the left hand operand
apply the operator to the two operands
push the result onto the stack
when the expression has been exhausted the result is the top (and only element) of the stack
Example: 6 * (5 + (2 + 3) * 8 + 3)