/*
烟台大学计算机与控制工程学院
文件名称:main.cpp,1.cpp,list.h
作者:王硕
完成日期:2017年9月15日
问题描述:顺序表的基本运算(1)
*/
#include <iostream>
#include "list.h"
using namespace std;
int main()
{
SqList *sq;
ElemType x[6]= {5,8,7,2,4,9};
CreateList(sq, x, 6);
if(ListEmpty(sq))
cout<<"此线性表为空!";
else
DispList(sq);
return 0;
}
//1.cpp
#include <stdio.h>
#include <malloc.h>
#include "list.h"
//用数组创建线性表
void CreateList(SqList *&L, ElemType a[], int n)
{
int i;
L=(SqList *)malloc(sizeof(SqList));
for (i=0; i<n; i++)
L->data[i]=a[i];
L->length=n;
}
//输出线性表DispList(L)
void DispList(SqList *L)
{
int i;
if (ListEmpty(L)) return;
for (i=0; i<L->length; i++)
printf("%d ",L->data[i]);
printf("\n");
}
//判定是否为空表ListEmpty(L)
int ListEmpty(SqList *L)
{
if(L->length==0)
return 1;
else
return 0;
}
//list.h
#ifndef LIST_H_INCLUDED
#define LIST_H_INCLUDED
#define MaxSize 50
typedef int ElemType;
typedef struct
{
ElemType data[MaxSize];
int length;
} SqList;
void CreateList(SqList *&L, ElemType a[], int n);//用数组创建线性表
int ListEmpty(SqList *L);//判定是否为空表ListEmpty(L)
void DispList(SqList *L);//输出线性表DispList(L)
#endif // LIST_H_INCLUDED
运行结果:
学习心得:这次让我感受到了用顺序表的基本算法实现程序
总结感受:先编写完成基本算法,再调用搭配,很有乐趣。