Lecture 8 Stack Applications
Lecture 8 Stack Applications
1 / 79
Stack Application
Learning Objectives
2 / 79
Stack Application
(3 + 4) × 5 − 6
3 4 + 5 × 6 −
Parsing. reads left-to-right and performs any operation on the last two
operands.
3 4 + 5 × 6 −
7 5 × 6 −
35 6 −
29
This is called reverse-Polish notation after the mathematician Jan
Lukasiewicz.
3 / 79
Stack Application
Other examples:
3 4 5 × + 6 −
3 20 + 6 −
23 6 −
17
3 4 5 6 − × +
3 4 −1 × +
3 −4 +
−1
4 / 79
Stack Application
Other examples:
3 4 5 × + 6 −
3 20 + 6 −
23 6 −
17
3 4 5 6 − × +
3 4 −1 × +
3 −4 +
−1
5 / 79
Stack Application
Other examples:
3 4 5 × + 6 −
3 20 + 6 −
23 6 −
17
3 4 5 6 − × +
3 4 −1 × +
3 −4 +
−1
6 / 79
Stack Application
Other examples:
3 4 5 × + 6 −
3 20 + 6 −
23 6 −
17
3 4 5 6 − × +
3 4 −1 × +
3 −4 +
−1
7 / 79
Stack Application
Other examples:
3 4 5 × + 6 −
3 20 + 6 −
23 6 −
17
3 4 5 6 − × +
3 4 −1 × +
3 −4 +
−1
8 / 79
Stack Application
Other examples:
3 4 5 × + 6 −
3 20 + 6 −
23 6 −
17
3 4 5 6 − × +
3 4 −1 × +
3 −4 +
−1
9 / 79
Stack Application
Other examples:
3 4 5 × + 6 −
3 20 + 6 −
23 6 −
17
3 4 5 6 − × +
3 4 −1 × +
3 −4 +
−1
10 / 79
Stack Application
Other examples:
3 4 5 × + 6 −
3 20 + 6 −
23 6 −
17
3 4 5 6 − × +
3 4 −1 × +
3 −4 +
−1
11 / 79
Stack Application
Other examples:
3 4 5 × + 6 −
3 20 + 6 −
23 6 −
17
3 4 5 6 − × +
3 4 −1 × +
3 −4 +
−1
12 / 79
Stack Application
Benefits.
no ambiguity with no brackets are required.
used by a computer to perform computations,
operands must be loaded into registers before operations can be
performed on them.
can be processed using stacks.
13 / 79
Stack Application
14 / 79
Stack Application
15 / 79
Stack Application
16 / 79
Stack Application
17 / 79
Stack Application
18 / 79
Stack Application
19 / 79
Stack Application
20 / 79
Stack Application
21 / 79
Stack Application
22 / 79
Stack Application
23 / 79
Stack Application
24 / 79
Stack Application
25 / 79
Stack Application
26 / 79
Stack Application
27 / 79
Stack Application
28 / 79
Stack Application
29 / 79
Stack Application
30 / 79
Stack Application
31 / 79
Stack Application
32 / 79
Stack Application
33 / 79
Stack Application
34 / 79
Stack Application
35 / 79
Stack Application
36 / 79
Stack Application
37 / 79
Stack Application
38 / 79
Stack Application
Result.
123 + 456 × −7 × + − 89 × +
1 − (2 + 3 + (4 − 5 × 6) × 7) + 8 × 9
39 / 79
Stack Application
For example,
1 − 2 + 3 + 4 − 5 × 6 × 7 + 8 × 9 = −132
1 2 − 3 + 4 + 5 6 7 × × − 8 9 × +
40 / 79
Stack Application
41 / 79
Stack Application
For example,
1 − ( 2 + 3 + ( 4 − 5 × 6 ) × 7 ) + 8 × 9
Output buffer,
50 / 79
Stack Application
For example,
1 − ( 2 + 3 + ( 4 − 5 × 6 ) × 7 ) + 8 × 9
Output buffer,
1
51 / 79
Stack Application
For example,
1 − ( 2 + 3 + ( 4 − 5 × 6 ) × 7 ) + 8 × 9
Output buffer,
1
52 / 79
Stack Application
For example,
1 − ( 2 + 3 + ( 4 − 5 × 6 ) × 7 ) + 8 × 9
Output buffer,
1
53 / 79
Stack Application
For example,
1 − ( 2 + 3 + ( 4 − 5 × 6 ) × 7 ) + 8 × 9
Output buffer,
1 2
54 / 79
Stack Application
For example,
1 − ( 2 + 3 + ( 4 − 5 × 6 ) × 7 ) + 8 × 9
Output buffer,
1 2
55 / 79
Stack Application
For example,
1 − ( 2 + 3 + ( 4 − 5 × 6 ) × 7 ) + 8 × 9
Output buffer,
1 2 3
56 / 79
Stack Application
For example,
1 − ( 2 + 3 + ( 4 − 5 × 6 ) × 7 ) + 8 × 9
Output buffer,
1 2 3 +
57 / 79
Stack Application
For example,
1 − ( 2 + 3 + ( 4 − 5 × 6 ) × 7 ) + 8 × 9
Output buffer,
1 2 3 +
58 / 79
Stack Application
For example,
1 − ( 2 + 3 + ( 4 − 5 × 6 ) × 7 ) + 8 × 9
Output buffer,
1 2 3 + 4
59 / 79
Stack Application
For example,
1 − ( 2 + 3 + ( 4 − 5 × 6 ) × 7 ) + 8 × 9
Output buffer,
1 2 3 + 4
60 / 79
Stack Application
For example,
1 − ( 2 + 3 + ( 4 − 5 × 6 ) × 7 ) + 8 × 9
Output buffer,
1 2 3 + 4 5
61 / 79
Stack Application
For example,
1 − ( 2 + 3 + ( 4 − 5 × 6 ) × 7 ) + 8 × 9
Output buffer,
1 2 3 + 4 5
62 / 79
Stack Application
For example,
1 − ( 2 + 3 + ( 4 − 5 × 6 ) × 7 ) + 8 × 9
Output buffer,
1 2 3 + 4 5 6
63 / 79
Stack Application
For example,
1 − ( 2 + 3 + ( 4 − 5 × 6 ) × 7 ) + 8 × 9
Output buffer,
1 2 3 + 4 5 6 × −
64 / 79
Stack Application
For example,
1 − ( 2 + 3 + ( 4 − 5 × 6 ) × 7 ) + 8 × 9
Output buffer,
1 2 3 + 4 5 6 × −
65 / 79
Stack Application
For example,
1 − ( 2 + 3 + ( 4 − 5 × 6 ) × 7 ) + 8 × 9
Output buffer,
1 2 3 + 4 5 6 × − 7
66 / 79
Stack Application
For example,
1 − ( 2 + 3 + ( 4 − 5 × 6 ) × 7 ) + 8 × 9
Output buffer,
1 2 3 + 4 5 6 × − 7 × +
67 / 79
Stack Application
For example,
1 − ( 2 + 3 + ( 4 − 5 × 6 ) × 7 ) + 8 × 9
Output buffer,
1 2 3 + 4 5 6 × − 7 × + −
68 / 79
Stack Application
For example,
1 − ( 2 + 3 + ( 4 − 5 × 6 ) × 7 ) + 8 × 9
Output buffer,
1 2 3 + 4 5 6 × − 7 × + − 8
69 / 79
Stack Application
For example,
1 − ( 2 + 3 + ( 4 − 5 × 6 ) × 7 ) + 8 × 9
Output buffer,
1 2 3 + 4 5 6 × − 7 × + − 8
70 / 79
Stack Application
For example,
1 − ( 2 + 3 + ( 4 − 5 × 6 ) × 7 ) + 8 × 9
Output buffer,
1 2 3 + 4 5 6 × − 7 × + − 8 9
71 / 79
Stack Application
For example,
1 − ( 2 + 3 + ( 4 − 5 × 6 ) × 7 ) + 8 × 9
Output buffer,
1 2 3 + 4 5 6 × − 7 × + − 8 9 × +
72 / 79
Stack Application Parsing
Application: parsing
73 / 79
Stack Application Parsing
1 <html >
2 <head ><title >Hello </title ></head >
3 <body ><p>This appears in the <i>browser </i>.</p></body >
4 </html >
74 / 79
Stack Application Parsing
75 / 79
Stack Application Parsing
76 / 79
Stack Application Function calls
77 / 79
Stack Application Function calls
Summary
78 / 79
Stack Application Function calls
Reading Material
79 / 79