堆疊與佇列 stack and queue -...

31
講師:洪安 堆疊與佇列 Stack and Queue 1

Upload: buitu

Post on 22-Mar-2018

246 views

Category:

Documents


0 download

TRANSCRIPT

Page 1: 堆疊與佇列 Stack and Queue - csie.ntu.edu.twr95116/CA200/slide/C8_StackQueue.pdf堆疊與佇列(1/3) 堆疊(Stack) 堆疊結構表示法 發牌問題 老鼠走迷宮問題 佇列(Queue)

講師:洪安

堆疊與佇列Stack and Queue

1

Page 2: 堆疊與佇列 Stack and Queue - csie.ntu.edu.twr95116/CA200/slide/C8_StackQueue.pdf堆疊與佇列(1/3) 堆疊(Stack) 堆疊結構表示法 發牌問題 老鼠走迷宮問題 佇列(Queue)

堆疊與佇列 (1/3)

堆疊 (Stack)

堆疊結構表示法

發牌問題

老鼠走迷宮問題

佇列 (Queue)

佇列結構表示法

排隊問題

2

Page 3: 堆疊與佇列 Stack and Queue - csie.ntu.edu.twr95116/CA200/slide/C8_StackQueue.pdf堆疊與佇列(1/3) 堆疊(Stack) 堆疊結構表示法 發牌問題 老鼠走迷宮問題 佇列(Queue)

堆疊與佇列 (2/3) 堆疊(Stack)

加入(push)與刪除(pop)於同一端

具有後進先出(LIFO, Last-in-First-out)或先進後出(FILO, First-in-Last-out)性質的有序串列

例子:疊盤子、發牌、走迷宮

佇列(Queue)

加入(enqueue)與刪除(dequeue)於不同端(front & rear)

先進先出(FIFO, First-in-First-out)

例子:排隊買票、坐公車

3

Page 4: 堆疊與佇列 Stack and Queue - csie.ntu.edu.twr95116/CA200/slide/C8_StackQueue.pdf堆疊與佇列(1/3) 堆疊(Stack) 堆疊結構表示法 發牌問題 老鼠走迷宮問題 佇列(Queue)

pop

堆疊與佇列 (3/3)

4

#0

#n-1

stack

#0 #1 #n-1

queue

enqueuedequeue

push

top

front rear

Page 5: 堆疊與佇列 Stack and Queue - csie.ntu.edu.twr95116/CA200/slide/C8_StackQueue.pdf堆疊與佇列(1/3) 堆疊(Stack) 堆疊結構表示法 發牌問題 老鼠走迷宮問題 佇列(Queue)

堆疊結構表示法-陣列 (1/4) 類別定義

建構函數定義

5

class Stack {

int stack[MAX_SIZE];

int top;

public:

Stack();

void push(int value);

int pop();

};

Stack::Stack() {

top = -1;

}

Page 6: 堆疊與佇列 Stack and Queue - csie.ntu.edu.twr95116/CA200/slide/C8_StackQueue.pdf堆疊與佇列(1/3) 堆疊(Stack) 堆疊結構表示法 發牌問題 老鼠走迷宮問題 佇列(Queue)

堆疊結構表示法-陣列 (2/4) 加入堆疊

void Stack::push(int value){

if( top >= (MAX_SIZE-1) ){

cout<<"堆疊容量超過!\n";exit(-1);

}//更新變數toptop++;stack[top] = value;

}

6

Page 7: 堆疊與佇列 Stack and Queue - csie.ntu.edu.twr95116/CA200/slide/C8_StackQueue.pdf堆疊與佇列(1/3) 堆疊(Stack) 堆疊結構表示法 發牌問題 老鼠走迷宮問題 佇列(Queue)

堆疊結構表示法-陣列 (3/4) 取出堆疊 int Stack::pop()

{int temp;

if( top<0 ) //判斷堆疊是否為空的{

cout<<"堆疊容量為空的!\n";return -1;

}temp = stack[top];//更新變數toptop--;return temp;

}

7

Page 8: 堆疊與佇列 Stack and Queue - csie.ntu.edu.twr95116/CA200/slide/C8_StackQueue.pdf堆疊與佇列(1/3) 堆疊(Stack) 堆疊結構表示法 發牌問題 老鼠走迷宮問題 佇列(Queue)

堆疊結構表示法-陣列 (4/4) 列出堆疊中的所有元素

