1 / 77

Implementing Data Structures

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 個儲存空間,並且 編號是連續 的。.

Download Presentation

Implementing Data Structures

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. Implementing Data Structures 資料結構實做

  2. Storing Arrays • 儲存陣列 有點類似「表格」概念,每一格儲存我們要的資訊

  3. Homogeneous Arrays • 同質陣列 長方形的資料區塊每一項有相同性質

  4. Homogeneous Arrays • Row-major order versus column major order • Address polynomial for each of themis continuous 由列與行來儲存的,例如:4列*4行= 16個儲存空間,並且編號是連續的。

  5. Figure 8.5 The array of temperature readings stored in memory starting at address x • int Readings[24]; // 宣告一個可存24個整數的空間(陣列) 位址 記憶單位

  6. Figure 8.5 The array of temperature readings stored in memory starting at address x • int Readings[24]; // 宣告一個可存24個整數的空間(陣列) • Readings[4]67; 位址 67 記憶單位

  7. 多維陣列

  8. 多維陣列 • int a[5]; 一維陣列

  9. 多維陣列 • int a[5]; 一維陣列 • int b[5][5]; 二維陣列

  10. 多維陣列 • int a[5]; 一維陣列 • int b[5][5]; 二維陣列 • int c[5][5][5]; 三維陣列

  11. 多維陣列 • 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]; • ...... 那這些多維的你能想像嗎! ?

  12. Row-major (row by row) • Column major (column by column)個整數的空間(陣列)

  13. Row-major (row by row) • Column major (column by column)個整數的空間(陣列)

  14. ex:Pascal, C/C++, Java, C# ex:FORTRAN • Row-major (row by row) • Column major (column by column)個整數的空間(陣列)

  15. 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

  16. Address polynomial • int a[r][c] • //宣告一個r(row行) *c(column列) 的二維陣列

  17. Address polynomial • int a[r][c] • //宣告一個r(row行) *c(column列) 的二維陣列 我的位址是x

  18. Address polynomial • int a[r][c] • //宣告一個r(row行) *c(column列) 的二維陣列 我的位址是x 那我呢???

  19. Address polynomial • int a[r][c] • //宣告一個r(row行) *c(column列) 的二維陣列 (row major order) a[i][j]的位址:  x+c*(i-1)+(j-1) x

  20. 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 • 位址多項式

  21. Example • int a[3][4] x

  22. Example • int a[3][4] • Row-major order • x+4*(2-1)+(3-1)=x+6 x

  23. 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

  24. 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行之值

  25. 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行之值

  26. Heterogeneous arrays • 異質陣列 每一項可能不同類型稱作文件

  27. Heterogeneous arrays • Components can be stored one after the other in acontiguous block • Components can be stored in separate locationsidentified by pointers 它儲存在連續的空間,他也可以用指標分開來儲存在不同的位置,應用在struct/class

  28. Figure 8.7 Storing the heterogeneous array Employee a.陣列儲存在連續區間 b.陣列組件儲存在分開區間

  29. Figure 8.7 Storing the heterogeneous array Employee a.陣列儲存在連續區間 個別項目通常被稱為元件(components) b.陣列組件儲存在分開區間

  30. Storing Lists • 儲存列表 例如儲存一串名字的list到電腦主記憶體之中

  31. Contiguous list 連續串列 • 優點:靜態時為便利的儲存結構 • 缺點:動態下極存不便

  32. Figure 8.8 Names stored in memory as a contiguous list 記憶單位連續區塊 第一名稱儲存區塊

  33. linked list 鏈結串列 • 簡單說:將各list的入口用pointer連結起來  • 儲存在分散的記憶單元裡 - 頭指標(head pointer):指標指到串列的開端 - 空指標(NLT pointer):最後一項指標中放NIL

  34. linked list 鏈結串列 • 簡單說:將各list的入口用pointer連結起來  • 儲存在分散的記憶單元裡 - 頭指標(head pointer):指標指到串列的開端 - 空指標(NLT pointer):最後一項指標中放NIL NIL

  35. Figure 8.9 The structure of a linked list 頭指標 名稱 指標 空指標

  36. A B C A  Head • linked list 可說是一系列node(節點)連結 • 每個節點至少包含 • data (資料) • 指到下一個節點的指標 node data pointer

  37. Figure 8.10 Deleting an entry from a linked list 頭指標 刪除的項目 舊指標 名稱 指標 新指標 空指標

  38. A B C • 將 A 節點的連結指標指向 C A B C

  39. Figure 8.11 Inserting an entry into a linked list 頭指標 新增的項目 新指標 新指標 名稱 指標 舊指標 空指標

  40. B B • 將 B 節點的連結指標指向 C (node.next) • 將 A (node)節點的連結指標指向 B C A C A

  41. B B • 將 B 節點的連結指標指向 C (node.next) • 將 A (node)節點的連結指標指向 B C A C A 步驟不能交換!!!Why?

  42. Storing Stacks and Queues • 儲存堆疊及佇列

  43. Stack 堆疊 • FILO :(First-In,Last-Out)先進後出

  44. Stack 堆疊 • FILO :(First-In,Last-Out)先進後出 • 記憶體需保留些連續區塊以供成長及縮小 • 頂端位置在保留記憶體區塊中前後移動,其單元稱 stack pointer

  45. Figure 8.12 A stack in memory 保留區塊的記憶單元 堆積的基底 堆積項目 供成長的空間 堆積指標

  46. Example

  47. Example Push 紅球

  48. Example Push 綠球

  49. Example Push 藍球

  50. Example Pop

More Related