數據結構之數組Array實例詳解。本站提示廣大學習愛好者:(數據結構之數組Array實例詳解)文章只能為提供參考,不一定能成為您想要的結果。以下是數據結構之數組Array實例詳解正文
投稿:lqh
這篇文章主要介紹了數據結構之數組Array實例詳解的相關資料,需要的朋友可以參考下數據結構之數組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=1;
const int ERROR=0;
const int INFEASIBLE=-1;
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; //假設數組維數的最大值為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<1 || dimm>MAX_ARRAY_DIM)return ERROR;
dim=dimm;
bounds=(int *)malloc(dim*sizeof(int));
if(!bounds)exit(OVERFLOW);//若各維長度合法,則存入A.bounds,並求出A的元素總數elemtotal
elemtotal=1;
va_start(ap,dim); //ap為va_list類型,是存放變長參量數表信息的數組
for(i=0;i<dim;i++)
{
bounds[i]=va_arg(ap,int);
if(bounds[i]<0)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-1]=1; //L=1,指針的增減以元素的大小為單位
for(i=dim-2;i>=0;i--)
constants[i]=bounds[i+1]*constants[i+1];
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=0;
for(i=0;i<dim;i++)
{
ind=va_arg(ap,int);
if(ind<0 || 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))<=0)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))<=0)return result;
*(base+off)=e;
return OK;
}//Assign
}Array;
void ArrayCheck()//代碼正誤檢驗
{
int i,j,k;
Array A;
ElemType e;
A.InitArray(3,2,3,2);
printf("維度:%d\n總元素個數:%d\n各維維界:",A.dim,A.elemtotal);
for(i=0;i<A.dim;i++)
printf("%d ",A.bounds[i]);
puts("");
for(i=0;i<A.bounds[0];i++)
for(j=0;j<A.bounds[1];j++)
for(k=0;k<A.bounds[2];k++)
A.Assign(i*100+j*10+k+111,i,j,k);
for(i=0;i<A.bounds[0];i++,puts(""))
for(j=0;j<A.bounds[1];j++,puts(""))
for(k=0;k<A.bounds[2];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 0;
}
感謝閱讀,希望能幫助到大家,謝謝大家對本站的支持!