数组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. Java 之文件IO编程 之读取

    package com.sun; /* * 这里是对文件IO流读取的操作 * 2014-08-10 */ import java.io.*; public class File_test { publ ...

  2. webrtc学习———记录三:mediaStreamTrack

    参考: https://developer.mozilla.org/en-US/docs/Web/API/MediaStreamTrack 转自http://c.tieba.baidu.com/p/3 ...

  3. FTPClient文件下载

    一.FTPClient下载文件所需要的jar包: org.apache.commons.net  [commons-net-3.4.jar] 二.FTPClient的连接和关闭 //FTPClient ...

  4. C#动态二维数组输出

    using System; using System.Collections.Generic; using System.Linq; using System.Text; namespace Cons ...

  5. JS DOM 来控制HTML元素

    JS DOM 来控制HTML元素 (ps:这个有很多方法,挑一些详解,嘻嘻) 1.getElementsByName():获取name. ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ ...

  6. React学习笔记(一) 基础知识

    现在最热门的前端框架有AngularJS.React.Bootstrap等.自从接触了ReactJS,ReactJs的虚拟DOM(Virtual DOM)和组件化的开发深深的吸引了我. React的基 ...

  7. iOS 异常汇总

    reason: 'invalid nib registered for identifier (cell) - nib must contain exactly one top level objec ...

  8. Windows phone 之Xml操作

    最近在做天气预报应用,这个应用中需要用到Xml来存储关注的城市列表,保存一下代码,方便以后使用,也给博友们一个参考: 其中:添加关注城市的操作代码是: 其实就是, (1)先把数据从CareCityCo ...

  9. ubuntu npm 私有库搭建 (npmjs.org 官方版本)

    目标 npm.xxx.com 安装和推送nodejs包 npmui.xxx.com 管理已经推送的nodejs包   安装 couchdb   https://launchpad.net/~couch ...

  10. dedecms织梦如何删除所有的文章?

    dedecms织梦如何删除所有的文章?dede一键删除所有文章的SQL命令:  DELETE FROM dede_addonarticle WHERE aid >= 1 and aid<= ...