1 Chapter 8 Stacks and Queues
2 This is a stack of books.
3 This is a queue.
4 Chap.8 Contents 8.1 Stacks The PureStack Interface Implementations of the PureStack Interface Stack Application 1: How Compilers Implement Recursion Stack Application 2: Converting from Infix to Postfix Prefix Notation 8.2 Queues The PureQueue Interface Implementations of the PureQueue Interface Computer Simulation Queue Application: A Simulated Car Wash
5 8.1 Stacks
6
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
9
10
11
12
13
14
The PureStack Interface
16
17
實作 PureStack Interface
19
20
21
22
23
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
Stack 的應用 1: Compiler 如何實作 Recursive
26
27 Run-time stack 處理 activation records. Push: When method is called Pop: When execution of method is completed
28
29
30
31
32
Stack 應用 2: 轉換 Infix( 中置式 ) 到 Postfix( 後置式 ) 在 infix 表示法中, operator( 運算子 ) 放置於 operands( 運算元 ) 之間. For example: a + b c – d + (e * f – g * h) / i
34 Old compilers: Infix Machine language 這會因為 parentheses( 小括號 ) 而產生混亂. Newer compilers: Infix Postfix Machine language
35 在 postfix 表示法中, an operator 直接地放置 於他的 operands 之後. No parenthesis needed. InfixPostfix a + bab+ a + b * cabc*+ a * b + cab*c+ (a + b) * cab+c*
36 postfix 不必使用 Parentheses 很棒 !
37 Let’s convert an infix string below to a postfix string. x – y * z ANS: xyz*-
38 Postfix 保留 operands 的先後順序, so an operand can be appended to postfix as soon as that operand is encountered in infix.
39 InfixPostfix x – y * z x
40 InfixPostfix x – y * z x The operands for ‘-’ 尚未在 postfix, 所以 ‘ - ’ 一定要先暫存在某地方.
41 InfixPostfix x – y * z xy
42 InfixPostfix x – y * z xy The operands for ‘*’ 未在 postfix, 所以 ‘*’ 一定要暫存在某地方, 且儲存在 ‘-’ 之前.
43 InfixPostfix x – y * z xyz
44 InfixPostfix x – y * z xyz* – ANS.
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
47 暫時儲存處是 : stack ! Here is the strategy (pseudo-code) for maintaining the stack:
48
49 口訣 : if Infix Greater, then Push
50 Convert from infix to postfix: InfixPostfix a + b * c / d - e
51 Infix Postfix a + b * c / d – e abc*d/+e – * + Operator stack
52
53 Convert to postfix: x * (y + z)
54
55
56
57
58 Infix token
59 Tokens
60
61 12
Prefix( 前置式 ) Notation
63
64
65
66
67
68
Queues
70
71 回想一下; STACK: (Last-In-First-Out) LIFO
72 Enqueue “ 張三 ”
73 張三 FrontBack
74 Enqueue “ 李四 ”
75 張三 Front 李四 Back
76 Enqueue “ 王五 ”
77 王五張三 Front 李四 Back
78 Dequeue
79 王五 Front 李四 Back
The PureQueue Interface
81
82
83
Implementations of the PureQueue Interface
85
86
87 Heavy Inheritance Tax 在 class reuse 實務上, 被 reuse 的 class 要有完整 method description developers 清楚了解後, 再寫 dummy code ( 如上述 get) 來 override 不能 reuse 的 methods. 這是相當沉重的負擔,有如稅負,故叫 Inheritance Tax.
88
89
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
Computer Simulation( 模擬 ) SYSTEM 是由互動的部分 (interacting parts) 所組成
92 model 是 system 的簡化. 建構 model 是為了研究 system
93
94
95
96
97
98
99
Queue Application: A Simulated Car Wash
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
103
104
105
106
107
108
109
110
111
112
113
114
115 Car class /* Car * 初始化 Car object * 為指定的下次到達時間 */ public Car (int nextArrivalTime) /* getArrivalTime /* 記錄 the just dequeued car 的 arrival time. the arrival time of this car */ public int getArrivalTime()
116
117
118
119
120
121
122
123
124
125
126
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
129
130