8

Page 9: 堆疊與佇列 Stack and Queue - csie.ntu.edu.twr95116/CA200/slide/C8_StackQueue.pdf堆疊與佇列(1/3) 堆疊(Stack) 堆疊結構表示法 發牌問題 老鼠走迷宮問題 佇列(Queue)

堆疊結構表示法-鏈結串列 (1/4) 類別定義

建構函數定義

9

class node {

private:

int data;

node *next;

friend class Stack;

};

class Stack {

node *top;

public:

Stack();

void push(int value);

int pop();

};

Stack::Stack(){

top=NULL;

}

Page 10: 堆疊與佇列 Stack and Queue - csie.ntu.edu.twr95116/CA200/slide/C8_StackQueue.pdf堆疊與佇列(1/3) 堆疊(Stack) 堆疊結構表示法 發牌問題 老鼠走迷宮問題 佇列(Queue)

堆疊結構表示法-鏈結串列 (2/4) 加入堆疊

void Stack::push(int value){

node *new_node;

new_node = new node;if( new_node == NULL){ //判斷配置是否成功

cout << "記憶體配置失敗!\n";exit(1);

}new_node->data = value;

new_node->next = top;

top = new_node;}

10NULL

top

1

top

2

3

1

2

3

Page 11: 堆疊與佇列 Stack and Queue - csie.ntu.edu.twr95116/CA200/slide/C8_StackQueue.pdf堆疊與佇列(1/3) 堆疊(Stack) 堆疊結構表示法 發牌問題 老鼠走迷宮問題 佇列(Queue)

堆疊結構表示法-鏈結串列 (3/4) 取出堆疊

int Stack::pop(){

int temp;node *ptr;

if( top != NULL ) //判斷堆疊是否為空的{//更新top, temp, stack指標ptr = top; //1top = top->next; //2temp = ptr->data; //3free(ptr); //4return temp;}else

exit(1);}

11 NULL

top

1

2

3

ptr

top

temp

4

Page 12: 堆疊與佇列 Stack and Queue - csie.ntu.edu.twr95116/CA200/slide/C8_StackQueue.pdf堆疊與佇列(1/3) 堆疊(Stack) 堆疊結構表示法 發牌問題 老鼠走迷宮問題 佇列(Queue)

堆疊結構表示法-鏈結串列 (4/4) 列出堆疊中的所有元素

12

Page 13: 堆疊與佇列 Stack and Queue - csie.ntu.edu.twr95116/CA200/slide/C8_StackQueue.pdf堆疊與佇列(1/3) 堆疊(Stack) 堆疊結構表示法 發牌問題 老鼠走迷宮問題 佇列(Queue)

free(ptr);

13

Page 14: 堆疊與佇列 Stack and Queue - csie.ntu.edu.twr95116/CA200/slide/C8_StackQueue.pdf堆疊與佇列(1/3) 堆疊(Stack) 堆疊結構表示法 發牌問題 老鼠走迷宮問題 佇列(Queue)

課堂練習-發牌問題 使用堆疊結構實作發牌程式

8_stack_array.cpp

8_stack_linkedlist.cpp

14

Page 15: 堆疊與佇列 Stack and Queue - csie.ntu.edu.twr95116/CA200/slide/C8_StackQueue.pdf堆疊與佇列(1/3) 堆疊(Stack) 堆疊結構表示法 發牌問題 老鼠走迷宮問題 佇列(Queue)

補充:隨機產生亂數 產生隨機亂數的函式:rand()

需#include<stdlib.h>

Ex: 產生一個0~99的整數亂數a = rand()%100;

Ex: 產生100~200的整數亂數 a= rand()%100+100;

初始化亂數表:srand(time(NULL))

需 #include <time.h>

功能:以電腦的時間作為亂數表的起始參數,否則每次程式執行所取的亂數都會一樣

15

Page 16: 堆疊與佇列 Stack and Queue - csie.ntu.edu.twr95116/CA200/slide/C8_StackQueue.pdf堆疊與佇列(1/3) 堆疊(Stack) 堆疊結構表示法 發牌問題 老鼠走迷宮問題 佇列(Queue)

課堂練習-老鼠走迷宮問題 (1/2) 使用堆疊結構實作老鼠走迷宮問題

8_stack_maze.cpp

16

