数组抽象数据类型 数据集合:{a,a,;a2yan1}每个数据类型为抽象数据元素类型 操作集合:(1)求数组元素个数 ArrayLength(D) (2)存数组元素 Storage(D,,x) (3)取数组元素Get(Di1x)
6 三、数组抽象数据类型 数据集合:{a0,a1,a2,…an-1 } 每个数据类型为抽象数据元素类型 操作集合:(1)求数组元素个数 ArrayLength(D) (2)存数组元素 Storage(D,i,x) (3)取数组元素 Get(D,i,x)
5.2动态数组 动态数组的设计方法 1、动态数组的概念 动态数组:它是在需要存储单元空间时才给出数组的具体个 数 C语言提供的支持函数有 malloc()、 calloc(、 freed 例:编写一个定义和使用二维3行4列动态数组的一个完整程序
7 5.2 动态数组 一、动态数组的设计方法 1、动态数组的概念 动态数组:它是在需要存储单元空间时才给出数组的具体个 数。 C语言提供的支持函数有: malloc( )、calloc( )、free( ) 例:编写一个定义和使用二维3行4列动态数组的一个完整程序
#inc lude <malloc. h> #inc lude <stdio. h> #include <stdlib.h> vold main(vo1 d t int 1, j, row=3, co1=4, **a a=(int*米)ma110c(r0W米 sizeof(in米); for(i=0;i<row; i++) ali]=(int *)malloc(col*sizeof (int)) for(i=0;i<row i++) for(j=0; j<col; j++) a printf( %d ali]lil); printf(“\n”);}
8 #include <malloc.h> #include <stdio.h> #include <stdlib.h> void main(void) { int i,j,row=3,col=4,**a; a=(int **) malloc(row*sizeof(int *)); for (i=0;i<row;i++) a[i]=(int *)malloc(col*sizeof(int)); for(i=0;i<row;i++) { for(j=0;j<col;j++) { a[i][j]=i+j; printf("%d ",a[i][j]); } printf(“\n”);} }