View
253
Download
0
Embed Size (px)
Citation preview
Implementing Data Structures資料結構實做
Storing Arrays• 儲存陣列 有點類似「表格」概念,每一格儲
存我們要的資訊
Homogeneous Arrays
• 同質陣列長方形的資料區塊每一項有相同性質
Homogeneous Arrays
• Row-major order versus column major order• Address polynomial for each of them is continuous 由列與行來儲存的,例如 :4 列 *4 行 = 16 個
儲存空間,並且編號是連續的。
Figure 8.5• int Readings[24]; // 宣告一個可存 24 個整數的空間(陣列)
The array of temperature readings stored in memory starting at address x
記憶單位
位址
Figure 8.5• int Readings[24]; // 宣告一個可存 24 個整數的空間(陣列)
• Readings[4]67;
The array of temperature readings stored in memory starting at address x
67記憶單位
位址
多維陣列
多維陣列• int a[5]; 一維陣列
多維陣列• int a[5]; 一維陣列• int b[5][5]; 二維陣列
多維陣列• int a[5]; 一維陣列• int b[5][5]; 二維陣列• int c[5][5][5]; 三維陣列
多維陣列• int a[5]; 一維陣列• int b[5][5]; 二維陣列• int c[5][5][5]; 三維陣列• int d[5][5][5][5];• int e[5][5][5][5][5];• ......
那這些多維的你能想像嗎! ?
• Row-major (row by row)
• Column major (column by column) 個整數的空間(陣列)
• Row-major (row by row)
• Column major (column by column) 個整數的空間(陣列)
• Row-major (row by row)
• Column major (column by column) 個整數的空間(陣列)
ex:Pascal, C/C++, Java, C#
ex:FORTRAN
ex:Pascal, C/C++, Java, C#
ex:FORTRAN
• Row-major (row by row)
• Column major (column by column)
x x+1 x+2 x+3
x+4 x+5 x+6 x+7
x+8 x+9 x+10 x+11
x+12 x+13 x+14 x+15
x x+4 x+8 x+12
x+1 x+5 x+9 x+13
x+2 x+6 x+10 x+14
x+3 X+7 x+11 x+15
0 1 2 3
1 2 3 4
0 1 2
1 2 3
16
Address polynomial• int a[r][c]
// 宣告一個 r(row 行 ) *c(column 列 ) 的二維陣列 1 ...... c
1 a[1][1] ...... a[1][c]2 a[2][1] ...... a[2][c]3 a[3][1] ...... a[3][c]
......
...... ...... ......
r a[r][1] ...... a[r][c]
17
Address polynomial• int a[r][c]
// 宣告一個 r(row 行 ) *c(column 列 ) 的二維陣列 1 ...... c
1 a[1][1] ...... a[1][c]2 a[2][1] ...... a[2][c]3 a[3][1] ...... a[3][c]
......
...... ...... ......
r a[r][1] ...... a[r][c]
我的位址是 x
18
Address polynomial• int a[r][c]
// 宣告一個 r(row 行 ) *c(column 列 ) 的二維陣列 1 ...... c
1 a[1][1] ...... a[1][c]2 a[2][1] ...... a[2][c]3 a[3][1] ...... a[3][m]
......
...... a[i][j] ......
r a[r][1] ...... a[r][c]
我的位址是 x
那我呢 ???
19
Address polynomial• int a[r][c]
// 宣告一個 r(row 行 ) *c(column 列 ) 的二維陣列 1 ...... c
1 a[1][1] ...... a[1][c]2 a[2][1] ...... a[2][c]3 a[3][1] ...... a[3][c]
......
...... a[i][j] ......
r a[r][1] ...... a[r][c]
x (row major order)
a[i][j] 的位址 : x+c*(i-1)+(j-1)
20
Address polynomial• int a[r][c]
// 宣告一個 r(row 行 ) *c(column 列 ) 的二維陣列 1 ...... c
1 a[1][1] ...... a[1][c]2 a[2][1] ...... a[2][c]3 a[3][1] ...... a[3][c]
......
...... a[i][j] ......
r a[r][1] ...... a[r][c]
x (row major order)
a[i][j] 的位址 : x+c*(i-1)+(j-1)
Address polynomial位址多項式
Example• int a[3][4]
1 2 3 4
1
2 target
3
x
Example• int a[3][4]
1 2 3 4
1
2 target
3
x
• Row-major order x+4*(2-1)+(3-1)=x+6
Example• int a[3][4]
1 2 3 4
1
2 target
3
x
• Row-major order x+4*(2-1)+(3-1)=x+6• Column-major order x+3*(3-1)+(2-1) =x+7
Figure 8.6• int Readings[4][5]; // 宣告一個可存 4*5 個整數的空間(陣
列)
•
A two-dimensional array with four rows and five columns stored in row major order
機器之記憶體
第 3 列第 4 行之值
概念性陣列
Figure 8.6• int Readings[4][5]; // 宣告一個可存 4*5 個整數的空間(陣
列)
•
A two-dimensional array with four rows and five columns stored in row major order
機器之記憶體
第 3 列第 4 行之值
概念性陣列x+1 x+2 x+3 x+4 x+5
x+6 x+7 x+8 x+9 x+10
x+11 x+12 x+13 x+14 x+15
x+16 x+17 x+18 x+19 x+20
位址
Heterogeneous arrays
• 異質陣列 每一項可能不同類型稱作文件
Heterogeneous arrays
• Components can be stored one after the other in a contiguous block
• Components can be stored in separate locationsidentified by pointers
它儲存在連續的空間 , 他也可以用指標分開來儲存在不同的位置,應用在 struct/class
Figure 8.7
• Storing the heterogeneous array Employee
a. 陣列儲存在連續區間
b. 陣列組件儲存在分開區間
Figure 8.7
• Storing the heterogeneous array Employee
a. 陣列儲存在連續區間
b. 陣列組件儲存在分開區間
個別項目通常被稱為元件( components )
Storing Lists• 儲存列表
例如儲存一串名字的 list 到電腦主記憶體之中
Contiguous list
連續串列• 優點:靜態時為便利的儲存結構 • 缺點:動態下極存不便
Figure 8.8•
Names stored in memory as a contiguous list
記憶單位連續區塊
第一名稱儲存區塊
linked list
鏈結串列• 簡單說:將各 list 的入口用 pointer 連結起來 • 儲存在分散的記憶單元裡
- 頭指標( head pointer ):指標指到串列的開端
- 空指標( NLT pointer ):最後一項指標中放NIL
linked list
鏈結串列• 簡單說:將各 list 的入口用 pointer 連結起來 • 儲存在分散的記憶單元裡
- 頭指標( head pointer ):指標指到串列的開端
- 空指標( NLT pointer ):最後一項指標中放NIL
NIL
Figure 8.9•
The structure of a linked list
頭指標
名稱 指標
空指標
• linked list 可說是一系列 node (節點)連結
• 每個節點至少包含– data ( 資料 )–指到下一個節點的指標
A
Head
B C
A
data pointer
node
Figure 8.10•
Deleting an entry from a linked list
頭指標
名稱 指標
舊指標
新指標 空指標
刪除的項目
• 將 A 節點的連結指標指向 C
BA C
BA C
Figure 8.11•
Inserting an entry into a linked list
頭指標
名稱 指標
舊指標
新指標 新指標
空指標
新增的項目
• 將 B 節點的連結指標指向 C ( node.next )
• 將 A ( node )節點的連結指標指向 B
A AC C
B B
• 將 B 節點的連結指標指向 C ( node.next )
• 將 A ( node )節點的連結指標指向 B
A AC C
B B
步驟不能交換 !!! Why?
Storing Stacks and Queues• 儲存堆疊及佇列
Stack
堆疊• FILO : ( First-In,Last-Out )先進後出
Stack
堆疊• FILO : ( First-In,Last-Out )先進後出
• 記憶體需保留些連續區塊以供成長及縮小• 頂端位置在保留記憶體區塊中前後移動,其單
元稱 stack pointer
Figure 8.12•
A stack in memory
堆積的基底
保留區塊的記憶單元
堆積項目
堆積指標
供成長的空間
Example
Example
Push 紅球
Example
Push 綠球
Example
Push 藍球
Example
Pop
Example
Example
Pop
Example
Example
Pop
Example
Queue
佇列• FIFO : ( First-In,First-Out ) 先進後
出
Queue
佇列• FIFO : ( First-In,First-Out ) 先進後出
• 以連續記憶體區塊做保留二單元做指標 ( head pointer 頭指標 & tail pointer
尾指標)
Figure 8.13
•A queue implementation with head and tail pointers. Note how the queue crawls through memory as entries are inserted and removed.
a. 空佇列 b. 插入項目 A,B,C 之後
c. 移除項目 A 插入 D 之後 d. 移除項目 B 插入 E 之後
缺點• queue 若不作控制,記憶體緩慢移動,且
經過地方所存的資料被摧毀 • 改善方法 環狀序列( circular queue )
Figure 8.14•
A circular queue containing the letters P through V
概念性的 queue 儲存結構(最後一個單元連到第一個單元)
實際的 queue 儲存結構
Figure 8.14•
A circular queue containing the letters P through V
概念性的 queue 儲存結構(最後一個單元連到第一個單元)
實際的 queue 儲存結構
A
Head
B C
Storing Binary Trees儲存二元樹
Storing Binary Trees儲存二元樹
Storing Binary Trees儲存二元樹
• 每個節點 = 資料 + 左右孩子指標
Left child pointer Right child pointer
Storing Binary Trees儲存二元樹
• 每個節點 = 資料 + 左右孩子指標• 通過根節點指標來做存取
Figure 8.15 The structure of a node in a binary tree
包含資料的記憶單元 左子指標 右子指標
Figure 8.16
•The conceptual and actual organization of a binary tree using a linked storage system
概念性的樹
實際的儲存結構
Figure 8.16
•The conceptual and actual organization of a binary tree using a linked storage system
概念性的樹
實際的儲存結構根節點
Figure 8.16
•
A
B C
D E F
NIL NIL NIL NIL NIL
NIL
Figure 8.17•
A tree stored without pointers
概念性的樹
實際的儲存結構
根節點樹第二層中的節點 樹第三層中的節點
Figure 8.18•
A sparse, unbalanced tree shown in its conceptual form and as it would be stored without pointers
概念性的樹
實際的儲存結構
根 第二層 第三層 第四層
Figure 8.18•
A sparse, unbalanced tree shown in its conceptual form and as it would be stored without pointers
概念性的樹
實際的儲存結構
根 第二層 第三層 第四層
太浪費記憶體空間了 !!!
靜態 vs 動態結構• 靜態結構比動態結構易於管理,
且可直接存取(動態須由指標一個指一個)
• 靜態加入、刪除及合併時,必須做大量資料的移動;動態則只須要改變指標即可
Manipulating Data Structures資料結構的操作
• 藉由事先已定義的程序( procedure )來完成資料結構的操作
• Example : stack 堆疊最少需要 push (堆入)和 pop (移除)兩個 procedures 來完成操作
Figure 8.19•
A procedure for printing a linked list
Figure 8.19•
A procedure for printing a linked list
可以將鏈結串列的名單列出
The end