数组的顺序存储表示
来源:互联网 发布:淘宝宝贝怎么上架视频 编辑:程序博客网 时间:2024/05/16 18:15
//以下构造了大小为3*3*3的3维数组
#include<iostream>
#include<cstdarg>#define OK 1
#define ERROR 0
#define OVERFLOW -1
typedef int elemtype;
using namespace std;
typedef struct{
int dim;
int*bounds;
elemtype *base;
int*constants;
}Array;
int InitArray(Array&L,int dim,...){ //创建Array
L.dim=dim;
int elemtotal=1;
L.bounds=(int *)malloc(dim*sizeof(int));
va_list ap;
va_start(ap,dim);
for(int i=0;i<dim;i++){
L.bounds[i]=va_arg(ap,int);
elemtotal*=L.bounds[i];
}
L.base=(elemtype*)malloc(elemtotal*sizeof(elemtype));
L.constants=(int *)malloc(dim*sizeof(int));L.constants[dim-1]=1;
for(i=dim-2;i>=0;i--){
L.constants[i]=L.constants[i+1]*L.bounds[i+1];
}va_end(ap);return OK;
}
int Locate(Array L,va_list ap,int &off){off=0;//定位
for(int i=0;i<L.dim;i++){int lop=va_arg(ap,int);
off+=lop*L.constants[i];
}
return OK;}
int Value(Array L,elemtype &e,int n,...)//获取元素
{
va_list ap;int off;va_start(ap,n);Locate(L,ap,off);
e=*(L.base+off);va_end(ap);
return OK;}
int Assign(Array &L,elemtype e,int n,...)//输入元素
{va_list ap;int off;va_start(ap,n);Locate(L,ap,off);
*(L.base+off)=e;va_end(ap);
return OK;}
int main(){
Array L;int e=0;
InitArray(L,3,3,3,3);
for(int i=0;i<3;i++)
for(int j=0;j<3;j++)
for(int u=0;u<3;u++)
{ Assign(L,e,3,i,j,u);e++;}
cout<<"输出:" <<endl;
for( i=0;i<3;i++)
for(int j=0;j<3;j++){
for(int u=0;u<3;u++)
{ Value(L,e,3,i,j,u);cout<<i<<j<<u<<":"<<e<<endl;}
cout<<endl;
}
return OK;}
2 0
- 数组的顺序存储表示
- 数组的顺序存储表示
- 数组的顺序存储表示和实现
- 顺序表是线性表基于数组的存储表示
- 数组的顺序存储表示和实现-数据结构
- (严蔚敏版)数组的顺序存储表示和实现代码
- 数组顺序存储表示和实现
- 栈的顺序存储表示
- 串的顺序存储表示
- 栈的顺序存储表示
- 多维数组的顺序表示
- 多维数组的顺序表示
- 第五章 数组的数组的顺序存储表示和实现
- 数据结构--数组和广义表--数组的顺序存储表示和实现
- 数组的顺序存储
- 数据结构之---C语言实现数组的顺序存储表示(可运行)
- C语言数据结构——数组顺序存储结构的实现和表示
- 串的定长顺序存储表示
- test1
- genymotion arm translation 1.1和支持5.0的包
- 微服务(Microservice)那点事
- 类学习四
- test
- 数组的顺序存储表示
- 你真的会二分查找吗?
- [教程] 从png到ico
- Section 2.4 图&章末总结
- CenOS6.5 python开发环境搭建 Django (一)
- POJ 2987 (最大权闭合图 最小割)
- c++的类的理解
- caffe上手2:使用INRIA行人数据集对BVLC Caffe进行fine-tuning
- PB中一些技巧