Advance Data Structure Review of Chapter 3 張啟中. Queue An ordered list All insertions take...
-
date post
18-Dec-2015 -
Category
Documents
-
view
216 -
download
1
Transcript of Advance Data Structure Review of Chapter 3 張啟中. Queue An ordered list All insertions take...
![Page 1: Advance Data Structure Review of Chapter 3 張啟中. Queue An ordered list All insertions take place at one end, rear All deletions take place at the opposite.](https://reader035.fdocument.pub/reader035/viewer/2022062515/56649d255503460f949fbe0c/html5/thumbnails/1.jpg)
Advance Data StructureReview of Chapter 3
張啟中
![Page 2: Advance Data Structure Review of Chapter 3 張啟中. Queue An ordered list All insertions take place at one end, rear All deletions take place at the opposite.](https://reader035.fdocument.pub/reader035/viewer/2022062515/56649d255503460f949fbe0c/html5/thumbnails/2.jpg)
Queue
Queue An ordered list All insertions take place at one end, rear All deletions take place at the opposite end, front First-In-First-Out (FIFO)
![Page 3: Advance Data Structure Review of Chapter 3 張啟中. Queue An ordered list All insertions take place at one end, rear All deletions take place at the opposite.](https://reader035.fdocument.pub/reader035/viewer/2022062515/56649d255503460f949fbe0c/html5/thumbnails/3.jpg)
Abstract Data Type for Queuetemplate <class KeyType>class Queue { //objects: a finite ordered list with zero or more elements. public: Queue(int MaxQueueSize=DefaultSize); //create an empty queue whose maximum size is MaxQueueSize Boolean IsFullQ(); //if (number of elements in queue== MaxQueueSize) return TRUE //else return FALSE void Add(const Keytype& item); //if (IsFullQ()) then QueueFull() //else insert item at rear of queue Boolean IsEmptyQ(); //if number of elements in the queue is equal to 0 then return TRUE //else return FALSE Keytype* Delete(KeyType&); //if (IsEmpty()) then QueueEmpty() and return 0 //else remove the item at front of queue and return a pointer to it
};
![Page 4: Advance Data Structure Review of Chapter 3 張啟中. Queue An ordered list All insertions take place at one end, rear All deletions take place at the opposite.](https://reader035.fdocument.pub/reader035/viewer/2022062515/56649d255503460f949fbe0c/html5/thumbnails/4.jpg)
Representation: Queue Definition
private: int front, rear; KeyType *queue; int MaxSize;
Implementation of Queuetemplate <class KeyType>Queue<KeyType>::Queue(int MaxQueueSize):MaxSize(MaxQueueSize)
{ queue = new Keytype[MaxSize]; front = rear = -1;}
![Page 5: Advance Data Structure Review of Chapter 3 張啟中. Queue An ordered list All insertions take place at one end, rear All deletions take place at the opposite.](https://reader035.fdocument.pub/reader035/viewer/2022062515/56649d255503460f949fbe0c/html5/thumbnails/5.jpg)
Queue 的操作
Implementation of member function IsFull() Implementation of member function IsEmpty() Implementation of member function Add() Implementation of member function Delete()
See textbook pp132-133
![Page 6: Advance Data Structure Review of Chapter 3 張啟中. Queue An ordered list All insertions take place at one end, rear All deletions take place at the opposite.](https://reader035.fdocument.pub/reader035/viewer/2022062515/56649d255503460f949fbe0c/html5/thumbnails/6.jpg)
Queue Implementation I: Linear Array
……
0 1 2 3 4 MaxSize -1
front = rear = -1
Initially
![Page 7: Advance Data Structure Review of Chapter 3 張啟中. Queue An ordered list All insertions take place at one end, rear All deletions take place at the opposite.](https://reader035.fdocument.pub/reader035/viewer/2022062515/56649d255503460f949fbe0c/html5/thumbnails/7.jpg)
Queue Implementation I: Linear Array
after Insert 4 elements
MaxSize -1
front = -1
……
0 1 2 3 4
rear = 3
Insert
template <class KeyType>void Queue<KeyType>::Add(const KeyType& x){ if (isFull()) QueueFull(); else queue[++rear] = x;}
![Page 8: Advance Data Structure Review of Chapter 3 張啟中. Queue An ordered list All insertions take place at one end, rear All deletions take place at the opposite.](https://reader035.fdocument.pub/reader035/viewer/2022062515/56649d255503460f949fbe0c/html5/thumbnails/8.jpg)
Queue Implementation I: Linear Array MaxSize -1
front = 1
……
0 1 2 3 4
rear = 3
delete
template <class KeyType>KeyType* Queue<KeyType>::Delete(){ if (isEmpty()) { QueueEmpty(); return 0; } x = queue[++front]; return &x;)
after delete 2 elements
![Page 9: Advance Data Structure Review of Chapter 3 張啟中. Queue An ordered list All insertions take place at one end, rear All deletions take place at the opposite.](https://reader035.fdocument.pub/reader035/viewer/2022062515/56649d255503460f949fbe0c/html5/thumbnails/9.jpg)
Queue Implementation I: Linear Array 如何判斷 Queue 是空的? front == rear
如何判斷 Queue 是滿的? rear == MaxSize-1
MaxSize -1
……
0 1 2 3 4
front = rear = 3 after delete 4 elements
after Insert MaxSize elementsfront = -1
MaxSize -1
……
0 1 2 3 4
rear = MaxSize-1
![Page 10: Advance Data Structure Review of Chapter 3 張啟中. Queue An ordered list All insertions take place at one end, rear All deletions take place at the opposite.](https://reader035.fdocument.pub/reader035/viewer/2022062515/56649d255503460f949fbe0c/html5/thumbnails/10.jpg)
Queue Implementation I: Linear Array 用 Linear Array 實作 Queue ,在新增時會面臨兩種最壞的情況。 解決方法:依序搬移 Queue 中的元素位置( case 2 需先刪除第
一個元素),但其代價需 O(MaxSize)
MaxSize -1
front = 3
……
0 1 2 3 4
rear = MaxSize-1
Case 1:
MaxSize -1
front = -1
……
0 1 2 3 4
rear = MaxSize-1
Case 2:
![Page 11: Advance Data Structure Review of Chapter 3 張啟中. Queue An ordered list All insertions take place at one end, rear All deletions take place at the opposite.](https://reader035.fdocument.pub/reader035/viewer/2022062515/56649d255503460f949fbe0c/html5/thumbnails/11.jpg)
為了改善 linear array 的缺失,必須將 Queue 改成環形陣列。所謂的環形陣列,是一個普通的陣列,但在概念上將其首尾相接而成。
0 2 3 4 5 6 71
0
1
2
3 4
5
6
7front
Rear
Queue Implementation II: Circular Array
![Page 12: Advance Data Structure Review of Chapter 3 張啟中. Queue An ordered list All insertions take place at one end, rear All deletions take place at the opposite.](https://reader035.fdocument.pub/reader035/viewer/2022062515/56649d255503460f949fbe0c/html5/thumbnails/12.jpg)
template <class KeyType>Queue<KeyType>::Queue(void):MaxSize(MaxQueueSize){ queue = new KeyType[MaxSize]; front = rear= 0;}
Queue Implementation II: Circular Array
front = rear = 0
Initially
0
1
2
3 4
MaxSize-1
●
●
![Page 13: Advance Data Structure Review of Chapter 3 張啟中. Queue An ordered list All insertions take place at one end, rear All deletions take place at the opposite.](https://reader035.fdocument.pub/reader035/viewer/2022062515/56649d255503460f949fbe0c/html5/thumbnails/13.jpg)
Queue Implementation II: Circular Array
0
1
2
3 4
●
●
MaxSize-1
front = 0
rear = 3
after insert 3 elements
Insert
0
1
2
3 4
●
●
MaxSize-1
front = 2
rear = 3
after delete 2 elements
Delete
![Page 14: Advance Data Structure Review of Chapter 3 張啟中. Queue An ordered list All insertions take place at one end, rear All deletions take place at the opposite.](https://reader035.fdocument.pub/reader035/viewer/2022062515/56649d255503460f949fbe0c/html5/thumbnails/14.jpg)
Queue Implementation II: Circular Array 用 circular Array 實作 Queue ,會面臨無法判斷 Queue 是空的
或是滿的問題。因為 front == rear 時 Queue 可能為空,也可能為滿。
如何解決?
0
1
2
3 4
●
●
MaxSize-1
front = rear = 3 Empty
0
1
2
3 4
●
●
MaxSize-1
front = rear = 3 Full
![Page 15: Advance Data Structure Review of Chapter 3 張啟中. Queue An ordered list All insertions take place at one end, rear All deletions take place at the opposite.](https://reader035.fdocument.pub/reader035/viewer/2022062515/56649d255503460f949fbe0c/html5/thumbnails/15.jpg)
Queue Implementation II: Circular Array 解決方法,有二種
Method 1 :犧牲空間 任何時候只允許 MaxSize-1 個元素在 Queue 中 Method 2 :犧牲時間
增加一個變數,記錄上一次的動作。 因為 Queue 元素的新增與刪除非常頻繁,所
以我們較常採用 Method 1 還有沒有其他的解法?(當然有)
![Page 16: Advance Data Structure Review of Chapter 3 張啟中. Queue An ordered list All insertions take place at one end, rear All deletions take place at the opposite.](https://reader035.fdocument.pub/reader035/viewer/2022062515/56649d255503460f949fbe0c/html5/thumbnails/16.jpg)
Circular Array : Method 1
template <class KeyType>void Queue<keyType>::Add(const KeyType& x){ int newrear =(rear+1) % MaxSize if (front == newrear) QueueFull(); else queue[rear=newrear]=x;}
0
1
2
3 4
●
●
MaxSize-1
front = 3
rear = 1
newrear = (rear + 1) % MaxSize
0
1
2
3 4
●
●
MaxSize-1
front = 3newrear = (rear + 1) % MaxSize
front == newrear
rear = 2
FullOK
![Page 17: Advance Data Structure Review of Chapter 3 張啟中. Queue An ordered list All insertions take place at one end, rear All deletions take place at the opposite.](https://reader035.fdocument.pub/reader035/viewer/2022062515/56649d255503460f949fbe0c/html5/thumbnails/17.jpg)
Circular Array : Method 1
template <class KeyType>KeyType* Queue<keyType>::Delete(const KeyType& x){ if (front == rear) { QueueEmpty(); return; } front = (front+1) % MaxSize; x = queue[front]; return &x;}
0
1
2
3 4
●
●
MaxSize-1
rear = 2
front = 2OK 0
1
2
3 4
●
●
MaxSize-1
front = rear = 2
Empty
![Page 18: Advance Data Structure Review of Chapter 3 張啟中. Queue An ordered list All insertions take place at one end, rear All deletions take place at the opposite.](https://reader035.fdocument.pub/reader035/viewer/2022062515/56649d255503460f949fbe0c/html5/thumbnails/18.jpg)
Circular Array : Method 2 當 add 時,令 LastOp = ‘+’ ,當 delete 時,令 LastOp = ‘-’ 。 當 front == rear 時,若 LastOp == ‘+’ 則 Queue 是滿的,反之,
若 LastOp == ‘-’ 則 Queue 為空。
Definitionprivate: int front, rear; KeyType *queue; int MaxSize; char LastOp;
Implementation of Queuetemplate <class KeyType>Queue<KeyType>::Queue(int MaxQueueSize):MaxSize(MaxQueueSize){ queue = new Keytype[MaxSize]; front = rear = -1 LastOp = ‘-’; // + add - delete}
![Page 19: Advance Data Structure Review of Chapter 3 張啟中. Queue An ordered list All insertions take place at one end, rear All deletions take place at the opposite.](https://reader035.fdocument.pub/reader035/viewer/2022062515/56649d255503460f949fbe0c/html5/thumbnails/19.jpg)
Example: Queue
Job Scheduling String convert to Integer Recognizing palindromes Event-driven Programs Process Queue
![Page 20: Advance Data Structure Review of Chapter 3 張啟中. Queue An ordered list All insertions take place at one end, rear All deletions take place at the opposite.](https://reader035.fdocument.pub/reader035/viewer/2022062515/56649d255503460f949fbe0c/html5/thumbnails/20.jpg)
Example Queue: Job Scheduling
front rear Q[0]
[1] [2] [3] [4] [5] …. Comments
-1 -1 queue empty initial
-1 0 J1 Job 1 joins Q
-1 1 J1 J2 Job 2 joins Q
-1 2 J1 J2 J3 Job 3 joins Q
0 2
J2 J3 Job 1 leaves Q
0 3
J2 J3 J4 Job 4 joins Q
1 3
J3 J4 Job 2 leaves Q
![Page 21: Advance Data Structure Review of Chapter 3 張啟中. Queue An ordered list All insertions take place at one end, rear All deletions take place at the opposite.](https://reader035.fdocument.pub/reader035/viewer/2022062515/56649d255503460f949fbe0c/html5/thumbnails/21.jpg)
Example Queue :String convert to Integer
【範例】
“bb1234” 1234 (b represents a blank)
b
bb
bb1
bb12
bb123
bb1234
b1234
1234
234 N = 0 Ch = ‘1’
34 N = 1 Ch = ‘2’
4 N = 12 Ch = ‘3’
N = 123 Ch = ‘4’
N = 1234 Ch = ‘4’
![Page 22: Advance Data Structure Review of Chapter 3 張啟中. Queue An ordered list All insertions take place at one end, rear All deletions take place at the opposite.](https://reader035.fdocument.pub/reader035/viewer/2022062515/56649d255503460f949fbe0c/html5/thumbnails/22.jpg)
Example Queue: Recognizing palindromes
S
Q
a b c d c b a
a
S
Q
a
b c d c b a
a b
S
Q
ba
c d c b a
![Page 23: Advance Data Structure Review of Chapter 3 張啟中. Queue An ordered list All insertions take place at one end, rear All deletions take place at the opposite.](https://reader035.fdocument.pub/reader035/viewer/2022062515/56649d255503460f949fbe0c/html5/thumbnails/23.jpg)
S
Q
d c b a
a b c
S
Q
c b a
a b c d c
S
Q
b a
cba
a b c d
dcba
cdcba
![Page 24: Advance Data Structure Review of Chapter 3 張啟中. Queue An ordered list All insertions take place at one end, rear All deletions take place at the opposite.](https://reader035.fdocument.pub/reader035/viewer/2022062515/56649d255503460f949fbe0c/html5/thumbnails/24.jpg)
S
Q
a
a b c d c b
bcdcba S
Q a b c d c b a
abcdcba S
Q a b c d c b a
abcdcba
![Page 25: Advance Data Structure Review of Chapter 3 張啟中. Queue An ordered list All insertions take place at one end, rear All deletions take place at the opposite.](https://reader035.fdocument.pub/reader035/viewer/2022062515/56649d255503460f949fbe0c/html5/thumbnails/25.jpg)
Example Queue: Event-driven Programs 目前流行的視窗系統(如 X
Window 、 MS Windows )是採用事件驅動( event-driven )的模式。
視窗系統必須處理許多不同的事件( event )— 如:鍵盤輸入、滑鼠移動、壓下滑鼠按鍵、放開滑鼠按鍵等等使用者的動作,以及視窗遮蓋、視窗浮現等等螢幕狀態的改變。這些事件依序地擺放在所謂的事件佇列( event queu
e )中,然後用循覆的方式依序處理(稱為事件迴路( eve
nt loop ))。
取出下一個事件
判斷是何事件
front rear
event queue
e1 e2 en
呼叫該事件的處理函式
eventloop
加入新事
件
![Page 26: Advance Data Structure Review of Chapter 3 張啟中. Queue An ordered list All insertions take place at one end, rear All deletions take place at the opposite.](https://reader035.fdocument.pub/reader035/viewer/2022062515/56649d255503460f949fbe0c/html5/thumbnails/26.jpg)
Example Queue: Process Queue 現代作業系統( operating system )為了不浪費電腦的強大能力,通常具有多工( multi-processing )的能力。 程序 (processes) 指的是正在執行的程式 多工 (multi-tasking) 指的是電腦在一個時段內能夠
同時執行多個程式。 Example: Windows 2000/XP 、 Linux 、 Mac OS
等
![Page 27: Advance Data Structure Review of Chapter 3 張啟中. Queue An ordered list All insertions take place at one end, rear All deletions take place at the opposite.](https://reader035.fdocument.pub/reader035/viewer/2022062515/56649d255503460f949fbe0c/html5/thumbnails/27.jpg)
Example Queue: Process Queue 作業系統如何做到多工?
作業系統只要同時載入若干的程式,然後在這些程式間迅速切換執行即可,由於 CPU 運算的速度和人認知的速度比較起來快了許多,因此造成了這些程式是「同時執行」的感覺。
在任一刻 CPU 仍然只能執行一個程序。
程序在整個的執行過程中,可能處於下列的狀態: 新生( new ) 執行( running ) 預備( ready ) 等待( waiting ) 結束( terminated )
![Page 28: Advance Data Structure Review of Chapter 3 張啟中. Queue An ordered list All insertions take place at one end, rear All deletions take place at the opposite.](https://reader035.fdocument.pub/reader035/viewer/2022062515/56649d255503460f949fbe0c/html5/thumbnails/28.jpg)
new
ready running
waiting
terminated
admittedinterrupt
schedulerdispatchI/O or event
completionI/O or event
wait
exit
Example Queue: Process Queue
![Page 29: Advance Data Structure Review of Chapter 3 張啟中. Queue An ordered list All insertions take place at one end, rear All deletions take place at the opposite.](https://reader035.fdocument.pub/reader035/viewer/2022062515/56649d255503460f949fbe0c/html5/thumbnails/29.jpg)
Multiple Stacks and Queues12
n
m1
n
mM0 m-1
b[0]t[0]
b[1]t[1]
b[2]t[2]
b[n]
niin
mitib
0,1][][initially
range b[i]+1 到 b[i+1] for stack i, 0 <= i < nb[n] = m-1
full See book p.156