9
1 11.1 Chapter 11 Data Structure Source: Foundations of Computer Science Cengage Learning 11.2 Define a data structure. 資料結構表示一組資料及其關聯性 Define an array as a data structure and how it is used to store a list of data items. 陣列的資料結構用來儲存一列資料 Distinguish between the name of an array and the names of the elements in an array. Describe operations defined for an array. 陣列的資料結構的運算 Define a record as a data structure and how it is used to store attributes belonging to a single data element. 紀錄的資料結構用來儲存資料屬性 Distinguish between the name of a record and the names of its fields. Define a linked list as a data structure and how it is implemented using pointers. 鏈結的資料結構及其指標的運用 Define the applications of arrays, records, and linked lists. 陣列、紀錄、鏈結 等資料結構的應用 Objectives 學習目標 After studying this chapter, students should be able to:

Data Structure - 南華大學chun/R-CS-chap11-Data Structure.pdf · 陣列的資料結構的運算 Define a record as a data structure and how it is used to store attributes belonging

  • Upload
    others

  • View
    0

  • Download
    0

Embed Size (px)

Citation preview

Page 1: Data Structure - 南華大學chun/R-CS-chap11-Data Structure.pdf · 陣列的資料結構的運算 Define a record as a data structure and how it is used to store attributes belonging

1

11.1

Chapter 11

Data

Structure Source: Foundations of Computer Science Cengage Learning

11.2

Define a data structure. 資料結構表示一組資料及其關聯性

Define an array as a data structure and how it is used to store a list of data

items. 陣列的資料結構用來儲存一列資料

Distinguish between the name of an array and the names of the elements in an

array.

Describe operations defined for an array. 陣列的資料結構的運算

Define a record as a data structure and how it is used to store attributes

belonging to a single data element. 紀錄的資料結構用來儲存資料屬性

Distinguish between the name of a record and the names of its fields.

Define a linked list as a data structure and how it is implemented using

pointers. 鏈結的資料結構及其指標的運用

Define the applications of arrays, records, and linked lists. 陣列、紀錄、鏈結等資料結構的應用

Objectives 學習目標 After studying this chapter, students should be able to:

Page 2: Data Structure - 南華大學chun/R-CS-chap11-Data Structure.pdf · 陣列的資料結構的運算 Define a record as a data structure and how it is used to store attributes belonging

2

11.3

Array name versus element name 陣列名稱 對 元素名稱

The name of the array is the name of the whole structure

陣列名稱代表其資料結構, while the name of an element

allows us to refer to that element. The name of the array陣列名稱 is scores and name of each element is the name of

the array followed by the index陣列元素索引, for example,

scores[1], scores[2], and so on. In this chapter, we mostly

need the names of the elements, but in some languages,

such as C, we also need to use the name of the array. 如一維陣列(one dimensional array)

int scores[100] = {40,55, 39, ..., 89, 22, 76, 85}

11.4

Multi-dimensional arrays 多維陣列

The arrays discussed so far are known as one-dimensional

arrays because the data is organized linearly in only one

direction. Many applications require that data be stored in

more than one dimension, called a two-dimensional (2D)

array. 二維陣列.

Memory layout 二維陣列資料存在記憶體的方式

The indexes in a one-dimensional array directly define the

relative positions of the element in actual memory. A two-

dimensional array how it is stored in memory using row-

major or column-major storage. Row-major storage is

more common 列優先的儲存較普及.

Page 3: Data Structure - 南華大學chun/R-CS-chap11-Data Structure.pdf · 陣列的資料結構的運算 Define a record as a data structure and how it is used to store attributes belonging

3

11.5

Example

We have stored the two-dimensional array (row-major) students in

memory. The array is 100 × 4 (100 rows and 4 columns). Show the

address of the element students[5][3] assuming that the element

student[1][1] is stored in the memory location with address 1000 and

each element occupies only one memory location.

