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

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,我会

尽快给您回复的。

  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值