数据结构(基本运算验证性实践路线)——顺序表

来源:互联网 发布:linux echo -e命令用法 编辑:程序博客网 时间:2024/06/01 21:15

/*
  *Copyright (c) 2015 烟台大学计算机与控制工程学院
  *All right reserved.
  *文件名称:list.cpp

  *writer:罗海员

  *date:2015年9月15日
  *版本:V1.0.1
  *操作系统:XP
  *运行环境:VC6.0
  *问题描述:1.目的是要测试“建立线性表”的算法CreateList,为查看建表的结果,需要实现“输出线性表”的算法DispList。
            2.在研习DispList中发现,要输出线性表,还要判断表是否为空,这样,实现判断线性表是否为空的算法ListEmpty成为必要。
   3.再加上main函数,这个程序由4个函数构成。main函数用于写测试相关的代码。

  *输入描述:判断表是否为空,实现判断表是否为空的算法ListEmpty。
  *算法库包括:
       1.包含定义顺序表数据结构的代码、宏定义、要实现算法的函数的声明;
       2.main函数,包括对函数的测试,和函数的调用(测试程序)
         3.用数组创建线性表CreateList(L)
     输出线性表DispList(L)
           判定是否为空表ListEmpty(L)
  *程序输出:需要实现“输出线性表”的算法DisList
*/

 

<span style="font-size:12px;"><strong>#include <stdio.h>#include <malloc.h>#define MaxSize 50    //Maxsize将用于后面定义存储空间的大小typedef int ElemType;  //ElemType在不同场合可以根据问题的需要确定,在此取简单的inttypedef struct{    ElemType data[MaxSize];  //利用了前面MaxSize和ElemType的定义    int length;} SqList;//自定义函数声明部分void CreateList(SqList *&L, ElemType a[], int n);//用数组创建线性表void DispList(SqList *L);//输出线性表DispList(L)bool ListEmpty(SqList *L);//判定是否为空表ListEmpty(L)//实现测试函数int main(){    SqList *sq;    ElemType x[6]= {5,8,7,2,4,9};    CreateList(sq, x, 6);    DispList(sq);    return 0;}//下面实现要测试的各个自定义函数//用数组创建线性表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)bool ListEmpty(SqList *L){    return(L->length==0);}</strong></span>

 

运行结果如下:


0 0