• 沒有找到結果。

Data structure

N/A
N/A
Protected

Academic year: 2022

Share "Data structure"

Copied!
59
0
0

加載中.... (立即查看全文)

全文

(1)

Data structure

Lecture by zolution Credit by music960633

2019/03/08

(2)

課程內容

• 0. 什麼是資料結構

• 1. stack

• 2. queue

• 3. linked list

(3)

為什麼要學資料結構

• 資料結構:如何在電腦中儲存資料

• 對於不同用途選擇不同資料結構

時間複雜度

空間複雜度

• coding複雜度

• 資料結構是一種工具

• (多種)演算法+(多種)資料結構

(4)

為什麼要學資料結構

(5)

為什麼要學資料結構

• 資料結構:如何在電腦中儲存資料

• 對於不同用途選擇不同資料結構

時間複雜度

空間複雜度

• coding複雜度

• 資料結構是一種工具

• (多種)演算法+(多種)資料結構

(6)

常見的資料結構操作

• push : 放進一個元素

• pop : 拿出一個元素

• query: 各種查詢

(7)

stack

• 堆疊

• 性質

• first in last out (FILO)

疊盤子

• 基本操作

• push:把一個元素放進stack

• pop :把一個元素拿出stack

(8)

stack

• 實做

使用陣列或linked list

• 陣列實做

用一個變數top記錄頂端的位置

• top=-1: stack是空的

• push: top加1

• pop : top減1

(9)

stack

stack 操作 結果

top

(10)

stack

stack 操作 結果

push 1

top

(11)

stack

stack

1 操作 結果

push 1

top

(12)

stack

stack

1 操作 結果

push 1 push 2

top

(13)

stack

stack

2 1 操作 結果

push 1 push 2

top

(14)

stack

stack

2 1 操作 結果

push 1 push 2

pop

top

(15)

stack

stack

1 結果

2 操作

push 1 push 2

pop

top

(16)

stack

stack

1 結果

2 操作

push 1 push 2

pop push 3

top

(17)

stack

stack

3 1 結果

2 操作

push 1 push 2

pop

push 3

top

(18)

stack

stack

3 1 結果

2 操作

push 1 push 2

pop push 3

pop

top

(19)

stack

stack

1 結果

2 3 操作

push 1 push 2

pop push 3

pop

top

(20)

stack

stack

1 結果

2 3 操作

push 1 push 2

pop push 3

pop pop

top

(21)

stack

stack 結果

2 3 1 操作

push 1 push 2

pop push 3

pop

pop

top

(22)

queue

• 佇列

• 性質

• first in first out (FIFO)

排隊

分類:單向、環狀

• 基本操作

• push:把一個元素放進queue

• pop :把一個元素拿出queue

(23)

queue

• 實做

使用陣列或linked list

• 陣列實做

用一個變數front記錄最前面的元素的「前一格」位置

用一個變數rear 記錄最後面的元素的位置

• front==rear: queue是空的

• push: rear 加1

• pop : front加1

(24)

queue

queue 操作 結果

front=rear

(25)

queue

queue 操作 結果

push 1

front=rear

(26)

queue

queue

1 操作 結果

push 1

rear

front

(27)

queue

queue

1 操作 結果

push 1 push 2

rear

front

(28)

queue

queue

2 1 操作 結果

push 1 push 2

rear

front

(29)

queue

queue

2 1 操作 結果

push 1 push 2

pop

rear

front

(30)

queue

queue

2 結果

1 操作

push 1 push 2

pop

rear

front

(31)

queue

queue

2 結果

1 操作

push 1 push 2

pop

push 3

rear

front

(32)

queue

queue

3 2 結果

1 操作

push 1 push 2

pop push 3

rear

front

(33)

queue

queue

3 2 結果

1 操作

push 1 push 2

pop push 3

pop

rear

front

(34)

queue

queue

3 結果

1 2 操作

push 1 push 2

pop push 3

pop

rear

front

(35)

queue

queue

3 操作

push 1 push 2

pop push 3

pop pop

結果 1 2

rear

front

(36)

queue

queue 結果

1 2 3 操作

push 1 push 2

pop push 3

pop pop

