C語(yǔ)言數(shù)據(jù)結(jié)構(gòu)之順序數(shù)組的實(shí)現(xiàn)
C語(yǔ)言數(shù)據(jù)結(jié)構(gòu)之順序數(shù)組的實(shí)現(xiàn)
以下為展示順序數(shù)組的示例:
1.用C語(yǔ)言實(shí)現(xiàn)的版本
#include<stdio.h> /* EOF(=^Z或F6),NULL */
#include<math.h> /* floor(),ceil(),abs() */
#include<stdlib.h> /*申請(qǐng)和釋放內(nèi)存*/
#include<stdarg.h> /*可變參數(shù)*/
#define OK 1 //成功標(biāo)志
#define ERROR 0 //錯(cuò)誤標(biāo)志
#define MAX_ARRAY_DIM 8 //數(shù)組最大維數(shù)
typedef int ElemType;
typedef int Status; /* Status是函數(shù)的類型,其值是函數(shù)結(jié)果狀態(tài)代碼,如OK等 */
typedef struct
{
ElemType *base; /* 數(shù)組元素基址,由InitArray分配 */
int dim; /* 數(shù)組維數(shù) */
intint *bounds; /* 數(shù)組維界基址,由InitArray分配 */
intint *constants; /* 數(shù)組映象函數(shù)常量基數(shù),相當(dāng)于每一維度的權(quán)重值,由InitArray分配 */
}Array;
/* 順序存儲(chǔ)數(shù)組的基本操作*/
Status InitArray(Array *A, int dim, ...)
{ /* 若維數(shù)dim和各維長(zhǎng)度合法,則構(gòu)造相應(yīng)的數(shù)組A,并返回OK */
int elemtotal = 1, i; /* elemtotal是元素總值 */
if (dim<1 || dim>MAX_ARRAY_DIM) //判斷數(shù)組維數(shù)
{
return ERROR;
}
(*A).dim = dim; /* 數(shù)組維數(shù) */
(*A).bounds = (intint *)malloc(dim*sizeof(int)); /* 數(shù)組維界基址 */
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;
}
/* 銷毀數(shù)組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指示的各下標(biāo)值合法,則求出該元素在A中的相對(duì)地址off */
/* Value()、Assign()調(diào)用此函數(shù) */
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;
}
/* ...依次為各維的下標(biāo)值,若各下標(biāo)合法,則e被賦值為A的相應(yīng)的元素值 */
Status Value(ElemType *e, Array A, ...)
{
va_list ap;
Status result;
int off;
va_start(ap, A);
if ((result = Locate(A, ap, &off)) == OVERFLOW) /* 調(diào)用Locate() */
{
return result;
}
*e = *(A.base + off);
return OK;
}
/* ...依次為各維的下標(biāo)值,若各下標(biāo)合法,則將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) /* 調(diào)用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]數(shù)組 */
ElemType e, *p1;
/* 構(gòu)造3*4*2的3維數(shù)組A */
InitArray(&A, dim, bound1, bound2, bound3);
/* 順序輸出A.bounds */
printf("輸出數(shù)組各維度的值:\n ");
p = A.bounds;
for (i = 0; i < dim; ++i)
{
printf("A.bounds[%d] = %d\n ", i, *(p + i));
}
printf("\n");
/* 順序輸出A.constants */
printf("輸出數(shù)組映像函數(shù)常量基數(shù)的值(相當(dāng)于每一維度的權(quán)重值):\n ");
p = A.constants;
for (i = 0; i < dim; ++i)
{
printf("A.constants[%d] = %d\n ", i, *(p + i));
}
printf("\n\n");
printf("%d頁(yè)%d行%d列矩陣元素如下:\n", bound1, bound2, bound3);
for (i = 0; i < bound1; ++i)
{
printf("第%d頁(yè):\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");
}
}
/* 銷毀數(shù)組A */
DestroyArray(&A);
}
運(yùn)行結(jié)果如下圖所示:

如有疑問(wèn)請(qǐng)留言或者到本站社區(qū)交流討論,感謝閱讀,希望能幫助到大家,謝謝大家對(duì)本站的支持!
- C++ 數(shù)據(jù)結(jié)構(gòu)線性表-數(shù)組實(shí)現(xiàn)
- 數(shù)據(jù)結(jié)構(gòu)之?dāng)?shù)組Array實(shí)例詳解
- JavaScript數(shù)據(jù)結(jié)構(gòu)之?dāng)?shù)組的表示方法示例
- C語(yǔ)言 數(shù)據(jù)結(jié)構(gòu)之連續(xù)存儲(chǔ)數(shù)組的算法
- Java數(shù)組模擬優(yōu)先級(jí)隊(duì)列數(shù)據(jù)結(jié)構(gòu)的實(shí)例
- PHP中使用數(shù)組實(shí)現(xiàn)堆棧數(shù)據(jù)結(jié)構(gòu)的代碼
- 數(shù)據(jù)結(jié)構(gòu)之?dāng)?shù)組翻轉(zhuǎn)的實(shí)現(xiàn)方法
相關(guān)文章
C語(yǔ)言實(shí)現(xiàn)俄羅斯方塊的六種模式詳程建議收藏
遲早一定會(huì)掛掉的俄羅斯方塊,為什么至今仍是世界游戲之王?它是怎么編寫的?本文將給大家詳細(xì)介紹六種模式的實(shí)現(xiàn),對(duì)大家的學(xué)習(xí)或工作具有一定的參考借鑒價(jià)值2022-02-02
C++實(shí)現(xiàn)LeetCode(191.位1的個(gè)數(shù))
這篇文章主要介紹了C++實(shí)現(xiàn)LeetCode(191.位1的個(gè)數(shù)),本篇文章通過(guò)簡(jiǎn)要的案例,講解了該項(xiàng)技術(shù)的了解與使用,以下就是詳細(xì)內(nèi)容,需要的朋友可以參考下2021-08-08
C++使用鏈表實(shí)現(xiàn)圖書管理系統(tǒng)
這篇文章主要介紹了C++使用鏈表實(shí)現(xiàn)圖書管理系統(tǒng),文中示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2022-03-03
C語(yǔ)言聯(lián)合體類型的實(shí)現(xiàn)
C語(yǔ)言實(shí)現(xiàn)第一次防死版掃雷游戲

