Download presentation
Presentation is loading. Please wait.
1
1 Chapter 8 Stacks and Queues
2
2 This is a stack of books.
3
3 This is a queue.
4
4 Chap.8 Contents 8.1 Stacks 8.1.1 The PureStack Interface 8.1.2 Implementations of the PureStack Interface 8.1.3 Stack Application 1: How Compilers Implement Recursion 8.1.4 Stack Application 2: Converting from Infix to Postfix 8.1.5 Prefix Notation 8.2 Queues 8.2.1 The PureQueue Interface 8.2.2 Implementations of the PureQueue Interface 8.2.3 Computer Simulation 8.2.4 Queue Application: A Simulated Car Wash
5
5 8.1 Stacks
6
6
7
7 Object-Oriented (O-O) Terms Stack, Queue 是特別的 list. 以 O-O 術語, 它們 EXTEND ( 延伸 ) list (ArrayList 或 LinkedList) General concepts ( 通用觀念 ) are Super class such as ArrayList Specialized concepts ( 專用觀念 ) are Sub class such as Stack
8
8
9
9
10
10
11
11
12
12
13
13
14
14
15
15 8.1.1 The PureStack Interface
16
16
17
17
18
18 8.1.2 實作 PureStack Interface
19
19
20
20
21
21
22
22
23
23
24
24 寫出下列的輸出結果 : LinkedListPureStack myStack = new LinkedListPureStack ( ); for (int i = 0; i < 10; i++) myStack.push (i * i); while (!myStack.isEmpty( )) System.out.println (myStack.pop( )); ANS: 81 (9*9), 64 (8*8), 49, 36, 25, 16, 9, 4, 1, 0
25
25 8.1.3 Stack 的應用 1: Compiler 如何實作 Recursive
26
26
27
27 Run-time stack 處理 activation records. Push: When method is called Pop: When execution of method is completed
28
28
29
29
30
30
31
31
32
32
33
33 8.1.4 Stack 應用 2: 轉換 Infix( 中置式 ) 到 Postfix( 後置式 ) 在 infix 表示法中, operator( 運算子 ) 放置於 operands( 運算元 ) 之間. For example: a + b c – d + (e * f – g * h) / i
34
34 Old compilers: Infix Machine language 這會因為 parentheses( 小括號 ) 而產生混亂. Newer compilers: Infix Postfix Machine language
35
35 在 postfix 表示法中, an operator 直接地放置 於他的 operands 之後. No parenthesis needed. InfixPostfix a + bab+ a + b * cabc*+ a * b + cab*c+ (a + b) * cab+c*
36
36 postfix 不必使用 Parentheses 很棒 !
37
37 Let’s convert an infix string below to a postfix string. x – y * z ANS: xyz*-
38
38 Postfix 保留 operands 的先後順序, so an operand can be appended to postfix as soon as that operand is encountered in infix.
39
39 InfixPostfix x – y * z x
40
40 InfixPostfix x – y * z x The operands for ‘-’ 尚未在 postfix, 所以 ‘ - ’ 一定要先暫存在某地方.
41
41 InfixPostfix x – y * z xy
42
42 InfixPostfix x – y * z xy The operands for ‘*’ 未在 postfix, 所以 ‘*’ 一定要暫存在某地方, 且儲存在 ‘-’ 之前.
43
43 InfixPostfix x – y * z xyz
44
44 InfixPostfix x – y * z xyz* – ANS.
45
45 As another test case, we start with x*y-z. After moving ‘x’ to postfix, ‘*’ is temporarily saved, and then ‘y’ 被加到 postfix. What happens when ‘-’ is accessed? InfixPostfix x * y – z xy
46
46
47
47 暫時儲存處是 : stack ! Here is the strategy (pseudo-code) for maintaining the stack:
48
48
49
49 口訣 : if Infix Greater, then Push
50
50 Convert from infix to postfix: InfixPostfix a + b * c / d - e
51
51 Infix Postfix a + b * c / d – e abc*d/+e – * + Operator stack
52
52
53
53 Convert to postfix: x * (y + z)
54
54
55
55
56
56
57
57
58
58 Infix token
59
59 Tokens
60
60
61
61 12
62
62 8.1.5 Prefix( 前置式 ) Notation
63
63
64
64
65
65
66
66
67
67
68
68
69
69 8.2 Queues
70
70
71
71 回想一下; STACK: (Last-In-First-Out) LIFO
72
72 Enqueue “ 張三 ”
73
73 張三 FrontBack
74
74 Enqueue “ 李四 ”
75
75 張三 Front 李四 Back
76
76 Enqueue “ 王五 ”
77
77 王五張三 Front 李四 Back
78
78 Dequeue
79
79 王五 Front 李四 Back
80
80 8.2.1 The PureQueue Interface
81
81
82
82
83
83
84
84 8.2.2 Implementations of the PureQueue Interface
85
85
86
86
87
87 Heavy Inheritance Tax 在 class reuse 實務上, 被 reuse 的 class 要有完整 method description developers 清楚了解後, 再寫 dummy code ( 如上述 get) 來 override 不能 reuse 的 methods. 這是相當沉重的負擔,有如稅負,故叫 Inheritance Tax.
88
88
89
89
90
90 Determine the output from the following: LinkedListPureQueue myQueue = new LinkedListPureQueue (); for (int i = 0; i < 10; i++) myQueue.enqueue (i * i); while (!myQueue.isEmpty( )) System.out.println (myQueue.dequeue( )); ANS: 0(0*0),1(1*1),4, 9, 16, 25, 36, 49, 64, 81
91
91 8.2.3 Computer Simulation( 模擬 ) SYSTEM 是由互動的部分 (interacting parts) 所組成
92
92 model 是 system 的簡化. 建構 model 是為了研究 system
93
93
94
94
95
95
96
96
97
97
98
98
99
99
100
100 8.2.4 Queue Application: A Simulated Car Wash
101
101 carWash Minimal waiting time is 0 min. Maximal waiting time:10 min*5car= 50 min car queue arrival departure from queue to queue Wash Station
102
102
103
103
104
104
105
105
106
106
107
107
108
108
109
109
110
110
111
111
112
112
113
113
114
114
115
115 Car class /* Car * 初始化 Car object * 為指定的下次到達時間 */ public Car (int nextArrivalTime) /* getArrivalTime /* 記錄 the just dequeued car 的 arrival time. * @return the arrival time of this car */ public int getArrivalTime()
116
116
117
117
118
118
119
119
120
120
121
121
122
122
123
123
124
124
125
125
126
126
127
127 public class Car{ protected int arrivalTime; public Car(){}//default constructor, //for the sake of sub-classses of Car public Car (int nextArrivalTime){ arrivalTime=nextArrivalTime; }// constructor with int parameter public int getArrivalTime(){ return arrivalTime; }/*getArrivalTime*/ }/*Car*/
128
128
129
129
130
130
Similar presentations
© 2025 SlidePlayer.com. Inc.
All rights reserved.