[计算机软件及应用]第5章数据结构数组和广义表.pptVIP

  • 1
  • 0
  • 约1.41万字
  • 约 53页
  • 2018-03-04 发布于浙江
  • 举报

[计算机软件及应用]第5章数据结构数组和广义表.ppt

[计算机软件及应用]第5章数据结构数组和广义表

1、创建数组InitArray Status InitArray(Array A, int dim, …) { if (dim1 || dim MAX_ARRAY_DIM) return ERROR; A.dim=dim; A.bounds=(int *) malloc (dim * sizeof(int)); //创建bounds数组,存放各维大小。 if (!A.bounds) exit (OVERFLOW) //将各维长度存入A.bounds数组,并计算数组A的元素总数elemtotal elemtotal=1; va_start(ap, dim); //将dim参数后的各维长度参数存入ap数组 for (i=0; idim; ++i) { A.bounds[i] = va_arg(ap, int); //将ap中的参数依次取出,并转换成int类型 if (A.bounds[i] 0) return UNDERFLOW; elemtotal *= A.bounds[i]; //计算数组A的元素总数,用于申请数组A的空间 } va_end(ap); //释放ap数组 1、创建数组InitArray (续) //申请A数组空间 A.base =(ElemType *) malloc (elemtotal * sizeof(ElemType)); if (!A.base) exit (OVERFLOW); //计算映像函数的参数ci,并存入A.constants数组 A.constants = (int *) malloc (dim * sizeof(int)); if (!A.constants) exit (OVERFLOW); A.constants[dim-1] = 1; // L=1, 指针的增减以元素的大小为单位 for ( i = dim-2 ; i=0; --i) A.constants[i] = A.bounds[i+1] * A.constants[i+1]; return OK; } 2、数组元素定位Locate Status Locate(Array A, va_list ap, int off) { //若ap指示的各下标值合法,则求出该元素在数组A中的相对地址off off=0; for ( i=0; idim; ++i) { ind=va_arg(ap,int); if (ind0 || ind = A.bounds[i]) return OVERFLOW; off = off + A.constants[i] * ind; } return OK; } 3、数组元素取值Value Status Value(Array A, Elemtype e, …) { //A是n维数组,e为元素变量,随后是n个下标值 //若各下标不超界,则e赋值为指定的数组元素值,并返回OK。 va_start(ap , e); if ((result=Locate(A, ap, off)) = 0) return result; e = * (A.base + off); return OK; } 4、数组元素赋值Assign Status Assign(Array A, ElemType e, …) { //A是n维数组,e为元素变量,随后是n个下标值 //若各下标不超界,则将e赋值给指定的数组元素,并返回OK。 va_start(ap , e); if ((result=Locate(A, ap, off)) = 0) return result; * (A.base + off) = e; return OK; } 5.5 广义表的存储结构 Status TransposeSMatrix(TSMatrix A,TsMatrix B) {  B.mu=A.nu; B.nu=A.mu; B.tu=A.tu;  if (B.tu) {   q=0; //注意:教材中算法的下标从1开始。   for (col=0; colA.nu; ++col )    for (p=0; pA.tu; ++p )     if (A.data[p].j == col) {       B.data[q].i = A.data[p].j;      B.data[q].j = A.data[p].i;      B.data[q].e = A.data[p].e; ++q;      }  } return OK; } 算法描述如下: //扫描A的所有列 //扫描所有非零元 演

文档评论(0)

1亿VIP精品文档

相关文档