- 烟台大学计算机控制学院
- 作者:张帅
- 完成日期:2017 9 20
- 问题描述:顺序表的建立和输入和输出,判断是否为空表
- 输入描述:六个数据元素
- 程序输出:线性表
- #include<iostream>
- #include <malloc.h>
- #include<cstdio>
- #define MaxSize 50
- using namespace std;
- typedef int ElemType; //ElemType在不同场合可以根据问题的需要确定,在此取简单的int
- typedef 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]= {1,2,3,4,5,6};
- 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);
- }
-
第三周-项目一(1)顺序表的基本运算
最新推荐文章于 2023-10-01 14:30:40 发布