C 언어 데이터 구조의 순서 배열 의 실현
다음은 순서 배열 의 예제 입 니 다.
1.C 언어 로 구현 한 버 전
#include<stdio.h> /* EOF(=^Z F6),NULL */
#include<math.h> /* floor(),ceil(),abs() */
#include<stdlib.h> /* */
#include<stdarg.h> /* */
#define OK 1 //
#define ERROR 0 //
#define MAX_ARRAY_DIM 8 //
typedef int ElemType;
typedef int Status; /* Status , , OK */
typedef struct
{
ElemType *base; /* , InitArray */
int dim; /* */
intint *bounds; /* , InitArray */
intint *constants; /* , , InitArray */
}Array;
/* */
Status InitArray(Array *A, int dim, ...)
{ /* dim , A, OK */
int elemtotal = 1, i; /* elemtotal */
if (dim<1 || dim>MAX_ARRAY_DIM) //
{
return ERROR;
}
(*A).dim = dim; /* */
(*A).bounds = (intint *)malloc(dim*sizeof(int)); /* */
if (!(*A).bounds)
{
exit(OVERFLOW);
}
va_list ap;
va_start(ap, dim);
for (i = 0; i < dim; ++i)
{
(*A).bounds[i] = va_arg(ap, int);
if ((*A).bounds[i] < 0)
{
return UNDERFLOW; /* math.h 4 */
}
elemtotal *= (*A).bounds[i];
}
va_end(ap);
(*A).base = (ElemType *)malloc(elemtotal*sizeof(ElemType));
if (!(*A).base)
{
exit(OVERFLOW);
}
(*A).constants = (intint *)malloc(dim*sizeof(int));
if (!(*A).constants)
{
exit(OVERFLOW);
}
(*A).constants[dim - 1] = 1;
for (i = dim - 2; i >= 0; --i)
{
(*A).constants[i] = (*A).bounds[i + 1] * (*A).constants[i + 1];
}
return OK;
}
/* A */
Status DestroyArray(Array *A)
{
if ((*A).base)
{
free((*A).base);
(*A).base = NULL;
}
else
{
return ERROR;
}
if ((*A).bounds)
{
free((*A).bounds);
(*A).bounds = NULL;
}
else
{
return ERROR;
}
if ((*A).constants)
{
free((*A).constants);
(*A).constants = NULL;
}
else
{
return ERROR;
}
return OK;
}
/* ap , A off */
/* Value()、Assign() */
Status Locate(Array A, va_list ap, intint *off)
{
int i, ind;
*off = 0;
for (i = 0; i < A.dim; ++i)
{
ind = va_arg(ap, int);
if (ind < 0 || ind >= A.bounds[i])
{
return OVERFLOW;
}
*off += A.constants[i] * ind;
}
return OK;
}
/* ... , , e A */
Status Value(ElemType *e, Array A, ...)
{
va_list ap;
Status result;
int off;
va_start(ap, A);
if ((result = Locate(A, ap, &off)) == OVERFLOW) /* Locate() */
{
return result;
}
*e = *(A.base + off);
return OK;
}
/* ... , , e A */
Status Assign(Array *A, ElemType e, ...)
{
va_list ap;
Status result;
int off;
va_start(ap, e);
if ((result = Locate(*A, ap, &off)) == OVERFLOW) /* Locate() */
{
return result;
}
*((*A).base + off) = e;
return OK;
}
void main()
{
Array A;
int i, j, k, *p, dim = 3, bound1 = 3, bound2 = 4, bound3 = 2; /* a[3][4][2] */
ElemType e, *p1;
/* 3*4*2 3 A */
InitArray(&A, dim, bound1, bound2, bound3);
/* A.bounds */
printf(" :
");
p = A.bounds;
for (i = 0; i < dim; ++i)
{
printf("A.bounds[%d] = %d
", i, *(p + i));
}
printf("
");
/* A.constants */
printf(" ( ):
");
p = A.constants;
for (i = 0; i < dim; ++i)
{
printf("A.constants[%d] = %d
", i, *(p + i));
}
printf("
");
printf("%d %d %d :
", bound1, bound2, bound3);
for (i = 0; i < bound1; ++i)
{
printf(" %d :
", i);
for (j = 0; j < bound2; ++j)
{
for (k = 0; k < bound3; ++k)
{
Assign(&A, i * 100 + j * 10 + k, i, j, k); /* i*100+j*10+k A[i][j][k] */
Value(&e, A, i, j, k); /* A[i][j][k] e */
printf("A[%d][%d][%d]=%2d ", i, j, k, e); /* A[i][j][k] */
}
printf("
");
}
printf("
");
}
p1 = A.base;
printf(" Array
");
for (i = 0; i < bound1*bound2*bound3; ++i) /* A.base */
{
printf("%4d", *(p1 + i));
//
if (i % (bound2*bound3) == (bound2*bound3 - 1))
{
printf("
");
}
}
/* A */
DestroyArray(&A);
}
실행 결 과 는 다음 그림 과 같 습 니 다.궁금 한 점 이 있 으 시 면 메 시 지 를 남기 거나 본 사이트 의 커 뮤 니 티 에 가서 토론 을 교류 하 세 요.읽 어 주 셔 서 감사합니다. 도움 이 되 셨 으 면 좋 겠 습 니 다.본 사이트 에 대한 지지 에 감 사 드 립 니 다!
이 내용에 흥미가 있습니까?
현재 기사가 여러분의 문제를 해결하지 못하는 경우 AI 엔진은 머신러닝 분석(스마트 모델이 방금 만들어져 부정확한 경우가 있을 수 있음)을 통해 가장 유사한 기사를 추천합니다:
C 언어 데이터 구조의 순서 배열 의 실현C 언어 데이터 구조의 순서 배열 의 실현 다음은 순서 배열 의 예제 입 니 다. 1.C 언어 로 구현 한 버 전 실행 결 과 는 다음 그림 과 같 습 니 다. 궁금 한 점 이 있 으 시 면 메 시 지 를 남기 거나...
텍스트를 자유롭게 공유하거나 복사할 수 있습니다.하지만 이 문서의 URL은 참조 URL로 남겨 두십시오.
CC BY-SA 2.5, CC BY-SA 3.0 및 CC BY-SA 4.0에 따라 라이센스가 부여됩니다.