Presentation is loading. Please wait.

Presentation is loading. Please wait.

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.

Similar presentations


Presentation on theme: "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."— Presentation transcript:

1 Advance Data Structure Review of Chapter 3 張啟中

2 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)

3 Abstract Data Type for Queue template 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 };

4 Representation: Queue Definition private: int front, rear; KeyType *queue; int MaxSize; Implementation of Queue template Queue ::Queue(int MaxQueueSize):MaxSize(MaxQueueSize) { queue = new Keytype[MaxSize]; front = rear = -1; }

5 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

6 Queue Implementation I: Linear Array …… 01234MaxSize -1 front = rear = -1 Initially

7 Queue Implementation I: Linear Array after Insert 4 elements MaxSize -1 front = -1 …… 01234 rear = 3 Insert template void Queue ::Add(const KeyType& x) { if (isFull()) QueueFull(); else queue[++rear] = x; }

8 Queue Implementation I: Linear Array MaxSize -1 front = 1 …… 01234 rear = 3 delete template KeyType* Queue ::Delete() { if (isEmpty()) { QueueEmpty(); return 0; } x = queue[++front]; return &x; ) after delete 2 elements

9 Queue Implementation I: Linear Array 如何判斷 Queue 是空的? front == rear 如何判斷 Queue 是滿的? rear == MaxSize-1 MaxSize -1 …… 01234 front = rear = 3 after delete 4 elements after Insert MaxSize elements front = -1 MaxSize -1 …… 01234 rear = MaxSize-1

10 Queue Implementation I: Linear Array 用 Linear Array 實作 Queue ,在新增時會面臨兩種最壞的情況。 解決方法:依序搬移 Queue 中的元素位置( case 2 需先刪除第一 個元素),但其代價需 O(MaxSize) MaxSize -1 front = 3 …… 01234 rear = MaxSize-1 Case 1: MaxSize -1 front = -1 …… 01234 rear = MaxSize-1 Case 2:

11 為了改善 linear array 的缺失,必須將 Queue 改成環形陣列。 所謂的環形陣列,是一個普通的陣列,但在概念上將其首尾相 接而成。 02345671 0 1 2 3 4 5 6 7 front Rear Queue Implementation II: Circular Array

12 template Queue ::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 ● ●

13 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

14 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

15 Queue Implementation II: Circular Array 解決方法,有二種  Method 1 :犧牲空間 任何時候只允許 MaxSize-1 個元素在 Queue 中  Method 2 :犧牲時間 增加一個變數,記錄上一次的動作。 因為 Queue 元素的新增與刪除非常頻繁,所以 我們較常採用 Method 1 還有沒有其他的解法?(當然有)

16 Circular Array : Method 1 template void Queue ::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 = 3 newrear = (rear + 1) % MaxSize front == newrear rear = 2 Full OK

17 Circular Array : Method 1 template KeyType* Queue ::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 = 2 OK 0 1 2 3 4 ● ● MaxSize-1 front = rear = 2 Empty

18 Circular Array : Method 2 當 add 時,令 LastOp = ‘+’ ,當 delete 時,令 LastOp = ‘-’ 。 當 front == rear 時,若 LastOp == ‘+’ 則 Queue 是滿的,反之,若 LastOp == ‘-’ 則 Queue 為空。 Definition private: int front, rear; KeyType *queue; int MaxSize; char LastOp; Implementation of Queue template Queue ::Queue(int MaxQueueSize):MaxSize(MaxQueueSize) { queue = new Keytype[MaxSize]; front = rear = -1 LastOp = ‘-’; // + add - delete }

19 Example: Queue Job Scheduling String convert to Integer Recognizing palindromes Event-driven Programs Process Queue

20 Example Queue: Job Scheduling frontrearQ[0][1][2][3][4][5]….Comments queue emptyinitial 0J1Job 1 joins Q 1J1J2Job 2 joins Q 2J1J2J3Job 3 joins Q 02 J2J3Job 1 leaves Q 03 J2J3J4Job 4 joins Q 13 J3J4Job 2 leaves Q

21 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’

22 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 baba c d c b a

23 S Q d c b a a b c S Q c b a a b c d c S Q b a cbacba a b c d dcbadcba cdcbacdcba

24 S Q a a b c d c b bcdcbabcdcba S Q a b c d c b a abcdcbaabcdcba S Q abcdcbaabcdcba

25 Example Queue: Event-driven Programs 目前流行的視窗系統(如 X Window 、 MS Windows )是 採用事件驅動( event-driven ) 的模式。 視窗系統必須處理許多不同 的事件( event ) — 如:鍵盤 輸入、滑鼠移動、壓下滑鼠 按鍵、放開滑鼠按鍵等等使 用者的動作,以及視窗遮蓋、 視窗浮現等等螢幕狀態的改 變。這些事件依序地擺放在 所謂的事件佇列( event queue ) 中,然後用循覆的方式依序 處理(稱為事件迴路( event loop ))。 取出下一個事件 判斷是何事件 frontrear event queue 呼叫該事件的 處理函式 event loop

26 Example Queue: Process Queue 現代作業系統( operating system )為了不浪費 電腦的強大能力,通常具有多工( multi- processing )的能力。  程序 (processes) 指的是正在執行的程式  多工 (multi-tasking) 指的是電腦在一個時段內能夠同 時執行多個程式。  Example: Windows 2000/XP 、 Linux 、 Mac OS 等

27 Example Queue: Process Queue 作業系統如何做到多工?  作業系統只要同時載入若干的程式,然後在這些程式間迅速切換執 行即可,由於 CPU 運算的速度和人認知的速度比較起來快了許多, 因此造成了這些程式是「同時執行」的感覺。  在任一刻 CPU 仍然只能執行一個程序。 程序在整個的執行過程中,可能處於下列的狀態:  新生( new )  執行( running )  預備( ready )  等待( waiting )  結束( terminated )

28 new readyrunning waiting terminated admitted interrupt scheduler dispatch I/O or event completion I/O or event wait exit Example Queue: Process Queue

29 Multiple Stacks and Queues M 0 m-1 b[0] t[0] b[1] t[1] b[2] t[2] b[n] initially range b[i]+1 到 b[i+1] for stack i, 0 <= i < n b[n] = m-1 full See book p.156


Download ppt "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."

Similar presentations


Ads by Google