数组的顺序存储表示和实现:
#include
#define MAX_ARRAY_DIM 8
typedef struct {
ElemType *base.
int dim.
int *bounds.
int *constants.
}Array.
Status InitArray(Array &.A,int dim,...).
Status DestroyArray(Array &.A).
Status Value(Array A,ElemType &.e,...).
Status Assign(Array &.A,ElemType e,...).
基本操作的算法描述:
Status InitArray(Array &.A,int dim,...){
if(dim<1||dim>MAX_ARRAY_DIM) return ERROR.
A.dim=dim.
A.bounds=(int *)malloc(dim *sizeof(int)).
if(!A.bounds) exit(OVERFLOW).
elemtotal=1.
va_start(ap,dim).
for(i=1.i
A.bounds[i]=va_arg(ap,int).
if(A.bounds[i]<0) return UNDERFLOW.
elemtotal*=A.bounds[i].
}
va_end(ap).
A.base=(ElemType *)malloc(elemtotal*sizeof(ElemType)).
if(!A.base) exit(OVERFLOW).
A.constants=(int *)malloc(dim*sizeof(int)).
if(!A.constants) exit(OVERFLOW).
A.constants[dim-1]=1.
for(i=dim-2.i>=0.--i)
A.constants[i]=A.bounds[i 1]*A.constants[i 1].
return OK.
}
Status DestoyArray(Array &.A){
if(!A.base) return ERROR.
free(A.base). A.base=NULL.
if !(A.bounds) return ERROR.