【数据结构】数组与广义表

63 阅读4分钟

携手创作,共同成长!这是我参与「掘金日新计划 · 8 月更文挑战」的第16天,点击查看活动详情 >>

数组

数组其实与顺序表、链表、栈、队列一样,存储了一对一的对应关系。在数组中可以存储顺序表、链表等数据结构,这就表明数组里面可以嵌套数组。

数组可以分为一维数组、二维数组、n维数组等。无论数组的维数多少,数组中存储的数据类型都一致。

数组的存储

数组作为线性存储结构,因此数组的存储实现是顺序存储结构,数组操作一般是查询与修改,对数组进行操作是很慢的效率方面。

  • 在多维数组中我们常用的就是二维数组,数组的存储有两种方式存储
    • 先列后行
    • 先行后列(c语言常用这个方式)

ex:多维数组查找指定元素

  • 我们需要知道以下信息
    • 数组的存储方式是先行后列还是先列后行
    • 在内存中的起始地址
    • 该元素的坐标
    • 元素的数据类型

代码实现:

#include<stdarg.h>
#include<malloc.h>
#include<stdio.h>
#include<stdlib.h> // atoi()
#include<io.h> // eof()
#include<math.h>

#define TRUE 1
#define FALSE 0
#define OK 1
#define ERROR 0
#define INFEASIBLE -1
#define OVERFLOW 3
#define UNDERFLOW 4
typedef int Status; //Status是函数的类型,其值是函数结果状态代码,如OK等
typedef int Boolean; //Boolean是布尔类型,其值是TRUE或FALSE
typedef int ElemType;

#define MAX_ARRAY_DIM 8 //假设数组维数的最大值为8
typedef struct
{
    ElemType *base; //数组元素基址,由InitArray分配
    int dim; //数组维数
    int *bounds; //数组维界基址,由InitArray分配
    int *constants; // 数组映象函数常量基址,由InitArray分配
} Array;

Status InitArray(Array *A,int dim,...)
{
    //若维数dim和各维长度合法,则构造相应的数组A,并返回OK
    int elemtotal=1,i; // elemtotal是元素总值
    va_list ap;
    if(dim<1||dim>MAX_ARRAY_DIM)
        return ERROR;
    (*A).dim=dim;
    (*A).bounds=(int *)malloc(dim*sizeof(int));
    if(!(*A).bounds)
        exit(OVERFLOW);
    va_start(ap,dim);
    for(i=0; i<dim; ++i)
    {
        (*A).bounds[i]=va_arg(ap,int);
        if((*A).bounds[i]<0)
            return UNDERFLOW;
        elemtotal*=(*A).bounds[i];
    }
    va_end(ap);
    (*A).base=(ElemType *)malloc(elemtotal*sizeof(ElemType));
    if(!(*A).base)
        exit(OVERFLOW);
    (*A).constants=(int *)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;
}
Status DestroyArray(Array *A)
{
    //销毁数组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;
}
Status Locate(Array A,va_list ap,int *off) // Value()、Assign()调用此函数 */
{
    //若ap指示的各下标值合法,则求出该元素在A中的相对地址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;
}
Status Value(ElemType *e,Array A,...) //在VC++中,...之前的形参不能是引用类型
{
    //依次为各维的下标值,若各下标合法,则e被赋值为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;
}
Status Assign(Array *A,ElemType e,...)
{
    //依次为各维的下标值,若各下标合法,则将e的值赋给A的指定的元素
    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;
}

int main()
{
    Array A;
    int i,j,k,*p,dim=3,bound1=3,bound2=4,bound3=2; //a[3][4][2]数组
    ElemType e,*p1;
    InitArray(&A,dim,bound1,bound2,bound3); //构造3*4*2的3维数组A
    p=A.bounds;
    printf("A.bounds=");
    for(i=0; i<dim; i++) //顺序输出A.bounds
        printf("%d ",*(p+i));
    p=A.constants;
    printf("\nA.constants=");
    for(i=0; i<dim; i++) //顺序输出A.constants
        printf("%d ",*(p+i));
    printf("\n%d页%d行%d列矩阵元素如下:\n",bound1,bound2,bound3);
    for(i=0; i<bound1; 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("A.base=\n");
    for(i=0; i<bound1*bound2*bound3; i++) //顺序输出A.base
    {
        printf("%4d",*(p1+i));
        if(i%(bound2*bound3)==bound2*bound3-1)
            printf("\n");
    }
    DestroyArray(&A);
    return 0;
}

广义表

广义表又称列表也是一种线性存储结构,主要存储{1,{1,2,3}}这样的数据。

  • 原子:单个元素
  • 子表:广义表

广义表的表头与表尾

  • 表头是广义表第一个元素
  • 表尾是广义表除去第一位元素的新广义表 广义表的存储主要使用链表实现