【项目1-顺序表的基本运算】

(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;
}

运行结果:


学习心得:

通过自己的编写,加深了对顺序表的印象与理解。

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值