程序的結構如下所示:
頭文件:
#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;
}
運行結果:
學習心得:
通過自己的編寫,加深了對順序表的印象與理解。