第三周 顺序表的基本运算

来源:互联网 发布:网络支付系统 编辑:程序博客网 时间:2024/05/18 11:24

问题及代码:

[cpp] view plain copy
print?
  1. /*   
  2. *所在学校:烟台大学计算机与控制工程学院   
  3.    
  4. *文件名称:xxy3.cpp   
  5.    
  6. *作    者:李浩南  
  7.    
  8. *完成日期:2017年9月17日   
  9.    
  10. *问题描述:顺序表的基本运算   
  11.    
  12. */   
  13. #include <stdio.h>  
  14. #include <malloc.h>  
  15.   
  16. #define MaxSize 50    //Maxsize将用于后面定义存储空间的大小  
  17. typedef int ElemType;  //ElemType在不同场合可以根据问题的需要确定,在此取简单的int  
  18. typedef struct  
  19. {  
  20.     ElemType data[MaxSize];  //利用了前面MaxSize和ElemType的定义  
  21.     int length;  
  22. } SqList;  
  23.   
  24. //自定义函数声明部分  
  25. void CreateList(SqList *&L, ElemType a[], int n);//用数组创建线性表  
  26. void DispList(SqList *L);//输出线性表DispList(L)  
  27. bool ListEmpty(SqList *L);//判定是否为空表ListEmpty(L)  
  28.   
  29. //实现测试函数  
  30. int main()  
  31. {  
  32.     SqList *sq;  
  33.     ElemType x[6]= {5,8,7,2,4,9};  
  34.     CreateList(sq, x, 6);  
  35.     DispList(sq);  
  36.     return 0;  
  37. }  
  38.   
  39. //下面实现要测试的各个自定义函数  
  40. //用数组创建线性表  
  41. void CreateList(SqList *&L, ElemType a[], int n)  
  42. {  
  43.     int i;  
  44.     L=(SqList *)malloc(sizeof(SqList));  
  45.     for (i=0; i<n; i++)  
  46.         L->data[i]=a[i];  
  47.     L->length=n;  
  48. }  
  49.   
  50. //输出线性表DispList(L)  
  51. void DispList(SqList *L)  
  52. {  
  53.     int i;  
  54.     if (ListEmpty(L))  
  55.         return;  
  56.     for (i=0; i<L->length; i++)  
  57.         printf("%d ",L->data[i]);  
  58.     printf("\n");  
  59. }  
  60.   
  61. //判定是否为空表ListEmpty(L)  
  62. bool ListEmpty(SqList *L)  
  63. {  
  64.     return(L->length==0);  
  65. }  

运行结果:

知识点总结:

运用了数组和链表实现了顺序表基本算法的编写

学习心得:

用顺序表的基本算法编写了程序,达到了目的,懂得了数组与链表之间的关系,能够对顺序列表的编写更加流畅。


阅读全文
原创粉丝点击