第三周项目1 顺序表得基本运算(1)

来源:互联网 发布:移动网络解锁 编辑:程序博客网 时间:2024/06/05 04:46
/* *Copyright (c) 2015,烟台大学计算机学院 *All rights reserved. *文件名称:test.cpp *作者:朱希康 *完成日期:2015年9月18日 *版本号:vc++6.0 * *问题描述:线性表 *输入描述:无 *程序输出:几个整数*/(1)建立一个线性表,并将其输出。
#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);}


知识点总结:通过建立线性表实现输出线性表的内容,这个方法把线性表的算法转换成程序,了解算法的基本运算。
0 0
原创粉丝点击