数据结构之数组Array
数组Array
基本操作
Status InitArray(int dimm,...)//若维数dim和随后的各维长度合法,则构造相应的数组A,并返回OK
Status DestroyArray() //销毁数组A
Status Locate(va_list ap,int &off) //若ap指示的各下标值合法,则求出该元素在A中相对地址off
Status Value(ElemType &e,...) //A是n维数组,e为元素变量,随后是n个下标值。若各下表不越界,则e赋值为所指定的A的元素值,并返回OK。
Status Assign(ElemType e,...) //A是n维数组,e为元素变量,随后是n各下表值。若各下标不越界,则将e的值付给所指定的A的元素,并返回OK。
几个小程序
//
//by coolxxx
//#include<bits/stdc++.h>
#include<iostream>
#include<algorithm>
#include<string>
#include<iomanip>
#include<map>
#include<stack>
#include<queue>
#include<set>
#include<bitset>
#include<memory.h>
#include<time.h>
#include<stdio.h>
#include<stdlib.h>
#include<string.h>
//#include<stdbool.h>
#include<math.h>
#define min(a,b) ((a)<(b)?(a):(b))
#define max(a,b) ((a)>(b)?(a):(b))
#define abs(a) ((a)>0?(a):(-(a)))
#define lowbit(a) (a&(-a))
#define sqr(a) ((a)*(a))
#define swap(a,b) ((a)^=(b),(b)^=(a),(a)^=(b))
#define mem(a,b) memset(a,b,sizeof(a))
#define eps (1e-10)
#define J 10000
#define mod 1000000007
#define MAX 0x7f7f7f7f
#define PI 3.14159265358979323
#pragma comment(linker,"/STACK:1024000000,1024000000")
#define N 8
const int OK=;
const int ERROR=;
const int INFEASIBLE=-;
typedef int Status;
using namespace std;
typedef long long LL;
double anss;
LL aans;
int cas,cass;
LL n,m,lll,ans; typedef int ElemType;
#include<stdarg.h> //标准头文件,提供宏va_start、va_arg、va_end 用于存取变长参数表
const int MAX_ARRAY_DIM=; //假设数组维数的最大值为8
typedef struct
{
ElemType *base; //数组元素基址,由InitArray分配
int dim; //数组维数
int *bounds; //数组维界基址,由InitArray分配
int *constants; //数组映像函数常量基址,由InitArray分配
int elemtotal;
Status InitArray(int dimm,...)//若维数dim和随后的各维长度合法,则构造相应的数组A,并返回OK
{
int i;
va_list ap;
if(dimm< || dimm>MAX_ARRAY_DIM)return ERROR;
dim=dimm;
bounds=(int *)malloc(dim*sizeof(int));
if(!bounds)exit(OVERFLOW);//若各维长度合法,则存入A.bounds,并求出A的元素总数elemtotal
elemtotal=;
va_start(ap,dim); //ap为va_list类型,是存放变长参量数表信息的数组
for(i=;i<dim;i++)
{
bounds[i]=va_arg(ap,int);
if(bounds[i]<)return UNDERFLOW;
elemtotal*=bounds[i];
}
va_end(ap);
base=(ElemType *)malloc(elemtotal*sizeof(ElemType));
if(!base)exit(OVERFLOW);
constants=(int *)malloc(dim*sizeof(int));
//求映像函数的常数ci,并存入A.constants[i-1],i=1,...,dim
if(!constants)exit(OVERFLOW);
constants[dim-]=; //L=1,指针的增减以元素的大小为单位
for(i=dim-;i>=;i--)
constants[i]=bounds[i+]*constants[i+];
return OK;
}//InitArray Status DestroyArray() //销毁数组A
{
if(!base)return ERROR;
free(base);base=NULL;
if(!bounds)return ERROR;
free(bounds);bounds=NULL;
if(!constants)return ERROR;
free(constants);constants=NULL;
return OK;
}//DestroyArray Status Locate(va_list ap,int &off) //若ap指示的各下标值合法,则求出该元素在A中相对地址off
{
int i,ind;
off=;
for(i=;i<dim;i++)
{
ind=va_arg(ap,int);
if(ind< || ind>=bounds[i])return OVERFLOW;
off+=constants[i]*ind;
}
return OK;
}//Locate Status Value(ElemType &e,...) //A是n维数组,e为元素变量,随后是n个下标值。
//若各下表不越界,则e赋值为所指定的A的元素值,并返回OK。
{
va_list ap;
int result,off;
va_start(ap,e);
if((result=Locate(ap,off))<=)return result;
e=*(base+off);
return OK;
}//Value Status Assign(ElemType e,...) //A是n维数组,e为元素变量,随后是n各下表值。
//若各下标不越界,则将e的值付给所指定的A的元素,并返回OK。
{
va_list ap;
int result,off;
va_start(ap,e);
if((result=Locate(ap,off))<=)return result;
*(base+off)=e;
return OK;
}//Assign }Array;
void ArrayCheck()
{
int i,j,k;
Array A;
ElemType e;
A.InitArray(,,,);
printf("维度:%d\n总元素个数:%d\n各维维界:",A.dim,A.elemtotal);
for(i=;i<A.dim;i++)
printf("%d ",A.bounds[i]);
puts("");
for(i=;i<A.bounds[];i++)
for(j=;j<A.bounds[];j++)
for(k=;k<A.bounds[];k++)
A.Assign(i*+j*+k+,i,j,k);
for(i=;i<A.bounds[];i++,puts(""))
for(j=;j<A.bounds[];j++,puts(""))
for(k=;k<A.bounds[];k++)
printf("%d ",(A.Value(e,i,j,k),e));
A.DestroyArray();
puts("");
}
/*
程序结果:
维度:3
总元素个数:12
各维维界:2 3 2
111 112
121 122
131 132 211 212
221 222
231 232
*/
int main()
{
#ifndef ONLINE_JUDGEW
// freopen("1.txt","r",stdin);
freopen("2.txt","w",stdout);
#endif
int i,j,k;
int x,y,z,xx,yy;
// init();
// for(scanf("%d",&cass);cass;cass--)
// for(scanf("%d",&cas),cass=1;cass<=cas;cass++)
// while(~scanf("%s",s))
// while(~scanf("%d%d",&n,&m))
{
ArrayCheck();
}
return ;
}
/*
// //
*/
数据结构之数组Array的更多相关文章
- 算法与数据结构基础 - 数组(Array)
数组基础 数组是最基础的数据结构,特点是O(1)时间读取任意下标元素,经常应用于排序(Sort).双指针(Two Pointers).二分查找(Binary Search).动态规划(DP)等算法.顺 ...
- 表示集合的数据结构:数组(Array),对象(Object),Map和Set
Map和Set是ES6标准新增的数据类型 Map: 是一组键值对的结构,使用一个二维数组来初始化Map,例如: var m = new Map([['xiaohong',100],['xiaolan' ...
- 我理解的数据结构(一)—— 数组(Array)
我理解的数据结构(一)-- 数组(Array) 首先,我是一个phper,但是毕竟php是一个脚本语言,如果使用脚本语言去理解数据结构具有一定的局限性.因为脚本语言是不需要编译的,如果你的语法写的不错 ...
- javascript数组array
注意:1.array的length不是只读的.可以从数组的末尾移出项或者向数组中添加新项.看下面例子: var colors = ["red","yellow" ...
- Java ArrayList和Vector、LinkedList与ArrayList、数组(Array)和列表集合(ArrayList)的区别
ArrayList和Vector的区别ArrayList与Vector主要从二方面来说. 一.同步性: Vector是线程安全的,也就是说是同步的,而ArrayList是线程序不安全的,不是同步 ...
- 用js来实现那些数据结构(数组篇01)
在开始正式的内容之前,不得不说说js中的数据类型和数据结构,以及一些比较容易让人混淆的概念.那么为什么要从数组说起?数组在js中是最常见的内存数据结构,数组数据结构在js中拥有很多的方法,很多初学者记 ...
- 数组Array和列表集合ArrayList、LinkedList和Vector的区别
一.ArrayList和Vector的区别 ArrayList与Vector主要从以下方面来说. 1.同步性: Vector是线程安全的,也就是说是同步的,而ArrayList是线程序不安全的,不是同 ...
- es6 --数组--Array.from() 、Array.isArray()、Array.of()、find()、findIndex()、fill()、entries() 、keys() ,values()
将两类对象转为真正的数组 Array.from()方法用于将两类对象转为真正的数组:类似数组的对象(array-like object)和可遍历(iterable)的对象(包括ES6新增的数据结构Se ...
- JS数组array常用方法
JS数组array常用方法 1.检测数组 1)检测对象是否为数组,使用instanceof 操作符 if(value instanceof Array) { //对数组执行某些操作 } 2)获取对象的 ...
随机推荐
- C++ 常见问题
1:保证编译后方法名不被修改: The: extern "C" { function declarations here in h file } will disable C++ ...
- window.resizeTo()和window.open()
函数:window.resizeTo(width, height) 作用:改变窗口大小到设定的宽和高 参数:width - 宽度像素,必须设定的参数 height - 高度像素,可 ...
- getSharedPreferences()与getSharedPreferences()与getDefaultSharedPreferences()的区别
http://blog.csdn.net/ah200614435/article/details/7869681 一直迷惑于这三个方法的关系,最近忙完项目,好好的分析一下. 如果你熟悉Context那 ...
- DataList分页-增加自动编号列
<asp:DataList ID="dl_XUDAXIA" runat="server"> <HeaderTemplate> <t ...
- ORACLE里的自增字段设置
大家都知道吧,这很坑,尤其是用惯了mysql里的自增字段设置,结果oracle里面没有的.oh,no 我用的是12c版本的,它有一个新特性,可以这样设置自增序列,在创建表是,把id设置为自增序列 cr ...
- Linux的cat、more、less的区别
cat命令功能用于显示整个文件的内容单独使用没有翻页功能因此经常和more命令搭配使用,cat命令还有就是将数个文件合并成一个文件的功能. more命令功能:让画面在显示满一页时暂停,此时可按空格健继 ...
- nginx利用limit模块设置IP并发防CC攻击
nginx利用limit模块设置IP并发防CC攻击 分类: 系统2013-01-21 09:02 759人阅读 评论(0) 收藏 举报 来源:http://blog.xencdn.net/nginx- ...
- jQuery 标签淡入淡出 个人随笔
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/ ...
- sass用法
可能刚开始我们学习前端的时候都习惯用html+css.来做网页,但是我们发现css有很多重复的代码或者是你要改里面的图片或者文字还有去诶个的找很麻烦,所以我们就用sass来简化它. 首先我们需要安装一 ...
- ASP.NET MVC轻教程 Step By Step 8——路由
在前面的教程里,细心的你可能会有个疑问,就是地址栏输入/Home/Write就可以进入留言页面.无论是静态HTML还是ASP/ASP.NET.PHP,URL都是和某个页面相关.比如假设有个URL是“w ...