已知100列x4行的陣列資料,左上角第一個位址為

1000,求第5列第3行的陣列資料之位址

Solution

We can use the following formula to find

the location of an element.

If the first element occupies the location 1000, the target

element occupies the location 1018.

y = 1000 + 4 x (5-1) + (3-1) = 1018

2 4 1

1

. . . . . . … . . .

100

5

Row

m=100

Column n=4

3

. . .

2

3

y

4

11.6

Operations on array 陣列資料結構的運算

There are some operations that we can define on an array as

a data structure. The common operations on arrays as

structures are searching, insertion, deletion, retrieval and

traversal.陣列一般運算有五種:搜尋、插入、刪除、檢查、追蹤等一筆資料

Retrieval: Randomly access an element for the inspected

purpose.

Traversal: traversal all elements of the array for a purpose.

Although searching, retrieval and traversal of an array is

an easy job 容易 , insertion and deletion are time

consuming費時. The elements need to be shifted down

before insertion and shifted up after deletion.

Page 4: Data Structure - 南華大學chun/R-CS-chap11-Data Structure.pdf · 陣列的資料結構的運算 Define a record as a data structure and how it is used to store attributes belonging

4

11.7

Insert / Delete for 1D-array A[21]

3 4 5 6 7 8 9 10 1 2

22 97 34 35 82 46 52 41 79 11

13 14 15 16 17 18 19 20 11 12

51 76 80 25 88 14 5 9 64 68

0

33

1 3 4 0

79 97 33 0 22

64 76 41 2 51

25 5 80 4 14

… 9 5

… 6

Row

m=7

Column n=5

2

11

68

88

35 52 34 1 46 82

Deletion_array (A[n],64)

{ for (i=0 to n-1)

{ if (A[i]==64) k = i; }

for (i=k to n-2)

{ A[i] = A[i+1]; }

n = n -1; update A[n];

}

Insertion_array (A[n],x,55)

{ n = n+1; update A[n];

for (i=n-2 to x by -1)

{ A[i+1] = A[i]); }

A[x] = 55;

}

Insert / Delete 2D-array B[7][5]

11.8

RECORDS 紀錄之資料結構

A record is a collection of related elements, possibly

of different types, having a single name. 陣列為單元素之單一資料型態;而紀錄可為多元素之異同的資料型態. Each element in a record is called a field. 每個元素稱為欄位 A field is the smallest element of named

data that has meaning. A field has a type and exists in

memory. Fields can be assigned values, which in turn

can be accessed for selection or manipulation. A field

differs from a variable primarily in that it is part of a

record.

Page 5: Data Structure - 南華大學chun/R-CS-chap11-Data Structure.pdf · 陣列的資料結構的運算 Define a record as a data structure and how it is used to store attributes belonging

5

11.9

Comparison of records and arrays

紀錄 與 陣列資料結構之比較

An array defines a combination of elements, while a

record defines the identifiable parts of an element. 陣列是定義相同元素之組合;紀錄是定義元素之不同識別.

For example, an array can define a class of students (40

students), but a record defines different attributes of a

student, such as id, name or grade. 如陣列是定義一班40位學生;紀錄是定義每個學生的屬性: 學號、姓名、年級等不同識別. struct node

{ int id;

string name;

char grade;

} ;

struct node student[30];

int student[30];

11.10

LINKED LISTS 鏈結列之資料結構

A linked list is a collection of data in which each element

contains the location of the next element — that is, each

element contains two parts: data and link.每個元素包含兩部分:資料與鏈結. The name of the list is the same as the

name of this pointer variable.

typedef struct node

{ int data;

struct node *link;

} NODE;

Page 6: Data Structure - 南華大學chun/R-CS-chap11-Data Structure.pdf · 陣列的資料結構的運算 Define a record as a data structure and how it is used to store attributes belonging

6

11.11

Memory Storage of Array versus linked list

66

20

72

21

74

22

85

23

… …

72

10

38

