顺序表

来源:互联网 发布:耐克淘宝旗舰店正吗 编辑:程序博客网 时间:2024/04/30 15:11
  1. /*  
  2.  * Copyright (c).2014, 烟台大学计算机学院  
  3.  * All rights reserved.  
  4.  *文件名称:test.cpp  
  5.  *作    者:石超
  6.  *完成日期:2015年10月 11日  
  7.  *版 本 号:v1.0  
  8.  */   
  9. #include <stdio.h>   
  10. #include <malloc.h>   
  11.   
  12. #define MaxSize 50    //Maxsize将用于后面定义存储空间的大小  
  13. typedef int ElemType;  //ElemType在不同场合可以根据问题的需要确定,在此取简单的int  
  14. typedef struct  
  15. {  
  16.     ElemType data[MaxSize];  //利用了前面MaxSize和ElemType的定义  
  17.     int length;  
  18. } SqList;  
  19.   
  20. //自定义函数声明部分   
  21. void CreateList(SqList *&L, ElemType a[], int n);//用数组创建线性表  
  22. void DispList(SqList *L);//输出线性表DispList(L)  
  23. bool ListEmpty(SqList *L);//判定是否为空表ListEmpty(L)  
  24.   
  25. //实现测试函数   
  26. int main()  
  27. {  
  28.     SqList *sq;  
  29.     ElemType x[6]= {5,8,7,2,4,9};  
  30.     CreateList(sq, x, 6);  
  31.     DispList(sq);  
  32.     return 0;  
  33. }  
  34.   
  35. //下面实现要测试的各个自定义函数   
  36. //用数组创建线性表   
  37. void CreateList(SqList *&L, ElemType a[], int n)  
  38. {  
  39.     int i;  
  40.     L=(SqList *)malloc(sizeof(SqList));  
  41.     for (i=0; i<n; i++)  
  42.         L->data[i]=a[i];  
  43.     L->length=n;  
  44. }  
  45.   
  46. //输出线性表DispList(L)   
  47. void DispList(SqList *L)  
  48. {  
  49.     int i;  
  50.     if (ListEmpty(L))  
  51.         return;  
  52.     for (i=0; i<L->length; i++)  
  53.         printf("%d ",L->data[i]);  
  54.     printf("\n");  
  55. }  
  56.   
  57. //判定是否为空表ListEmpty(L)   
  58. bool ListEmpty(SqList *L)  
  59. {  
  60.     return(L->length==0);  
  61. }  


运行结果:

知识点总结:要求学会线性表的创立、输出及检测线性表是否为空表。

 

0 0