zoukankan      html  css  js  c++  java
  • 数组实现

    一、数组的定义

    几乎所有的程序设计语言都把数组类型设定为固有类型。

    以抽象数据类型的形式讨论数组的定义和实现,可以让我们加深对数组类型的理解。

    数组的定义:

    ADT Array{

    数据对象:ji=0,...,bi-1,i=1,2,...,n;

    D={aj1j2...jn|n(>0)称为数组的维数,bi是数组第i维的长度,ji是数组元素的第i维下标,aj1j2...jn (-ElemSet}

    数据关系:R={R1,R2,...Rn|

    Ri={<aj1...ji...jn,aj1...ji+1 ...jn>|

    0<=jk<=bk-1,1<=k<=n且k<>i,

    0<=ji<=bi-2,aj1...ji...jn,

    aj1...ji+1 ...jn(-D,i=2,...n}

    基本操作:

    InitArray(&A,n,bound1,...,boundn)

    若维数和各维长度合法,则构造相应的数组A,并返回OK.

    DestroyArray(&A)

    操作结果:销毁数组A.

    Value(A,&e,index1,...,indexn)

    初始条件:A是n维数组,e为元素变量,随后是n个下标值.

    操作结果:若各下标不超界,则e赋值为所指定的A的元素值,并返回OK.

    Assign(&A,e,index1,...,indexn)

    初始条件:A是n维数组,e为元素变量,随后是n个下标值.

    操作结果:若下标不超界,则将e的值赋给 所指定的A的元素,并返回OK.

    }ADT Array

    列向量的一维数组:

    二代码实现

    #include <stdio.h>
    #include <stdlib.h>
    #include<stdarg.h>
    #define MAX_ARRAY_DIM 8
    #define OK 1
    #define OVERFLOW -1
    #define UNDERFLOW -2
    #define ERROR -3


    typedef int Status;
    typedef int ElemType;
    typedef struct {
      ElemType *base;
      int dim;
      int *bounds;
      int *constants;
    }Array;
    Status InitArray(Array *A,int di,...);
    Status DestroyArray(Array *A);
    Status Value(Array A,ElemType *e,...);
    Status Assign(Array *A,ElemType e,...);

    Status InitArray(Array *A,int di,...){
      int elemtotal,i;    
      va_list ap;
      if(di<1||di>MAX_ARRAY_DIM) return ERROR;
      A->dim=di;
      A->bounds=(int *)malloc(di *sizeof(int));
      if(!A->bounds) exit(OVERFLOW);
      elemtotal=1;
      va_start(ap,di);
      for(i=1;i<di;++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(di*sizeof(int));
      if(!A->constants) exit(OVERFLOW);
      A->constants[di-1]=1;
      for(i=di-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;
      free(A->bounds); A->bounds=NULL;
      if (!A->constants) return ERROR;
      free(A->constants); A->constants=NULL;
      return OK;
    }

    Status Locate(Array A,va_list ap){
      int i,ind,off;    
      off=0;
      for(i=0;i<A.dim;++i){
        ind=va_arg(ap,int);
        if(ind<0||ind>=A.bounds[i]) return -1;
        off+=A.constants[i]*ind;
      }
      return off;
    }

    Status Value(Array A,ElemType *e,...){
      int result;  
      va_list ap; 
      va_start(ap,e);
      if ((result=Locate(A,ap))<=-1) return result;
      *e=*(A.base+result);
      return OK;
    }

    Status Assign(Array *A,ElemType e,...){
      int result; 
      va_list ap;    
      int off;
      va_start(ap,e);
      if((result=Locate(*A,ap))<=-1) return result;
      *(A->base+off)=e;
      return OK;
    }

    int main(int argc, char *argv[])
    {
      Array A,*Arr;
      Arr=(Array *)malloc(sizeof(Array));
      int dim,j,q;
      int m,n;
      ElemType t;
      va_list ap;
      dim=3;
      InitArray(Arr,2,3,4);
      t=1;
      Assign(Arr,t,1,1);
      t=2;
      Assign(Arr,t,1,2);
      A=*Arr;
      n=2;
      Value(A,n);
      printf("%d\n",j);
      system("PAUSE"); 
      return 0;
    }

  • 相关阅读:
    flume sink两种类型 file_rool 自定义sing com.mycomm.MySink even if there is only one event, the event has to be sent in an array
    为什么引入进程20年后,又引入线程?
    As of Flume 1.4.0, Avro is the default RPC protocol.
    Google Protocol Buffer 的使用和原理
    Log4j 2
    统一日志 统一订单
    网站行为跟踪 Website Activity Tracking Log Aggregation 日志聚合 In comparison to log-centric systems like Scribe or Flume
    Percolator
    友盟吴磊:移动大数据平台的架构、实践与数据增值
    Twitter的RPC框架Finagle简介
  • 原文地址:https://www.cnblogs.com/djcsch2001/p/2035168.html
Copyright © 2011-2022 走看看