(1)目的是要测试“建立线性表”的算法CreateList,为查看建表的结果,需要实现“输出线性表”的算法DispList。在研习DispList中发现,要输出线性表,还要判断表是否为空,这样,实现判断线性表是否为空的算法ListEmpty成为必要。这样,再加上main函数,这个程序由4个函数构成。main函数用于写测试相关的代码。
主要函数:
运行结果:
程序的结构如下所示:
头文件:
#ifndef FUKA_H_INCLUDED
#define FUKA_H_INCLUDED
#include"fuka.h"
#include <iostream>
#define MaxSize 50
typedef int ElemType;
typedef struct
{
ElemType date[MaxSize];
int length;
}SqList;
void CreateList(SqList *&L,ElemType a[],int n);
void DispList(SqList*L);
bool ListEmpty(SqList *L);
#endif // FUKA_H_INCLUDED
主要函数:
#include <iostream>
#include "fuka.h"
#include <malloc.h>
using namespace std;
void CreateList(SqList *&L,ElemType a[],int n)
{
int i;
L=(SqList *)malloc(sizeof(SqList));
for(i=0;i<n;i++)
{
L->date[i]=a[i];
}
L->length=n;
}
void DispList(SqList *L)
{
int i;
for(i=0;i<L->length;i++)
cout<<L->date[i]<<" ";
cout<<endl;
}
bool ListEmpty(SqList *L)
{
return(L->length==0);
}
Main 函数:
#include <iostream>
#include "fuka.h"
using namespace std;
int main()
{
SqList *l;
ElemType a[6]={3,4,6,8,9,5};
CreateList(l,a,6);
if(ListEmpty(l))
return 0;
else
DispList(l);
return 0;
}
运行结果:
学习心得:
通过自己的编写,加深了对顺序表的印象与理解。