设为首页 加入收藏

TOP

数据结构之---C语言实现数组的顺序存储表示(可运行)
2015-07-16 12:04:22 来源: 作者: 【 】 浏览:104
Tags:数据结构 ---C 语言 实现 顺序 存储 表示 运行
//数组的顺序存储表示
//杨鑫
#include 
  
   
#include 
   
     #include 
    
      #include 
     
       #define OK 1 #define ERROR 0 #define UNDERFLOW 2 #define MAX_ARRAY_DIM 8 typedef int Status; typedef int ElemType; typedef struct { ElemType *base; //数组的实体 int dim; //数组维数 int *bounds; //根据下文bound应该是bounds,数组各维的长度 int *constants; //数组映像函数常量的基址 }Array; //数组初始化 Status InitArray(Array *A, int dim,...) { int elemtotal=1,i; va_list ap; if (dim < 1 || dim > MAX_ARRAY_DIM) return ERROR; A->dim = dim; A->bounds = (int*)malloc(dim*sizeof(int)); if(!A->bounds) return ERROR; va_start(ap,dim); for(i=0;i
      
       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) return ERROR; A->constants=(int*)malloc(dim*sizeof(int)); if(!A->constants) return ERROR; 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 DestroyArray(Array *A) { free(A->base); free(A->bounds); free(A->constants); return OK; } //找到A的地址 Status Locate(Array A,va_list ap,int *off) { int ind, i; *off=0; for(i=0;i
       
        =A.bounds[i]) return ERROR; *off+=A.constants[i]*ind; } return OK; } //赋值把值赋值给数组A Status Assign(Array *A,ElemType e,...) { va_list ap; Status result; int i,j,k; int off; va_start(ap,e); if((result=Locate(*A,ap,&off))<0) return result; *(A->base+off)=e; va_end(ap); return OK; } //把值赋值给数组A中指定的元素e Status Value(Array A,ElemType *e,...) { int off; Status result; va_list ap; va_start(ap,e); if ((result=Locate(A,ap,&off))<0) return result; *e=*(A.base+off); va_start(ap,e); return OK; } int main() { int i,j,k; Array A; ElemType e; A.dim=3; InitArray(&A,A.dim,2,2,2); printf("这是一个 %d 维的数组!\n",A.dim); printf("数组每个维度的大小:\n"); for (i=0;i
         
         



如图:



】【打印繁体】【投稿】【收藏】 【推荐】【举报】【评论】 【关闭】 【返回顶部
分享到: 
上一篇最短的计算大数乘法的c程序 下一篇OC教程4目标动作回调-Target Acti..

评论

帐  号: 密码: (新用户注册)
验 证 码:
表  情:
内  容: