多维数组的存储表示与实现--自己写数据结构

 

       国际惯例,直接上代码

文件multiarray.h存放数据结构体,如下:

#ifndef _MULTIARRAY_H_
#define _MULTIARRAY_H_

#define MAX_DIM  10
#include  

typedef struct _MArray
{
    int *base;   //基址 
    int dim;      //维度 
    int *bounds;  //存放没个维度大小的头指针 
    int *wide;     //存放每个维度偏移常量的头指针 
}MArray,*pMArray;

pMArray init_multi_array(void);
int set_multi_array(pMArray pma,int dim,...);
void destory_multi_array(pMArray pma);
int locate_array_elem(pMArray pma,va_list ap,int *offset) ;
int get_array_elem(int* e,pMArray pma,...);
int assign_array_elem(int e,pMArray pma,...);

#endif

文件multiarray.c存放数据结构实现方法,如下:

/*******************************
时间:2014.12.14
作者:XIAO_PING_PING
编译环境:DEV-C++ 4.9.9.2 
内容:多维数组的存储表示与实现
功能:学习些数据结构 
********************************/

#include 
#include 

#include "multiarray.h"

/*初始化一个空的数组*/
pMArray init_multi_array(void)/*注意:必须以返回值输出,
                               否则后面给成员赋值会报错*/ 
{
    pMArray pma;
    pma = (MArray *)malloc(sizeof(MArray));
    
    pma->base = NULL; 
    pma->bounds = NULL; 
    pma->wide = NULL; 
    pma->dim = 0;    
    
    return pma;
}

/*设置多维数组*/ 
int set_multi_array(pMArray p,int dims,...)
{
    int i,elem_num = 1;   
    va_list ap = NULL;
    
    if(dims < 1 || dims > MAX_DIM)  
    {
        return -2;
    }
    p->dim = dims;
    p->bounds = (int *)malloc(dims * sizeof(int));
    
    if(!p->bounds)
    {
        return -1;               
    }
    
    va_start(ap,dims);
    for(i = 0;i < dims;i++)
    {
        p->bounds[i] = va_arg(ap,int);
        if(p->bounds[i] < 0)
        {
            return -2;
        }
        elem_num *= p->bounds[i];
    }
    va_end(ap);
        
    p->base = (int *)malloc(elem_num * sizeof(int));
    if(p->base == NULL)
    {
        return -2;
    }
    
    p->wide = (int *)malloc(dims * sizeof(int));
    if(!p->wide)
    {
        return -1;               
    }
    
    p->wide[dims - 1] = 1;
    for(i = dims - 2;i >= 0;i--)
    {
        p->wide[i] = p->wide[i + 1] * p->bounds[i + 1];
    }
    
    return 0;
}

/*摧毁多维数组*/ 
void destory_multi_array(pMArray pma)
{
    if(NULL != pma->wide)
    {
        free(pma->wide);  
        pma->wide = NULL;      
    }      
    
    if(NULL != pma->bounds)
    {
        free(pma->bounds);  
        pma->bounds = NULL;      
    }   
    
    if(NULL != pma->base)
    {
        free(pma->base);  
        pma->base = NULL;      
    } 
    
    free(pma);
}

/*定位元素a[i][j][k]...位置*/
int locate_array_elem(pMArray pma,va_list ap,int *offset) 
{
    int i;  
    int cur = 0;
    
    for(i = 0;i < pma->dim ;i++)
    {   
        cur = va_arg(ap,int);
        if(0 > cur || cur > pma->bounds[i])
        {
            return -1;
        }
        
        *offset += pma->wide[i] * cur; 
    }
    
    return 0;
}

/*得到多维数组特定位置的元素*/
int get_array_elem(int* e,pMArray pma,...)
{
    int i;
    int offset = 0;
    int ret = 0;
    va_list ap = NULL;
    
    va_start(ap,pma);
    if(0 != locate_array_elem(pma,ap,&offset))
    {
        return -1;                                  
    }
    va_end(ap);
    
    *e = *(pma->base + offset);
    
    return 0;
}

/*给多维数组特定位置赋值*/ 
int assign_array_elem(int e,pMArray pma,...)
{
    int offset = 0;
    int ret = 0;   
    
    va_list ap = NULL;
    
    va_start(ap,pma);
    
    ret = locate_array_elem(pma,ap,&offset);
    if(ret)
    {
        return -1;                                  
    }
    va_end(ap);
    
    *(pma->base + offset) = e;
    return 0;   
}

测试文件test.c

#include 
#include 
#include 

#include "multiarray.h"

int main()
{
    pMArray arr;  

    int dim = 3;  

    int bound1 = 2, bound2 = 3, bound3 = 4; //arr[2][3][4]数组   

    int i = 0, j = 0, k = 0;  

    int assign_elem = 0;  

    int get_elem = 0;  

    int *p = NULL;  

    arr = init_multi_array();
  
    set_multi_array(arr, dim, bound1, bound2, bound3);  

  

    printf("array.bounds = ");          //顺序输出array.bounds   

    p = arr->bounds;  

    for(i = 0; i < dim; i++)  

    {  

        printf("%d ", *(p + i));  

    }  

  

    printf("\narray.contents = ");      //顺序输出array.contents   

    p = arr->wide;  

    for(i = 0; i < dim; i++)  

    {  

        printf("%d ", *(p + i));  

    }  

  

    printf("\narray[%d][%d][%d] : \n", bound1, bound2, bound3);  

    for(i = 0; i < bound1; i++)  

    {  

        for(j = 0; j < bound2; j++)  

        {  

            for(k = 0; k < bound3; k++)  

            {  

                assign_elem = i * 100 + j * 10 + k;  

                assign_array_elem(assign_elem, arr, i, j, k);  

                get_array_elem(&get_elem, arr, i, j, k);  

                printf("array[%d][%d][%d]=%-4d", i, j, k, get_elem);  

            }  

            printf("\n");  

        }  

        printf("\n");  

    }  

  

    p = arr->base;  

    for(i = 0; i < bound1 * bound2 * bound3; i++)  

    {  

        printf("%-4d", *(p + i));  

        if(i % (bound2 * bound3) == bound2 * bound3 - 1)  

        {  

            printf("\n");  

        }  

    }  

  

    destory_multi_array(arr);  

    getch(); 

    return 0;  
      
}

以及运行结果如下:
多维数组的存储表示与实现--自己写数据结构_第1张图片

你可能感兴趣的:(数据结构)