您的位置:首页 > 理论基础 > 数据结构算法

C语言 数组的顺序表示与实现 数据结构

2008-05-02 18:14 811 查看
C语言 数组的顺序表示与实现 数据结构

今天下午琢磨c语言中的数组的存储结构,特别是多维数组的存储结构,结合以前学习的汇编语言,对多维数组

在存储单位中的分配情况,书上的数据结构中的说明有些代码我实在是没看明白,但是我还是努力地把代码,写

了下来,虽然代码不能运行,但是也大致地体现了,多维数组在内存机制中调用方式。

好啦,还是和大家一起分享,如果大家对代码有什么看法或观点,可以发送邮件到yijiyong100@163.com,我会

尽快给您回复的。

/****************************************/
/*Description: Sequence Array*/
/*Email:yijiyong100@163.com*/
/*Author:yi_landry Harbin Normal University Computer Science*/
/*Date:2008-5-2*/
/*Copyright:HNU2008.cop*/
/*Environment:turbo c 2.01 English Version*/
/****************************************/

# include<stdlib.h>
# include<stdio.h>
# include<stdarg.h>/*afford macro va_start,va_arg_arg and va_end*/
# define OVERFLOW 0
# define OK 1
# define MAX_ARRAY_DIM 8 //The largest dimension of the sequence array

/****************************************/
/*The struct of sequence array */
/****************************************/
struct SqArray
{
int * base;/*the base address of the sequence array*/
int dim; /*the dimension of the sequence array*/
int * bounds;/*the base address of dimension bound*/
int * constants;/*the base address of mapping function*/
int elemtotal;/*the total elements of in the sequence array*/
}A;

/****************************************/
/*Initial the array*/
/****************************************/
InitArray(struct SqArray * A,int dim,va_list ap)/*va_list is a list type can contain variable elements*/
{
int i;
int p = dim;
if (dim <1 || dim > MAX_ARRAY_DIM)
{
printf("The dimension %d you input is overflow!/n",dim);
return 0;
}
A->dim = dim;
A->bounds = (int *)malloc(dim *sizeof(int));
if (!A->bounds) exit(OVERFLOW);
A->elemtotal = 1;
va_start(ap,dim);
for (i=0;i<dim;++i)
{
A->bounds[i] = va_arg(ap,int);
if (A->bounds[i]<0) exit(OVERFLOW);
A->elemtotal *= A->bounds[i];
}
va_end(ap);
A->base = (int *)malloc(A->elemtotal * sizeof(int));
if (!A->base) exit(OVERFLOW);
A->constants = (int *)malloc(dim* sizeof(int));
if (!A->constants)
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;
}

/****************************************/
/*Destroy the current sequence array*/
/****************************************/
Destrory(struct SqArray * A)
{
if (!A->base) return 0;
free(A->base); A->base = NULL;
if (!A->bounds) return 0;
free(A->bounds); A->bounds = NULL;
if (!A->constants); return 0;
free(A->constants); A->constants =NULL;
return 0;
}

/****************************************/
/*get some element in the array*/
/****************************************/
Locate(struct SqArray * A,va_list ap,int &off)
{
int i,ind;
for (i =0;i<A->dim;++i)
{
ind = va_arg(ap,int);
if (ind <0 || ind>=A->bounds[i]) return 0;
off += A->constants[i]*ind;
}
return OK:
}

/****************************************/
/*get the value of some element in the array*/
/****************************************/
Value(struct * A,int &e)
{
int result;
va_start(ap,e);
if ((result = Locate(A,ap,off))<=0) return result;
e = *(A->base + off);
return 0;
}

/****************************************/
/*another way to get the element's value*/
/****************************************/
AssginArray(struct SqArray * A,int e,va_list ap)
{
va_start(ap,e);
if ((result = Locate(A,ap,off))<=0) return result;
*(A->base+off) =e;
return OK:
}

main()
{
return 0;
}
如果大家对代码有什么看法或观点,可以发送邮件到yijiyong100@163.com,我会尽快给您回复的。
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: