CH2-1 - Upload-2023 03 16
CH2-1 - Upload-2023 03 16
資料結構
Chih-Wei Lin(林志瑋)
Email:[email protected]
Electrical Engineering, College of Electrical Engineering and Computer Science
National Taiwan Ocean University
Data Structures
Arrays and Structures
Chih-Wei Lin(林志瑋)
Email:[email protected]
Electrical Engineering, College of Electrical Engineering and Computer Science
National Taiwan Ocean University
Chapter 2 Arrays and Structures
• Definition of Array
• a set of pairs,
• Arrays
• Array:
• a set of pairs,
• data structure
• For each index, there is a value associated with that index.
• representation (possible)
• Implemented by using consecutive memory.
陣列名稱 A
陣列註標 0 1 2 3 4
陣列元素 A[0] A[1] A[2] A[3] A[4]
7
D i f f e re n c e b e t w e e n v a r i a b l e
declaration and array declaration
variable declaration array declaration
int A, B, C; int A[3];
主記憶體
變數1: A
A陣列
A[0] A[1] A[2]
變數2: B
變數3: C
Advantages Disadvantages
• Deletion and insertion cause
• Ease of use
frequent data movement
• Use index value to quickly
• Waste unnecessary memory
access data
• A large amount of data can be • Array length is constant, may
processed at one time not be enough
• Easier to express data
processing skills
9
Storage of array
• Add "label" or "index" directly after the array name
• [Example]: Declare an array of A[3] and store 10, 20, 30 respectively,
as shown below:
int A[3];
A[0]=10; //指把10指定給A陣列中的第0項的資料中
A[1]=20;
A[2]=30;
A陣列 10 20 30
10
Example
• Please input the grades of six students into the array in sequence, and
calculate and output the sum
成績
1 60
2 70
3 80
4 85
5 90
6 100
11
Array, but not using the for loop
algorithm
main()
{
//宣告
int A[6]={60,70,80,85,90,100};
int sum;
//處理
sum = A[0] + A[1] + A[2] + A[3] + A[4] + A[5];
//輸出
printf(“總和為:%3d",sum);
printf("\n");
system("PAUSE");
return(0);
}
12
Array, using the for loop algorithm
(best)
main()
{
//宣告
int A[6]={60,70,80,85,90,100};
int i,sum=0;
//處理
for (i = 0; i<=5;i++ )
sum+=A[i];
//輸出
printf(“總和為:%3d",sum);
printf("\n");
system("PAUSE");
return(0);
}
13
Five commonly used operation
instructions for one-dimensional
arrays
• Read
• Write
• Insert
• Delete
• Copy
14
Read
• [Definition] Use Index to "read" data
• [Example] Put the second element of the A array into the X
variable
• [Writing] X = A[1]; //The annotation of the array starts from
0
A陣列 10 20 30
X
20
15
Write
• [Definition] Use index to "write" data
• [Example] Write the value 50 to the second index position of
the A array
• [Writing] A[1] = 50; //The annotation of the array starts from
0 寫入前:
A陣列 10 20 30
寫入後: 寫入50到A[1]
A陣列 10 20->50 30
16
50
Insert(1/2)
• [Definition] Insert a new element at the position i, the original label i
and its following elements must be moved back one position
• [Example] A new element will be inserted at the position marked 1
(15)
• [algorithm]
17
Insert(2/2)
• [Illustration]
差入前: A[1]的內容為20
A陣列 10 20 30
差入後: 插入15到A[1]
3
A陣列 10 15 20
18
Delete(1/2)
• [Definition] means to delete the specified element marked i.
The original element marked i is deleted. In order to avoid
wasting memory space, all subsequent elements must be
moved forward by one position
• [Example] delete an old element at position 1 (20)
• [algorithm]
19
Delete(2/2)
• [Illustration]
刪除前: A[1]的內容為20
A陣列 10 20 30
刪除後:
1
A陣列 10 20 30
2
刪除後: 插入15到A[1]
A陣列 10 30 3 0
20
Copy(1/2)
• [Definition] refers to copying all the values in the elements of
the source array to the destination array one by one
• [Example] Copy all the values in the elements of array A to
array B one by one
• [algorithm]
21
Copy(2/2)
• [Illustration]
來源陣列:
A陣列 10 20 30
複製(Copy)
目的陣列:
B陣列 10 20 30
22
2.1 The array as an ADT (2/6)
• Arrays in C (cont’d)
• Compare int *list1 and int list2[5] in C.
Same: list1 and list2 are pointers.
Difference: list2 reserves five locations.
• Notations:
list2 - a pointer to list2[0]
(list2 + i) - a pointer to list2[i] (&list2[i])
*(list2 + i) - list2[i]
2.1 The array as an ADT (5/6)
• Arrays in C (cont’d)
程序 2.1程序舉例
2.1 The array (6/6)
• Example:
1-dimension array addressing
• int one[] = {0, 1, 2, 3, 4};
• Goal: print out address and value
• void print1(int *ptr, int rows) {
/* print out a one-dimensional array using a pointer */
int i;
printf(“Address Contents\n”);
for (i=0; i < rows; i++)
printf(“%8u%5d\n”, ptr+i, *(ptr+i) );
printf(“\n”);
}
results
2.2 Dynamic storage allocation for
arrays
• 2.2.1 1-dimension array
程序1.4選擇排序
2.2 Dynamic storage allocation for
arrays
• 2.2.1 1-dimension array
程序1.4選擇排序
2.2 Dynamic storage allocation for
arrays
• 2.2.1 2-dimension array
• Declaration of two-dimensional array
• Storage structure
"Array of Arrays"
2.2 Dynamic storage allocation for
arrays
• 2.2.1 2-dimension array
• Constructs a two-dimensional array
2.2 Dynamic storage allocation for
arrays
• 2.2.1 2-dimension array
• Constructs a two-dimensional array
• the following statement for the usage of this program
2.2 Dynamic storage allocation for
arrays
• 2.2.1 2-dimension array
• Constructs a two-dimensional array
• The other two memory allocation functions
• calloc
• realloc
2.2 Dynamic storage allocation for
arrays
• 2.2.1 2-dimension array
• calloc
2.2 Dynamic storage allocation for
arrays
• 2.2.1 2-dimension array
• CALLOC macro definition
2.2 Dynamic storage allocation for
arrays
• 2.2.1 2-dimension array
• realloc
• realloc function can resize the storage space allocated by malloc
or calloc.
• instruction
2.2 Dynamic storage allocation for
arrays
• 2.2.1 2-dimension array
• REALLOC macro definition
N-dimensional Array
• 1D array
• 2D array
42
1D array
• If the A array has N elements, the starting address of
the array is L0, and the index value starts from 0, and
d is the element size, what is the starting position of
A[i]? A array 0 1 2 … i N-1
d d
L0 L0+2d L0+i*d
43
Exercise
44
Index starts from L
• If the index of the A array is from L to U, the
starting address of the array is L0, and d is the
element size, what is the starting position of A[i]?
L L+1 L+2 … i U
A陣列
d d
45
2D array
47
Sequential representation and
implementation
0 A[0, 0]
1 A[0, 1]
.
2 .
.
3
A[0, N-1]
.
.
. A[1, 0]
.
.
. A[1, 1]
.
. .
. .
.
M-1
A[M-1, N-1]
邏輯位置 實體位置
由上而下一列一列讀入一維陣列 50
Row-major
0 1 2 ……. N-1
0 • 令L0為起始位置
• d為元素大小
1
i row 則A[i, j]的位置 = L0 + [i * N + j] * d
2
Δ的位置為:
3
Δ A[3, 2] = L0 + [3 * N + 2] * d
A[i, j]
.
.
.
j column
.
.
.
.
.
.
M-1
51
Sequential representation and
implementation
0 A[0, 0]
1 A[1, 0]
.
2 .
.
3
A[M-1,0]
.
.
. A[0, 1]
.
.
. A[1, 1]
.
. .
. .
.
M-1
A[M-1, N-1]
邏輯位置 實體位置
由上而下一列一列讀入一維陣列 53
Column-major
0 1 2 ……. N-1
0 • 令L0為起始位置
• d為元素大小
1
i row 則A[i, j]的位置 = L0 + [j * M + i] * d
2
Δ的位置為:
3
Δ A[3, 2] = L0 + [2 * M + 3] * d
A[i, j]
.
.
.
.
.
.
.
.
.
M-1
54
j column
… …
a11 a12 … a1n
S ea21
q ua22e n…t iaa2n l r e p r eaa11s12 e n t第a t i o naa1121 a n第d
A=implementation
…………… 1 1
… 行 … 列
am1 am2 … amn a1n am1
(a) Representation of a
a21 a12
two-dimensional array a22 第 a22 第
2 2
… 行 … 列
a2n am2
┆ ┆ ┆ ┆
am1 a1m
am2 第 a2m 第
m n
… 行 … 列
Amn amn
Figure Two-dimensional array and its
sequential storage legend
… …
(b) row-major (c) Column-major
Sequential representation and
implementation
• Assuming a two-dimensional array A=(aij)m´n, if the number of
storage units occupied by each element is l (units). LOC[a11]
represents the first address of element a11 (the first address of
the array)
• 1. Store in "row-major order"
• (1) The (first) address corresponding to each element in row 1 is:
• LOC[a1j]=LOC[a11]+(j-1)´l j=1,2, …,n
• (2) The (first) address corresponding to each element in row 2 is:
• LOC[a2j]=LOC[a11]+n´l +(j-1)´l j=1,2, …,n
………
• (3) The (first) address corresponding to each element in row m is:
• LOC[amj]=LOC[a11]+(m-1)´n´l +(j-1)´l j=1,2, …,n
• It can be seen from this that the (first) address of any element aij in the two-
dimensional array is:
• LOC[aij]=LOC[a11]+[(i-1)´n +(j-1)]´l i=1,2, …,m j=1,2, …,n
Sequential representation and
implementation
• Assuming a two-dimensional array A=(aij)m´n, if the number of
storage units occupied by each element is l (units). LOC[a11]
represents the first address of element a11 (the first address of
the array)
• 2. Store in "column -major order"
• (1) The (first) address corresponding to each element in column 1 is:
• LOC[aj1]=LOC[a11]+(j-1)´l j=1,2, …,m
• (2) The (first) address corresponding to each element in column 2 is:
• LOC[aj2]=LOC[a11]+m´l +(j-1)´l j=1,2, …,m
………
• (3) The (first) address corresponding to each element in row m is:
• LOC[ajn]=LOC[a11]+ (n-1)´m´l +(j-1)´l j=1,2, …,m
• It can be seen from this that the (first) address of any element aij in the two-
dimensional array is:
• LOC[aij]=LOC[a11]+[(i-1)´m+(j-1)]´l i=1,2, …,m j=1,2, …,n
Sequential representation and
implementation
• Assuming a two-dimensional array A=(aij)m´n, if the number of
storage units occupied by each element is l (units). LOC[a11]
represents the first address of element a11 (the first address of
the array)
• For three-dimensional arrays A=(aijk)m´n´p, if the number of
storage units occupied by each element is l (units),
LOC[a111] represents the first address of element a111, that is,
the first address of the array.
• To store in "row-major order".
• The address of any element aijk in the three-dimensional array is:
• LOC(aijk)=LOC[a111]+[(i-1)´n´p+(j-1)´p+(k-1)]´l
Sequential representation and
implementation
• Assuming a two-dimensional array A=(aij)m´n, if the number of
storage units occupied by each element is l (units). LOC[a11]
represents the first address of element a11 (the first address of
the array)
• For n-dimensional arrays A=(aj1j2…jn) , if the number of
storage units occupied by each element is l (units), LOC[a11
…1] represents the first address of element a11 …1, that is, the
first address of the array.
• To store in "row-major order".
• The address of any element aj1j2…jn in the n-dimensional array is:
• LOC[aj1j2…jn]=LOC[a11 …1]+[(b2´…´bn)´(j1-1)
+ (b3´…´bn)´(j2-1)+ …
+ bn´(jn-1-1)+ (jn-1)] ´l
2.3 Structures and Unions (1/6)
程序2.4检查两个结构相等的函数
• persion1 = perosn2
2.3 Structures and Unions (2/6)
char name[10]
Memory Layout
human_being
int age
date int month
float salary
int day
int month
int year date
int day
int year
• A person born on February 11, 1994, would have have values for the date
struct set as
2.3 Structures and Unions (4/6)
• 2.3.2 Unions
• A union declaration is similar to a structure.
• The fields of a union must share their memory space.
• Only one field of the union is “active” at any given time
Memory Layout
sex_type
enum tag_field
u int children int beard
person1.sex_info.sex = male;
person1.sex_info.u.beard = FALSE;
and
person2.sex_info.sex = female;
person2.sex_info.u.children = 4;
2.3 Structures and Unions (5/6)