数据结构之数组Array实例详解

2025-05-27 0 34

数据结构数组Array实例详解

数组Array

基本操作

?

1

2

3

4

5
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。

几个小程序(代码正误检验)

?

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

18

19

20

21

22

23

24

25

26

27

28

29

30

31

32

33

34

35

36

37

38

39

40

41

42

43

44

45

46

47

48

49

50

51

52

53

54

55

56

57

58

59

60

61

62

63

64

65

66

67

68

69

70

71

72

73

74

75

76

77

78

79

80

81

82

83

84

85

86

87

88

89

90

91

92

93

94

95

96

97

98

99

100

101

102

103

104

105

106

107

108

109

110

111

112

113

114

115

116

117

118

119

120

121

122

123

124

125

126

127

128

129

130

131

132

133

134

135

136

137

138

139

140

141

142

143

144

145

146

147

148

149

150
//

//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("");

}

?

1

2

3

4

5

6

7

8

9

10

11
程序结果:

维度:3

总元素个数:12

各维维界:2 3 2

111 112

121 122

131 132

211 212

221 222

231 232

主函数:

?

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

18
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;

}

感谢阅读,希望能帮助到大家,谢谢大家对本站的支持!

收藏 (0) 打赏

感谢您的支持,我会继续努力的!

打开微信/支付宝扫一扫,即可进行扫码打赏哦,分享从这里开始,精彩与您同在
点赞 (0)

声明:本站所有文章,如无特殊说明或标注,均为本站原创发布。任何个人或组织,在未征得本站同意时,禁止复制、盗用、采集、发布本站内容到任何网站、书籍等各类媒体平台。如若本站内容侵犯了原著者的合法权益,可联系我们进行处理。

快网idc优惠网 建站教程 数据结构之数组Array实例详解 https://www.kuaiidc.com/73990.html

相关文章

发表评论
暂无评论