[데이터 구조] 배열 의 순서 저장 표시

#include 
#include 
#include 

#define MAX_ARRAY_DIM 8     //       

#define OK 1
#define ERROR 0
#define UNDERFLOW -1
#define OVERFLOW -2

typedef int ElemType;
typedef int Status;

typedef struct {
    ElemType  *base;    //     
    int       dim;      //     
    int       *bounds;  //               
    int       *constants;   //               
} array;

//     
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){
        return ERROR;
    }
    int elemtotal = 1;  //      
    va_list ap;
    va_start(ap, dim);
    for(int i=0; i=0; i--){
        a.constants[i] = a.bounds[i+1] * a.constants[i+1];  //                   
    }
    return OK;
}

//    
Status DestoryArray(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;
}

//           , off  
Status Locate(array a, va_list ap, int &off){
    off = 0;
    int ind;
    for(int i=0; i=a.bounds[i]){
            return OVERFLOW;
        }
        off += a.constants[i] * ind;
    }
    return OK;
}

//            
Status Value(array a, ElemType &e, ...){
    va_list ap;
    va_start(ap, e);
    Status result;
    int off;
    if((result = Locate(a, ap, off)) <= 0){
        return result;
    }
    va_end(ap);
    e = *(a.base + off);
    return OK;
}

//  
Status Assign(array &a, ElemType &e, ...){
    va_list ap;
    va_start(ap, e);
    Status result;
    int off;
    if((result = Locate(a, ap, off)) <= 0){
        return result;
    }
    va_end(ap);
    *(a.base + off) = e;
    return OK;
}

좋은 웹페이지 즐겨찾기