数据结构_数组
来源:互联网 发布:xenomai编程笔记 编辑:程序博客网 时间:2024/04/30 19:24
#include <iostream>#include <cstdarg>#include <cstdio>#include <cstdlib>#include <cmath>#include <algorithm>#include <cstring>#include <string>#define TRUE 1#define FALSE 0#define OK 1#define ERROR 0#define INFEASIBLE -1#define MAX_ARRAY_DIM 8using namespace std;typedef int ElemType;int elemtotal=1;struct array{ElemType *base; //数组地址 int dim; //数组维数 int *bounds; //数组维界基址int *constants; //映像函数 };int initarray(array *A,int dim, ...) //初始化一个多维数组{va_list ap;if(dim<1||dim>MAX_ARRAY_DIM)return ERROR;A->dim=dim;A->bounds=(int *)malloc(sizeof(int)*dim);if(!A->bounds)return ERROR;va_start(ap,dim);for(int i=0;i<dim;i++){A->bounds[i]=va_arg(ap,int);if(A->bounds[i]<0)return ERROR;elemtotal*=A->bounds[i];}va_end(ap);A->base=(ElemType *)malloc(elemtotal+15*sizeof(ElemType));if(!A->base)return ERROR;A->constants=(int *)malloc(dim*sizeof(int));if(!A->constants)return ERROR; A->constants[dim-1]=1; for(int i=dim-2;i>=0;i--) A->constants[i]=A->bounds[i+1]*A->constants[i+1]; return OK;}void destroyarray(array *A) //释放空间 {if(A->base)free(A->base);if(A->bounds)free(A->bounds);if(A->constants)free(A->constants);A->base=A->bounds=A->constants=NULL;A->dim=0;}int locate(array *A,va_list ap,int *off) //求位置 {int ind;int x=0;for(int i=0;i<A->dim;i++){ind=va_arg(ap,int);if(ind<0||ind>=A->bounds[i])return ERROR;x+=A->constants[i]*ind;}*off=x;return OK;}int value(ElemType *e,array *A, ...) //赋值e {va_list ap;int off;va_start(ap,A);if(locate(A,ap,&off)==ERROR)return ERROR;*e=*(A->base+off);return OK;}int assign(array *A,ElemType e, ...){va_list ap;int off;va_start(ap,e);if(locate(A,ap,&off)==ERROR)return ERROR;*(A->base+off)=e;return OK;}int getarray(array *A,ElemType e, ...){va_list ap;int off;va_start(ap,e);if(locate(A,ap,&off)==ERROR)return ERROR;for(int i=elemtotal-1;i>=off;i--){*(A->base+i+1)=*(A->base+i);}*(A->base+off)=e;return OK;}int delarray(array *A,ElemType *e, ...){va_list ap;int off;va_start(ap,e);if(locate(A,ap,&off)==ERROR)return ERROR;*e=*(A->base+off);for(int i=off+1;i<=elemtotal;i++){*(A->base+i-1)=*(A->base+i);}return OK;}int main(){array A;int *p,dim=3,bound1=3,bound2=4,bound3=2;ElemType e,*p1;initarray(&A,dim,bound1,bound2,bound3); p=A.bounds; printf("A->bounds="); for(int i=0;i<dim;i++) { printf("%d ",*(p+i)); } p=A.constants; printf("\nA->constants="); for(int i=0;i<dim;i++) { printf("%d ",*(p+i)); } printf("\n%d页%d行%d列矩阵元素如下:\n",bound1,bound2,bound3); for(int i=0;i<bound1;i++) { for(int j=0;j<bound2;j++) { for(int k=0;k<bound3;k++) { assign(&A,i*100+j*10+k,i,j,k); value(&e,&A,i,j,k); printf("A[%d][%d][%d]=%2d ",i,j,k,e); } printf("\n"); } printf("\n"); } p1=A.base; printf("A->base=\n"); for(int i=0;i<bound1*bound2*bound3;i++) { printf("%4d",*(p1+i)); if(i%(bound2*bound3)==bound2*bound3-1) printf("\n"); } getarray(&A,999,1,1,1);//插入 printf("插入一个数之后的数组:\n"); for(int i=0;i<bound1*bound2*bound3+1;i++) { printf("%4d",*(p1+i)); if(i%(bound2*bound3)==bound2*bound3-1) printf("\n"); } printf("\n"); printf("删除一个元素之后的数组:\n"); int ee; delarray(&A,&ee,1,1,1); for(int i=0;i<bound1*bound2*bound3;i++) { printf("%4d",*(p1+i)); if(i%(bound2*bound3)==bound2*bound3-1) printf("\n"); } printf("删除的数为:%d\n",ee); printf("A->dim=%d\n",A.dim); destroyarray(&A); return 0;}
0 0
- 数据结构_数组
- 数据结构_树形数组
- 数据结构(java)_数组顺序查找
- 数据结构03_栈、队列和数组
- 数据结构与算法总结2_常用的数据结构(数组)
- 《数据结构与算法C#语言描述》笔记2_数组
- 数据结构基础_对一个数组进行升序排序
- 07-数据结构_线性结构-连续存储-数组
- 树状数组,小结_(优雅的数据结构)
- 树状数组,小结_(优雅的数据结构)
- 复习(数据结构):动态数组:c++_常规写法
- 数据结构与算法分析_数组的子序列问题
- 集合框架_数据结构之数组和链表
- 数据结构和算法_数组/链表反转
- Java数据结构之数组_动力节点Java学院整理
- 数据结构_数组与广义表_链表的递归遍历
- 数据结构_数组与广义表_矩阵的十字链表存储稀疏矩阵相加
- 数据结构_数组与广义表_广义表的建立、遍历、复制、求深度
- 九度考研真题 浙大 2005-2浙大1014:排名 多重排序
- 数据结构_括号匹配
- SimpleDateFormat并发问题解决方案
- VIII.1 INS-30510: Insufficient number of ASM disks selected.
- 九度考研真题 浙大 2005-3浙大1012:畅通工程
- 数据结构_数组
- java学习之浅谈多线程4--SwingWorker
- 视频框架 Vitamio 使用教程+部分心得 (二)第一个视频demo
- Java编程思想重点笔记(Java开发必看)
- java集合的特点
- 九度考研真题 浙大 2006-1浙大1015:还是A+B 比较
- 三分钟学会JUnit单元测试
- QImage的显示
- bash 判断进程是否运行