C语言 数组的顺序表示与实现 数据结构
2008-05-02 18:14
811 查看
C语言 数组的顺序表示与实现 数据结构
今天下午琢磨c语言中的数组的存储结构,特别是多维数组的存储结构,结合以前学习的汇编语言,对多维数组
在存储单位中的分配情况,书上的数据结构中的说明有些代码我实在是没看明白,但是我还是努力地把代码,写
了下来,虽然代码不能运行,但是也大致地体现了,多维数组在内存机制中调用方式。
好啦,还是和大家一起分享,如果大家对代码有什么看法或观点,可以发送邮件到yijiyong100@163.com,我会
尽快给您回复的。
今天下午琢磨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,我会尽快给您回复的。
相关文章推荐
- 数组的顺序存储表示和实现-数据结构
- [C语言][数据结构]基础的线性表的顺序表示和实现
- 数据结构之---C语言实现图的数组(邻接矩阵)存储表示
- 数据结构(严蔚敏)C语言实现---线性表的顺序存储
- 一、数据结构基础之顺序表C语言实现
- 三、数据结构基础之顺序栈C语言实现
- C语言实现顺序表-数据结构
- 数据结构教程 第六课 线性表的顺序表示和实现
- 数据结构之顺序表(数组实现与链表实现)
- 数组的顺序结构表示和实现
- 数组的顺序表示和C语言实现
- 数据结构(Data structure):用链表实现多项式的表示和运算(C语言)
- 数据结构笔记(一)线性表的顺序表示和基本操作及其顺序表实现的集合运算(A-B)U(B-A)实例
- 数据结构之C语言实现顺序表(SeqList)无主函数
- 线性表的顺序表示和实现 - 数据结构
- 【数据结构之线性表顺序存储】简单的数组的方式实现
- 数据结构之栈的顺序表示及其实现
- 数据结构之:线性表的顺序表示和实现
- 数据结构中,几种树的结构表示方法(C语言实现)
- C语言 数据结构 线性表的顺序表示