数据结构示例之顺序数组的实现
来源:互联网 发布:阿里云域名查询 编辑:程序博客网 时间:2024/04/30 12:27
以下为展示顺序数组的示例:
1.用c语言实现的版本
#include<stdio.h> /* EOF(=^Z或F6),NULL */#include<math.h> /* floor(),ceil(),abs() */#include<stdlib.h> /*申请和释放内存*/#include<stdarg.h> /*可变参数*/#define OK 1 //成功标志#define ERROR 0 //错误标志#define MAX_ARRAY_DIM 8 //数组最大维数typedef int ElemType;typedef int Status; /* Status是函数的类型,其值是函数结果状态代码,如OK等 */typedef struct{ElemType *base; /* 数组元素基址,由InitArray分配 */int dim; /* 数组维数 */int *bounds; /* 数组维界基址,由InitArray分配 */int *constants; /* 数组映象函数常量基数,相当于每一维度的权重值,由InitArray分配 */}Array;/* 顺序存储数组的基本操作*/Status InitArray(Array *A, int dim, ...){ /* 若维数dim和各维长度合法,则构造相应的数组A,并返回OK */int elemtotal = 1, i; /* elemtotal是元素总值 */if (dim<1 || dim>MAX_ARRAY_DIM) //判断数组维数{return ERROR;}(*A).dim = dim; /* 数组维数 */(*A).bounds = (int *)malloc(dim*sizeof(int)); /* 数组维界基址 */if (!(*A).bounds){exit(OVERFLOW);}va_list ap;va_start(ap, dim);for (i = 0; i < dim; ++i){(*A).bounds[i] = va_arg(ap, int);if ((*A).bounds[i] < 0){return UNDERFLOW; /* 在math.h中定义为4 */}elemtotal *= (*A).bounds[i];}va_end(ap);(*A).base = (ElemType *)malloc(elemtotal*sizeof(ElemType));if (!(*A).base){exit(OVERFLOW);}(*A).constants = (int *)malloc(dim*sizeof(int));if (!(*A).constants){exit(OVERFLOW);}(*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;}/* 销毁数组A */Status DestroyArray(Array *A){ if ((*A).base){free((*A).base);(*A).base = NULL;}else{return ERROR;}if ((*A).bounds){free((*A).bounds);(*A).bounds = NULL;}else{return ERROR;}if ((*A).constants){free((*A).constants);(*A).constants = NULL;}else{return ERROR;}return OK;}/* 若ap指示的各下标值合法,则求出该元素在A中的相对地址off *//* Value()、Assign()调用此函数 */Status Locate(Array A, va_list ap, int *off) {int i, ind;*off = 0;for (i = 0; i < A.dim; ++i){ind = va_arg(ap, int);if (ind < 0 || ind >= A.bounds[i]){return OVERFLOW;}*off += A.constants[i] * ind;}return OK;}/* ...依次为各维的下标值,若各下标合法,则e被赋值为A的相应的元素值 */Status Value(ElemType *e, Array A, ...){va_list ap;Status result;int off;va_start(ap, A);if ((result = Locate(A, ap, &off)) == OVERFLOW) /* 调用Locate() */{return result;}*e = *(A.base + off);return OK;}/* ...依次为各维的下标值,若各下标合法,则将e的值赋给A的指定的元素 */Status Assign(Array *A, ElemType e, ...){ va_list ap;Status result;int off;va_start(ap, e);if ((result = Locate(*A, ap, &off)) == OVERFLOW) /* 调用Locate() */{return result;}*((*A).base + off) = e;return OK;}void main(){Array A;int i, j, k, *p, dim = 3, bound1 = 3, bound2 = 4, bound3 = 2; /* a[3][4][2]数组 */ElemType e, *p1;/* 构造3*4*2的3维数组A */InitArray(&A, dim, bound1, bound2, bound3); /* 顺序输出A.bounds */printf("输出数组各维度的值:\n ");p = A.bounds;for (i = 0; i < dim; ++i){printf("A.bounds[%d] = %d\n ", i, *(p + i));}printf("\n");/* 顺序输出A.constants */printf("输出数组映像函数常量基数的值(相当于每一维度的权重值):\n ");p = A.constants;for (i = 0; i < dim; ++i){printf("A.constants[%d] = %d\n ", i, *(p + i));}printf("\n\n");printf("%d页%d行%d列矩阵元素如下:\n", bound1, bound2, bound3);for (i = 0; i < bound1; ++i){printf("第%d页:\n", i);for (j = 0; j < bound2; ++j){for (k = 0; k < bound3; ++k){Assign(&A, i * 100 + j * 10 + k, i, j, k); /* 将i*100+j*10+k赋值给A[i][j][k] */Value(&e, A, i, j, k); /* 将A[i][j][k]的值赋给e */printf("A[%d][%d][%d]=%2d ", i, j, k, e); /* 输出A[i][j][k] */}printf("\n");}printf("\n");}p1 = A.base;printf("顺序输出Array的元素\n");for (i = 0; i < bound1*bound2*bound3; ++i) /* 顺序输出A.base */{printf("%4d", *(p1 + i));//输出换行if (i % (bound2*bound3) == (bound2*bound3 - 1)){printf("\n");}}/* 销毁数组A */DestroyArray(&A);}
运行结果如下图所示:
0 0
- 数据结构示例之顺序数组的实现
- 数据结构示例之用数组顺序队列
- 数据结构示例之用链表实现顺序队列
- 数据结构示例之使用数组实现栈
- 数据结构:数组的顺序存储实现
- 【数据结构之线性表顺序存储】简单的数组的方式实现
- c数据结构之顺序栈(数组实现1)
- C数据结构之顺序栈(数组实现2)
- 数据结构示例之数组模拟多栈实现
- 数据结构示例之用数组实现圆形队列
- 简单数据结构的实现之顺序栈
- 数据结构之栈的顺序表实现
- 数据结构之队列的顺序实现
- 数据结构之顺序表的实现
- JAVA数据结构之顺序队列的实现
- 数据结构之---C语言实现数组的顺序存储表示(可运行)
- 数据结构示例之数组翻转
- C语言 数组的顺序表示与实现 数据结构
- Canvas学习
- 重启http服务
- synchronized(this)与synchronized(class)
- MySQL自定义函数用法详解-复合结构自定义变量/流程控制--及其相关案例
- 暑期至今学习总结
- 数据结构示例之顺序数组的实现
- android:如何做到软件盘弹出只顶起输入框
- Spark RDD
- 数据结构实验之数组三:快速转置
- google protobuf数据类型
- Java list 判断时需判断非空
- 【思想感悟】一屋不扫何以扫天下
- 数字和一个数学运算符
- 阶乘