《數(shù)據(jù)結(jié)構(gòu)(C語言版)》數(shù)組的實(shí)現(xiàn)
清華大學(xué)計(jì)算機(jī)系列教材? ?累計(jì)發(fā)行超400萬冊(cè)
嚴(yán)蔚敏 吳偉民 編著
數(shù)據(jù)結(jié)構(gòu)(C語言版)
以下是本人對(duì)該紫皮書第五章數(shù)組與廣義表中5.2節(jié)的代碼實(shí)現(xiàn),感覺這節(jié)的代碼超綱了,include了一個(gè)不知道是什么的庫,說實(shí)話下面的代碼運(yùn)行不出來,不知道哪里報(bào)錯(cuò)了,以后有時(shí)間再改吧,最近比較忙可能不一定能保證日更了
#define _CRT_SECURE_NO_WARNINGS
#include <stdio.h>
#include <stdlib.h>
#include <stdarg.h>//標(biāo)準(zhǔn)頭文件,提供宏va_start、va_arg和va_end,用于存儲(chǔ)變長參數(shù)表
#define TRUE 1
#define FALSE 0
#define OK 1
#define ERROR 0
#define INFEASIBLE -1
#define OVERFLOW -2
#define UNDERFLOW -3
/*數(shù)組特點(diǎn)結(jié)構(gòu)固定,維數(shù)和維界不變,故一般采用順序存儲(chǔ)結(jié)構(gòu)表示*/
#define MAX_ARRAY_DIM 8
typedef int Status;
typedef int ElemType;
typedef struct
{
ElemType* base;//數(shù)組元素基址,由InitArray分配
int dim;//數(shù)組維數(shù)
int* bounds;//數(shù)組維界基址,由InitArray分配
int* constants;//數(shù)組映像函數(shù)常量基址,由InitArray分配
}Array;
Status InitArray(Array& A, int dim, ...);//初始化數(shù)組
Status DestoryArray(Array& A);//銷毀數(shù)組
Status Value(Array A, ElemType& e, ...);
Status Assgin(Array& A, ElemType e, ...);
int main()
{
Array array;
int value;
int dim = 3;? //維數(shù)3?
//初始化
InitArray(array, dim, 2, 3, 4);? ? //三個(gè)維數(shù)分別為2,3,4?
printf("%d %d %d \n", array.bounds[0], array.bounds[1], array.bounds[2]);
printf("%d %d %d \n", array.constants[0], array.constants[1], array.constants[2]);
Assgin(array, 666, 1, 2, 3);
Value(array, value, 1, 2, 3);
return 0;
return 0;
}
Status InitArray(Array& A, int dim,...)//初始化數(shù)組
{
if (dim<1 || dim>MAX_ARRAY_DIM)
{
return ERROR;
}
A.dim = dim;
A.bounds = (int*)malloc(dim * sizeof(int));
if (!A.bounds)
{
exit(OVERFLOW);
}
int elemtotal = 1;
va_list ap;//ap為va_list類型,是存放變長參數(shù)表信息的數(shù)組
va_start(ap, dim);//從dim后開始讀取
for (int i = 0; i < dim; i++)//循環(huán),為bounds賦值?
{
A.bounds[i] = va_arg(ap, int);//從參數(shù)表中讀取各個(gè)維數(shù)對(duì)應(yīng)的值?
if (A.bounds[i] < 0)
{
return UNDERFLOW;
}
elemtotal *= A.bounds[i];//總數(shù)=維數(shù)對(duì)應(yīng)值的乘積值?
}
va_end(ap);//結(jié)束參數(shù)的讀取?
A.base = (ElemType*)malloc(elemtotal * sizeof(ElemType));
if (!A.base)
{
exit(OVERFLOW);
}
A.constants = (int*)malloc(elemtotal * sizeof(int));
if (!A.constants)
{
exit(OVERFLOW);
}
A.constants[dim - 1] = 1;
for (int i = dim - 2; i >= 0; i--)
{
A.constants[i] = A.bounds[i + 1] * A.constants[i + 1];
}
}
Status DestoryArray(Array& A) //銷毀數(shù)組
{
if (!A.base)?
{
return ERROR;
}
free(A.base);? ? //釋放基址空間?
A.base = NULL;? ? ?//數(shù)組元素基址置為空
if (!A.bounds)?
{
return ERROR;
}
free(A.bounds);? ? //釋放基址空間?
A.bounds = NULL;? ? ?//數(shù)組維界基址置為空
if (!A.constants)?
{
return ERROR;
}
free(A.constants);? ? //釋放基址空間?
A.constants = NULL;? ? ?//數(shù)組映像函數(shù)常量基址置為空
return OK;
}
Status Locate(Array A, va_list ap, int& off) {
//若ap指示的各下標(biāo)值合法,則求出該元素在A中相對(duì)地址off
off = 0;
for (int i = 0; i < A.dim; i++)?
{
int ind = va_arg(ap, int);
if (ind < 0 || ind >= A.bounds[i])?
{
printf("數(shù)組小標(biāo)越界\n");
return 0;
}
//公式對(duì)應(yīng)P92?
off += A.constants[i] * ind;? ? //每一維的小標(biāo)程每一維的偏移量?
}
}
//獲取元素的值?
Status Value(Array A, ElemType& e, ...)?
{
//A是n維數(shù)組,e為元素變量,隨后是n個(gè)下標(biāo)值
//若各下標(biāo)不超界,則e賦值為所指定的A的元素值
va_list ap;
int off;
Status result;
va_start(ap, e);
if ((result = Locate(A, ap, off)) <= 0)?
{
return result;
}
e = *(A.base + off);
printf("值為:%d\n", e);
return OK;
}
//為元素賦值?
Status Assgin(Array& A, ElemType e, ...)
{
//A是n維數(shù)組,e為元素變量,隨后是n個(gè)下標(biāo)值
//若各下標(biāo)不超界,則e賦值為所指定的A的元素值
va_list ap;
int off;
Status result;
va_start(ap, e);
if ((result = Locate(A, ap, off)) <= 0)?
{
return result;
}
*(A.base + off) = e;
printf("賦值為:%d成功\n", e);
return OK;
}