0 可走1 不能走3 死路2 正確路徑1 1 1 1 1 1 1 1 1 1

1 0 1 0 1 0 0 0 0 1

1 0 1 0 1 0 1 1 0 1

1 0 1 0 1 1 1 0 0 1

1 0 1 0 0 0 0 0 1 1

1 0 0 0 1 1 1 0 0 1

1 1 1 1 1 1 1 1 1 1

起點(1,1)

終點(8,5)

Page 17: 堆疊與佇列 Stack and Queue - csie.ntu.edu.twr95116/CA200/slide/C8_StackQueue.pdf堆疊與佇列(1/3) 堆疊(Stack) 堆疊結構表示法 發牌問題 老鼠走迷宮問題 佇列(Queue)

課堂練習-老鼠走迷宮問題 (2/2) 使用堆疊結構實作老鼠走迷宮問題

8_stack_maze.cpp

17

0 可走1 不能走3 死路2 正確路徑1 1 1 1 1 1 1 1 1 1

1 2 1 3 1 3 3 3 3 1

1 2 1 3 1 3 1 1 3 1

1 2 1 3 1 1 1 3 3 1

1 2 1 2 2 2 2 2 1 1

1 2 2 2 1 1 1 2 2 1

1 1 1 1 1 1 1 1 1 1

起點(1,1)

終點(8,5)

Page 18: 堆疊與佇列 Stack and Queue - csie.ntu.edu.twr95116/CA200/slide/C8_StackQueue.pdf堆疊與佇列(1/3) 堆疊(Stack) 堆疊結構表示法 發牌問題 老鼠走迷宮問題 佇列(Queue)

堆疊的應用-副程式的呼叫和返回

a

b

a

程式A呼叫副程式B 程式B再呼叫副程式C

18

Page 19: 堆疊與佇列 Stack and Queue - csie.ntu.edu.twr95116/CA200/slide/C8_StackQueue.pdf堆疊與佇列(1/3) 堆疊(Stack) 堆疊結構表示法 發牌問題 老鼠走迷宮問題 佇列(Queue)

堆疊的應用-河內塔(1/2) 堆疊的應用

http://tw.t45ol.com/play/137/tower-of-hanoi.html1. 每次僅可搬移一個碟片

2. 從某一根柱子上取出之碟片一定要放到另外兩根柱子之一後才可以再搬另一個碟片

3. 無論何時,每一根柱子上之碟片都要保持上小下大的排列方式

A 柱

(a)

(b)

B 柱 C 柱

C 柱B 柱A 柱

19

Page 20: 堆疊與佇列 Stack and Queue - csie.ntu.edu.twr95116/CA200/slide/C8_StackQueue.pdf堆疊與佇列(1/3) 堆疊(Stack) 堆疊結構表示法 發牌問題 老鼠走迷宮問題 佇列(Queue)

堆疊的應用-河內塔(2/2) 當 N=3 時,整個搬移過程為:

o 步驟 < 1> 從 A 柱搬 1 號碟片到 C 柱

o 步驟 < 2> 從 A 柱搬 2 號碟片到 B 柱

o 步驟 < 3> 從 C 柱搬 1 號碟片到 B 柱

o 步驟 < 4> 從 A 柱搬 3 號碟片到 C 柱

o 步驟 < 5> 從 B 柱搬 1 號碟片到 A 柱

o 步驟 < 6> 從 B 柱搬 2 號碟片到 C 柱

o 步驟 < 7> 從 A 柱搬 1 號碟片到 C 柱

20

Page 21: 堆疊與佇列 Stack and Queue - csie.ntu.edu.twr95116/CA200/slide/C8_StackQueue.pdf堆疊與佇列(1/3) 堆疊(Stack) 堆疊結構表示法 發牌問題 老鼠走迷宮問題 佇列(Queue)

佇列結構表示法-陣列 (1/3) 類別定義

建構函數定義

21

Page 22: 堆疊與佇列 Stack and Queue - csie.ntu.edu.twr95116/CA200/slide/C8_StackQueue.pdf堆疊與佇列(1/3) 堆疊(Stack) 堆疊結構表示法 發牌問題 老鼠走迷宮問題 佇列(Queue)

佇列結構表示法-陣列 (2/3) 加入佇列

22

