顺序数组 C语言数据结构之顺序数组的实现

软件发布|下载排行|最新软件

当前位置:首页IT学院IT技术

顺序数组 C语言数据结构之顺序数组的实现

  2021-03-29 我要评论
想了解C语言数据结构之顺序数组的实现的相关内容吗,在本文为您仔细讲解顺序数组的相关知识和一些Code实例,欢迎阅读和指正,我们先划重点:数据结构数组,C语言数据结构之顺序数组的实现,下面大家一起来学习吧。

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("输出数组各维度的值:\n "); 
  p = A.bounds; 
  for (i = 0; i < dim; ++i) 
  { 
    printf("A.bounds[%d] = %d\n ", i, *(p + i)); 
  } 
  printf("\n"); 
 
  /* 顺序输出A.constants */ 
  printf("输出数组映像函数常量基数的值(相当于每一维度的权重值):\n "); 
  p = A.constants; 
  for (i = 0; i < dim; ++i) 
  { 
    printf("A.constants[%d] = %d\n ", i, *(p + i)); 
  } 
  printf("\n\n"); 
 
  printf("%d页%d行%d列矩阵元素如下:\n", bound1, bound2, bound3); 
  for (i = 0; i < bound1; ++i) 
  { 
    printf("第%d页:\n", 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("\n"); 
    } 
    printf("\n"); 
  } 
 
  p1 = A.base; 
  printf("顺序输出Array的元素\n"); 
  for (i = 0; i < bound1*bound2*bound3; ++i) /* 顺序输出A.base */ 
  { 
    printf("%4d", *(p1 + i)); 
    //输出换行 
    if (i % (bound2*bound3) == (bound2*bound3 - 1)) 
    { 
      printf("\n"); 
    } 
  } 
 
  /* 销毁数组A */ 
  DestroyArray(&A); 
} 

运行结果如下图所示:

如有疑问请留言或者到本站社区交流讨论,感谢阅读,希望能帮助到大家,谢谢大家对本站的支持!

猜您喜欢

Copyright 2022 版权所有 软件发布 访问手机版

声明:所有软件和文章来自软件开发商或者作者 如有异议 请与本站联系 联系我们