11

66

22

10

23

… … … 96

30

Null

31

… 74

38

50

39

… 85

50

30

51

Head

66 10 72 38 74 50 85 30 96 Null

Head

pre (*pre).data = 72

(*pre).link = 38

cur (*cur).data = 74

(*cur).link = 50

cur = 38

(*pre).link = cur = 38

96

24

鍊結列

陣列

11.12

Operations on linked lists 鍊結列的各種運算

Searching a linked list 搜尋一個鍊結列

Since nodes in a linked list have no names, we use two

pointers, pre (for previous) and cur (for current). At the

beginning of the search, the pre pointer is null and the cur

pointer points to the first node. The search algorithm moves

the two pointers together towards the end of the list.

Inserting a node 插入一個節點到鍊結列

❑ Inserting into an empty list. 插入一個節點 到空的鍊結列

❑ Insertion at the beginning of the list. 插入一個節點到鍊結列的前端

❑ Insertion at the end of the list. 插入一個節點到鍊結列的尾端

❑ Insertion in the middle of the list. 插入一個節點到鍊結列的中間

Page 7: Data Structure - 南華大學chun/R-CS-chap11-Data Structure.pdf · 陣列的資料結構的運算 Define a record as a data structure and how it is used to store attributes belonging

7

11.13

Before deleting a node in a linked list, we apply the

search algorithm. 刪除一個節點前, 我們必須先用搜尋找到這個節點. However, deletion is simpler than insertion: we

have only two cases 僅有兩種情況— deleting the first

node刪除第一個節點 and deleting any other node刪除任一節點. In other words, the deletion of the last and the

middle nodes can be done by the same process.

Deleting a node 從鍊結列 刪除一個節點

11.14

We start traversing by setting the walking pointer to the

first node in the list. Then, using a loop, we continue until

all of the data has been processed. 追蹤為從鍊結列的前端 到 尾端, 可採用迴圈方式. Each iteration of the loop

processes the current node, then advances the walking

pointer to the next node. When the last node has been

processed, the walking pointer becomes null and the loop

terminates.

Traveling a Linked List 追蹤整個鍊結列

Applications of linked lists 鍊結列的應用

A linked list is a very efficient data structure for sorted

list that will go through many insertions and deletions. 鍊結列對插入或刪除一筆資料最有效.

Page 8: Data Structure - 南華大學chun/R-CS-chap11-Data Structure.pdf · 陣列的資料結構的運算 Define a record as a data structure and how it is used to store attributes belonging

8

11.15

null head 76 32 49

Traversal each node from a Linked List 追蹤整個鍊結列

Traversal_list (head)

{ cur = head;

while (cur != NULL)

{ printout cur->data;

cur = cur->next;

}

}

typedef struct node

{ int data;

struct node *next;

} NODE;

11.16

Insertion a node at the head from a Linked List

插入一筆資料到鍊結列的前端

Insertion_list (head, new)

{ new->next = head;

head = new;

}

typedef struct node

{ int data;

struct node *next;

} NODE;

null head 76 32 49

87 new

Page 9: Data Structure - 南華大學chun/R-CS-chap11-Data Structure.pdf · 陣列的資料結構的運算 Define a record as a data structure and how it is used to store attributes belonging

9

11.17

null head 76 32 49

Deletion a node from a Linked List

從鍊結列中 刪除一筆資料

Deletion_list (head, 49)

{ cur = head;

while (cur->data != 49)

{ pre = cur;

cur = cur->next;

}

pre->next = cur->next;

}

Review Questions

Please define a 1D Array and a 1D array of records in C.

Please show the algorithm of finding the average score

from 2D-array data.

Please show the traversal algorithm for a linked list

Please show the algorithm of inserting a node into the

middle of a linked list.

Please show the algorithm of deleting a node from the

middle of a linked list.

Please compare their number of processing times for

inserting/deleting a number from a 1D array and a linked

list.

11.18