770 likes | 851 Views
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 個儲存空間,並且 編號是連續 的。.
E N D
Implementing Data Structures 資料結構實做
Storing Arrays • 儲存陣列 有點類似「表格」概念,每一格儲存我們要的資訊
Homogeneous Arrays • 同質陣列 長方形的資料區塊每一項有相同性質
Homogeneous Arrays • Row-major order versus column major order • Address polynomial for each of themis continuous 由列與行來儲存的,例如:4列*4行= 16個儲存空間,並且編號是連續的。
Figure 8.5 The array of temperature readings stored in memory starting at address x • int Readings[24]; // 宣告一個可存24個整數的空間(陣列) 位址 記憶單位
Figure 8.5 The array of temperature readings stored in memory starting at address x • int Readings[24]; // 宣告一個可存24個整數的空間(陣列) • Readings[4]67; 位址 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)個整數的空間(陣列)
ex:Pascal, C/C++, Java, C# ex:FORTRAN • Row-major (row by row) • Column major (column by column)個整數的空間(陣列)
ex:Pascal, C/C++, Java, C# ex:FORTRAN 0 1 2 3 • Row-major (row by row) • Column major (column by column) 0 1 2 1 2 3 4 1 2 3
Address polynomial • int a[r][c] • //宣告一個r(row行) *c(column列) 的二維陣列
Address polynomial • int a[r][c] • //宣告一個r(row行) *c(column列) 的二維陣列 我的位址是x
Address polynomial • int a[r][c] • //宣告一個r(row行) *c(column列) 的二維陣列 我的位址是x 那我呢???
Address polynomial • int a[r][c] • //宣告一個r(row行) *c(column列) 的二維陣列 (row major order) a[i][j]的位址: x+c*(i-1)+(j-1) x
Address polynomial • int a[r][c] • //宣告一個r(row行) *c(column列) 的二維陣列 (row major order) a[i][j]的位址: x+c*(i-1)+(j-1) x • Address polynomial • 位址多項式
Example • int a[3][4] x
Example • int a[3][4] • Row-major order • x+4*(2-1)+(3-1)=x+6 x
Example • int a[3][4] • Row-major order • x+4*(2-1)+(3-1)=x+6 • Column-major order • x+3*(3-1)+(2-1) =x+7 x
Figure 8.6 A two-dimensional array with four rows and five columns stored inrow major order • int Readings[4][5];// 宣告一個可存4*5個整數的空間(陣列) 概念性陣列 機器之記憶體 第3列第4行之值
Figure 8.6 A two-dimensional array with four rows and five columns stored inrow major order • int Readings[4][5];// 宣告一個可存4*5個整數的空間(陣列) 概念性陣列 位址 機器之記憶體 第3列第4行之值
Heterogeneous arrays • 異質陣列 每一項可能不同類型稱作文件
Heterogeneous arrays • Components can be stored one after the other in acontiguous 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.陣列儲存在連續區間 個別項目通常被稱為元件(components) b.陣列組件儲存在分開區間
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 頭指標 名稱 指標 空指標
A B C A Head • linked list 可說是一系列node(節點)連結 • 每個節點至少包含 • data (資料) • 指到下一個節點的指標 node data pointer
Figure 8.10 Deleting an entry from a linked list 頭指標 刪除的項目 舊指標 名稱 指標 新指標 空指標
A B C • 將 A 節點的連結指標指向 C A B C
Figure 8.11 Inserting an entry into a linked list 頭指標 新增的項目 新指標 新指標 名稱 指標 舊指標 空指標
B B • 將 B 節點的連結指標指向 C (node.next) • 將 A (node)節點的連結指標指向 B C A C A
B B • 將 B 節點的連結指標指向 C (node.next) • 將 A (node)節點的連結指標指向 B C A C A 步驟不能交換!!!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 Push 紅球
Example Push 綠球
Example Push 藍球
Example Pop