项目名称 数据结构 顺序表的基本运算
作者 吴玉然
完成日期 2017 10 18
问题描述 目的是要测试“建立线性表”的算法CreateList,为查看建表的结果,需要实现“输出线性表”的算法DispList。在研习DispList中发现,要输出线性表,还要判断表是否为空,这样,实现判断线性表是否为空的算法ListEmpty成为必要。这样,再加上main函数,这个程序由4个函数构成
代码
#include "stdio.h"
#include "malloc.h"
#define MaxSize 50
typedef int ElemType;
typedef struct
{
ElemType data[MaxSize];
int length;
}SqList;
//创建线性表
void CreateList(SqList *&L,ElemType a[],int n)
{
L=(SqList *)malloc(sizeof(SqList));//分配存放线性表的空间
for(int i=0;i<n;i++)
L->data[i]=a[i];
L->length=n;
}
//判断线性表是否为空
bool EmptyList(SqList *L)
{
if(L->length==0)
return true;
else
return false;
}
//输出线性表
void DispList(SqList *L)
{
int i=0;
for(i=0;i<L->length;i++)
printf("%d ",L->data[i]);
printf("\n");
}
int main()
{
SqList *sq;
ElemType a[6]={1,2,3,4,5,6};
ElemType x;
CreateList(sq,a,6);
DispList(sq);
return 0;
}