数组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的更多相关文章

  1. 算法与数据结构基础 - 数组(Array)

    数组基础 数组是最基础的数据结构,特点是O(1)时间读取任意下标元素,经常应用于排序(Sort).双指针(Two Pointers).二分查找(Binary Search).动态规划(DP)等算法.顺 ...

  2. 表示集合的数据结构:数组(Array),对象(Object),Map和Set

    Map和Set是ES6标准新增的数据类型 Map: 是一组键值对的结构,使用一个二维数组来初始化Map,例如: var m = new Map([['xiaohong',100],['xiaolan' ...

  3. 我理解的数据结构(一)—— 数组(Array)

    我理解的数据结构(一)-- 数组(Array) 首先,我是一个phper,但是毕竟php是一个脚本语言,如果使用脚本语言去理解数据结构具有一定的局限性.因为脚本语言是不需要编译的,如果你的语法写的不错 ...

  4. javascript数组array

    注意:1.array的length不是只读的.可以从数组的末尾移出项或者向数组中添加新项.看下面例子: var colors = ["red","yellow" ...

  5. Java ArrayList和Vector、LinkedList与ArrayList、数组(Array)和列表集合(ArrayList)的区别

    ArrayList和Vector的区别ArrayList与Vector主要从二方面来说.  一.同步性:   Vector是线程安全的,也就是说是同步的,而ArrayList是线程序不安全的,不是同步 ...

  6. 用js来实现那些数据结构(数组篇01)

    在开始正式的内容之前,不得不说说js中的数据类型和数据结构,以及一些比较容易让人混淆的概念.那么为什么要从数组说起?数组在js中是最常见的内存数据结构,数组数据结构在js中拥有很多的方法,很多初学者记 ...

  7. 数组Array和列表集合ArrayList、LinkedList和Vector的区别

    一.ArrayList和Vector的区别 ArrayList与Vector主要从以下方面来说. 1.同步性: Vector是线程安全的,也就是说是同步的,而ArrayList是线程序不安全的,不是同 ...

  8. es6 --数组--Array.from() 、Array.isArray()、Array.of()、find()、findIndex()、fill()、entries() 、keys() ,values()

    将两类对象转为真正的数组 Array.from()方法用于将两类对象转为真正的数组:类似数组的对象(array-like object)和可遍历(iterable)的对象(包括ES6新增的数据结构Se ...

  9. JS数组array常用方法

    JS数组array常用方法 1.检测数组 1)检测对象是否为数组,使用instanceof 操作符 if(value instanceof Array) { //对数组执行某些操作 } 2)获取对象的 ...

随机推荐

  1. php读取操作大文件

    在php中,对于文件的读取时,最快捷的方式莫过于使用一些诸如file.file_get_contents之类的函数,简简单单的几行代码就能 很漂亮的完成我们所需要的功能.但当所操作的文件是一个比较大的 ...

  2. php之常量小见

    php设置常量有二种方法,一为define(),二为使用关键字const. define()函数带有三个形参,一为常量名,通常以大写字母命名,二为值,三为是否对大小写敏感,其值为可选,默认为false ...

  3. JS1 js获取dom元素方法

     js获取dom元素方法  1.通过ID选取元素(getElementById) 1)使用方法:document.getElementById("domId")         其 ...

  4. JQuery Datatables(一)

    最近项目中用了Bootstrap的样式风格,控件用了JQuery Datatables,主要有几下几点目标: 实现BootStrap风格的table,使用Ajax获取数据,并有勾选项 可以实现全选,单 ...

  5. C# DbHelperSQLite,SQLite数据库帮助类 (转载)

    主要功能如下数据访问抽象基础类 主要是访问SQLite数据库主要实现如下功能 .数据访问基础类(基于SQLite),主要是用来访问SQLite数据库的. .得到最大值:是否存在:是否存在(基于SQLi ...

  6. ViewPage实现幻灯广告墙

        ViewPage实现幻灯广告墙 功能简介 类似幻灯片功能 自动切换图片 触摸停止切换图片 自带标题功能和切换圆点 核心功能实现方法 1. 布局 <LinearLayout xmlns:a ...

  7. Local IIS 7.0 - CS0016: Could not write to output file / Microsoft.Net > Framework > v4.0.30319 > Temporary ASP.NET Files

    This week I went nuts over my local IIS. I have never swore to a machine that much in my whole life. ...

  8. java.lang.InstantiationError: sun.net.ftp.FtpClient

    发送邮件功能.本地可以,测试环境上报错.是JDK  版本导致的,,本地1.6   测试环境JDK  1.7 解决办法: 1.测试环境重新配置jdk  1.6 环境.. 2.安装  JDK  1.7  ...

  9. 使用oracle的SQL Developer创建用户的方法

    这里针对的是Oracle11g,其它版本的没测试过不知道. 首先,使用system建立连接,密码是你安装时指定的那个密码,然后在最下面的“其它用户”中创建一个新用户. 注意1:创建完毕后一定要注意要在 ...

  10. 【转】怎样创建一个Xcode插件(Part 1)

      原文:How To Create an Xcode Plugin: Part 1/3 原作者:Derek Selander 译者:@yohunl 译者注:原文使用的是xcode6.3.2,我翻译的 ...