Page 23: 堆疊與佇列 Stack and Queue - csie.ntu.edu.twr95116/CA200/slide/C8_StackQueue.pdf堆疊與佇列(1/3) 堆疊(Stack) 堆疊結構表示法 發牌問題 老鼠走迷宮問題 佇列(Queue)

佇列結構表示法-陣列 (3/3) 取出佇列

23

Page 24: 堆疊與佇列 Stack and Queue - csie.ntu.edu.twr95116/CA200/slide/C8_StackQueue.pdf堆疊與佇列(1/3) 堆疊(Stack) 堆疊結構表示法 發牌問題 老鼠走迷宮問題 佇列(Queue)

佇列結構表示法-鏈結串列 (1/3) 類別定義

建構函數定義

24

Page 25: 堆疊與佇列 Stack and Queue - csie.ntu.edu.twr95116/CA200/slide/C8_StackQueue.pdf堆疊與佇列(1/3) 堆疊(Stack) 堆疊結構表示法 發牌問題 老鼠走迷宮問題 佇列(Queue)

佇列結構表示法-鏈結串列 (2/3) 加入佇列

25

Page 26: 堆疊與佇列 Stack and Queue - csie.ntu.edu.twr95116/CA200/slide/C8_StackQueue.pdf堆疊與佇列(1/3) 堆疊(Stack) 堆疊結構表示法 發牌問題 老鼠走迷宮問題 佇列(Queue)

佇列結構表示法-鏈結串列 (3/3) 取出佇列

26

Page 27: 堆疊與佇列 Stack and Queue - csie.ntu.edu.twr95116/CA200/slide/C8_StackQueue.pdf堆疊與佇列(1/3) 堆疊(Stack) 堆疊結構表示法 發牌問題 老鼠走迷宮問題 佇列(Queue)

小練習 實作佇列結構 實作enqueue()和dequeue()成員函數 實作print()成員函數 此函數可印出所有佇列中的資料

27

int main()

{

Queue q;

q.enqueue(10);

q.enqueue(20);

q.enqueue(25);

q.print();

q.dequeue();

q.print();

system("pause");

return 0;

}

主程式

輸出

Page 28: 堆疊與佇列 Stack and Queue - csie.ntu.edu.twr95116/CA200/slide/C8_StackQueue.pdf堆疊與佇列(1/3) 堆疊(Stack) 堆疊結構表示法 發牌問題 老鼠走迷宮問題 佇列(Queue)

課堂練習-加入使用者介面 使用佇列結構加上使用者介面,讓使用者加入資料、

取出資料

8_queue_array.cpp

8_queue_linkedlist.cpp

28

Page 29: 堆疊與佇列 Stack and Queue - csie.ntu.edu.twr95116/CA200/slide/C8_StackQueue.pdf堆疊與佇列(1/3) 堆疊(Stack) 堆疊結構表示法 發牌問題 老鼠走迷宮問題 佇列(Queue)

佇列的應用-優先佇列 (1/2)

優先佇列結構 (採用陣列實施)

front_b,B 等級佇列的首端

C4

C3

C2

C1

4

3

2

1

0

B2

B1

A3

A2

A1

8

7

6

5

A 等級

B 等級

C 等級

rear_a ,A 等級佇列的尾端

front_a,A 等級佇列的首端

front_c,C 等級佇列的首端

rear_c ,C 等級佇列的尾端

rear_b ,B 等級佇列的尾端

29

Page 30: 堆疊與佇列 Stack and Queue - csie.ntu.edu.twr95116/CA200/slide/C8_StackQueue.pdf堆疊與佇列(1/3) 堆疊(Stack) 堆疊結構表示法 發牌問題 老鼠走迷宮問題 佇列(Queue)

佇列的應用-優先佇列 (2/2)

優先佇列結構(採用鏈結串列實施)

Priority Queue

A2A1 B1A3 C1B2 C3C2

front rear_a rear_b rear_c 0

C4 0

B3

30

Page 31: 堆疊與佇列 Stack and Queue - csie.ntu.edu.twr95116/CA200/slide/C8_StackQueue.pdf堆疊與佇列(1/3) 堆疊(Stack) 堆疊結構表示法 發牌問題 老鼠走迷宮問題 佇列(Queue)

佇列的應用-雙向佇列 左端佇列加入A、B、C,右端佇列加入甲、乙、丙

A

0

B

1

C

2 3

n-3

n-2

n-1n-4

‧‧‧

fl

rl

fr

rr

31