Array of Data Structure
来源:互联网 发布:怎样申请多个淘宝小号 编辑:程序博客网 时间:2024/05/17 01:20
#include<stdio.h>
typedef int datatype;
typedef struct {//一个三维数组的实现,多维类似。
datatype *base;//int *类型
int index[3];//存储各维的长度
int c[3];//用于计算位移
}array;
int initarray(array *A,int b1,int b2,int b3)//初始化,开空间。
{
int elements;
if (b1 < 0 || b2 < 0 || b3 < 0)//不能成为数组的,返回0
return 0;
A->index[0] = b1;
A->index[1] = b2;
A->index[2] = b3;
elements = b1*b2*b3;//元素总个数
A->base = (datatype *)malloc(sizeof(datatype)*elements);//开空间
if (!(A->base))
return 0;
A->c[0] = b2*b3;//数学问题
A->c[1] = b3;
A->c[2] = 1;
return 1;
}
int value(array A, int i1, int i2, int i3,datatype *x)//显示某个位置元素的值
{
int off;//记录位置
if (i1 < 0 || i2 < 0 || i3 < 0 || i1 >= A.index[0] || i2 >= A.index[1] || i3 >= A.index[2])
return 0;
off = i1*A.c[0] + i2*A.c[1] + i3*A.c[2];
//printf("off=%d\n", off);
*x= *(A.base+off);//不需要乘4
return 1;
}
int assign(array *A, datatype e, int i1, int i2, int i3)//建立一个多维数组
{
int off;
if (i1 < 0 || i2 < 0 || i3 < 0 || i1 >= A->index[0] || i2 >= A->index[1] || i3 >= A->index[2])
return 0;
off = i1*A->c[0] + i2*A->c[1] + i3*A->c[2];
*(A->base + off)=e;
//printf("e=%d\n", e);
return 1;
}
int main()
{
array A;
int i;
datatype j = 1;
i=initarray(&A, 2, 2, 2);
if (i == 0)
return 0;
int a=0, b=0, c=0;
for (a=0; a < 2; a++)
for (b=0; b < 2; b++)//此处b不能省略初始化。
for (c=0; c < 2; c++)
{
i = assign(&A, j*2, a, b, c);
if (i == 0)
return 0;
++j;
}
for (a = 0; a < 2; a++)
for (b = 0; b < 2; b++)
for (c = 0; c < 2; c++)
{
i = value(A, a, b, c, &j);
if (i == 0)
return 0;
printf("%5d\n", j);
}
}
typedef int datatype;
typedef struct {//一个三维数组的实现,多维类似。
datatype *base;//int *类型
int index[3];//存储各维的长度
int c[3];//用于计算位移
}array;
int initarray(array *A,int b1,int b2,int b3)//初始化,开空间。
{
int elements;
if (b1 < 0 || b2 < 0 || b3 < 0)//不能成为数组的,返回0
return 0;
A->index[0] = b1;
A->index[1] = b2;
A->index[2] = b3;
elements = b1*b2*b3;//元素总个数
A->base = (datatype *)malloc(sizeof(datatype)*elements);//开空间
if (!(A->base))
return 0;
A->c[0] = b2*b3;//数学问题
A->c[1] = b3;
A->c[2] = 1;
return 1;
}
int value(array A, int i1, int i2, int i3,datatype *x)//显示某个位置元素的值
{
int off;//记录位置
if (i1 < 0 || i2 < 0 || i3 < 0 || i1 >= A.index[0] || i2 >= A.index[1] || i3 >= A.index[2])
return 0;
off = i1*A.c[0] + i2*A.c[1] + i3*A.c[2];
//printf("off=%d\n", off);
*x= *(A.base+off);//不需要乘4
return 1;
}
int assign(array *A, datatype e, int i1, int i2, int i3)//建立一个多维数组
{
int off;
if (i1 < 0 || i2 < 0 || i3 < 0 || i1 >= A->index[0] || i2 >= A->index[1] || i3 >= A->index[2])
return 0;
off = i1*A->c[0] + i2*A->c[1] + i3*A->c[2];
*(A->base + off)=e;
//printf("e=%d\n", e);
return 1;
}
int main()
{
array A;
int i;
datatype j = 1;
i=initarray(&A, 2, 2, 2);
if (i == 0)
return 0;
int a=0, b=0, c=0;
for (a=0; a < 2; a++)
for (b=0; b < 2; b++)//此处b不能省略初始化。
for (c=0; c < 2; c++)
{
i = assign(&A, j*2, a, b, c);
if (i == 0)
return 0;
++j;
}
for (a = 0; a < 2; a++)
for (b = 0; b < 2; b++)
for (c = 0; c < 2; c++)
{
i = value(A, a, b, c, &j);
if (i == 0)
return 0;
printf("%5d\n", j);
}
}
阅读全文
0 0
- Array of Data Structure
- Dynamic Array--Data Structure
- characteristics of data structure
- Data structure of tree
- Defination of Some Data Structure
- Thoughts of learning data structure
- Array of Structures vs Structure of Arrays
- JavaScript Build Tree Data Structure Using Array
- LeetCode Summary of Data Structure & Algorithms
- 【以太坊】Definitions of Data Structure
- Basics of Hash Table--Data Structure
- 并行化你的程序--Array of structure 与 structure of Array
- Data Structure
- Data structure
- Data Structure
- Data structure
- Data structure
- data structure
- 二叉搜索树的第K个节点
- 运算放大器分析----虚短和虚断(转载)
- Jvm知识-笔记七
- setBackground(),setBackgroundResource(),setBackgroundColor(),setBackgroundDrawable()的区别和用法
- PAT乙级1009. 说反话 (20)
- Array of Data Structure
- 2017.10.21
- 排序——选择排序(C++实现)
- Spring Boot 批量上传: The field files exceeds its maximum permitted size of 1048576 bytes.
- C++/C++11中std::runtime_error的使用
- Java格式化日期的三种方式
- leetCode Q1:Two Num(java)
- C语言:输入日期,判断是这一年的第几天?
- 总结之CSIG-MIIP 2017