front=rear

(37)

queue

• 產生的問題

一個元素一旦被pop,該位置無法再放新的元素

• 解決方法

• rear到陣列最後的時候,將所有元素平移到前方

使用環狀queue

使用linked list

(38)

queue

front

queue 5 4 操作

...

...

...

push 6

rear

方法一

(39)

queue

queue

5 4 操作

...

...

...

push 6 方法一

front

rear

(40)

queue

queue

6 5 4 操作

...

...

...

push 6 方法一

front

rear

(41)

queue

front

queue 5 4 操作

...

...

...

push 6

rear

方法二

(42)

queue

front

queue 5 4

6 操作

...

...

...

push 6

rear

方法二

(43)

時間複雜度

push pop

stack O(1) O(1)

queue O(1) O(1)

(44)

Practice

• Try Homework Q36, 37

(45)

linked list

• 鏈結串列

• 基本單元:Node(data + pointer)

• 將很多Node接起來

• head指標指向第一個Node,最後一個指標指向NULL

data data NULL

head data

(46)

linked list

• 性質

每個元素只記錄他的下一個元素,外部只記錄起點(head)

動態宣告記憶體

分類:單向、雙向、環狀

不能隨機存取(random access)

• 基本操作

• insert: 在兩個元素中插入一個元素,或是在頭尾插入元素

• delete: 刪除一個元素

(47)

linked list

• 實做

先定義一個struct/class Node,作為linked list的節點,裡面存資 訊和一個指向下一個Node的指標

使用時只用一個變數head記錄linked list的起點就可以了

(48)

linked list

• 為什麼要這麼麻煩,用陣列不就好了(還能random access)?

• 1. 避免記憶體浪費

• dynamic array也可以做啊(ex: stl的vector)

• 2. 可以快速的插入和刪除節點!

• array無法做到

(49)

linked list

• insert

data head

data

data NULL

(50)

linked list

• delete

data

head data data NULL

(51)

linked list

• 用linked list實做stack

• 用linked list實做queue

(52)

linked list

• 用linked list實做stack

• push: 在head前面插入一個節點

• pop : 刪除head指向的節點

• 用linked list實做queue

還要多記錄linked list的尾端(end)

• push: 在end後面插入一個節點

• pop : 刪除head指向的節點

(53)

linked list

• 雙向

• 環狀

data data data

data data

head data

(54)

時間複雜度

random access

push front

push back

pop front

pop back

Array Yes O(n) O(1) O(n) O(1)

Linked list

No O(1) O(1) O(1) O(1)

(55)

例題思考1 – 括弧匹配

• 給一個括弧字串,判斷他是否合法

• ([]){} -> 合法

• {([[]]))} -> 不合法

• What should we do?

(56)

例題思考1 – 括弧匹配

• Sol 1: 數數

• 數有幾個左 幾個右就好

• {([)]} ?

(57)

例題思考1 – 括弧匹配

• Sol 1: 數數

• 數有幾個左 幾個右就好

• {([)]} ?

(58)

例題思考1 – 括弧匹配

• STACK!!!

(59)

例題思考2 – 長條圖的最大矩形

參考文獻

相關文件

Site Most head &neck, oral cavity rare Oral site → tongue, the floor of the mouth, palate, gingiva, vestibular mucosa, lips and mental nerve area. Floor

Features Smooth surfaced, whitish nodule, sessile, 0.3X0.4cm. Smooth surface, pink

Site Right retromolar area Mandible posterior (54%) Symptom/Sign Swelling and pain Asymptomatic and painless. swelling of the affected bone in

Union of green and round: garden hose grass peas ball pie grapes Intersection of green and round: peas grapes.

• 先定義一個 struct/class Node ,作為 linked list 的節點,裡面存資 訊和一個指向下一個 Node 的指標. • 使用時只用一個變數 head 記錄 linked

•  先定義一個struct/class Node,作為linked list的節點,裡面存資 訊和一個指向下一個Node的指標. • 

In this section we define a general model that will encompass both register and variable automata and study its query evaluation problem over graphs. The model is essentially a

we use data (random sample) to test if the data provides significant evidence to reject the null hypothesis.. If X > c reject