第三周-项目一(1)顺序表的基本运算

来源:互联网 发布:二手淘宝纸箱开槽机 编辑:程序博客网 时间:2024/06/03 18:38
  1. 烟台大学计算机控制学院
  2. 作者:张帅
  3. 完成日期:2017 9 20
  4. 问题描述:顺序表的建立和输入和输出,判断是否为空表 
  5. 输入描述:六个数据元素
  6. 程序输出:线性表 
  7. #include<iostream>  
  8. #include <malloc.h>  
  9. #include<cstdio>  
  10. #define MaxSize 50  
  11. using namespace std;  
  12. typedef int ElemType;  //ElemType在不同场合可以根据问题的需要确定,在此取简单的int  
  13. typedef struct  
  14. {  
  15.     ElemType data[MaxSize];  //利用了前面MaxSize和ElemType的定义  
  16.     int length;  
  17. } SqList;  
  18.   
  19. //自定义函数声明部分  
  20. void CreateList(SqList *&L, ElemType a[], int n);//用数组创建线性表  
  21. void DispList(SqList *L);//输出线性表DispList(L)  
  22. bool ListEmpty(SqList *L);//判定是否为空表ListEmpty(L)  
  23.   
  24. //实现测试函数  
  25. int main()  
  26. {  
  27.     SqList *sq;  
  28.     ElemType x[6]= {1,2,3,4,5,6};  
  29.     CreateList(sq, x, 6);  
  30.     DispList(sq);  
  31.     return 0;  
  32. }  
  33.   
  34. //下面实现要测试的各个自定义函数  
  35. //用数组创建线性表  
  36. void CreateList(SqList *&L, ElemType a[], int n)  
  37. {  
  38.     int i;  
  39.     L=(SqList *)malloc(sizeof(SqList));  
  40.     for (i=0; i<n; i++)  
  41.         L->data[i]=a[i];  
  42.     L->length=n;  
  43. }  
  44.   
  45. //输出线性表DispList(L)  
  46. void DispList(SqList *L)  
  47. {  
  48.     int i;  
  49.     if (ListEmpty(L))  
  50.         return;  
  51.     for (i=0; i<L->length; i++)  
  52.         printf("%d ",L->data[i]);  
  53.     printf("\n");  
  54. }  
  55.   
  56. //判定是否为空表ListEmpty(L)  
  57. bool ListEmpty(SqList *L)  
  58. {  
  59.     return(L->length==0);  
  60. }