第3周 项目1 —顺序表的基本运算

  1. <span><span class="comment"></span></span><pre class="cpp" name="code">/*    
  2. *烟台大学计算机与控制工程学院    
  3. *作    者:宋雨静     
  4. *语    言:c/c++   
  5. *完成日期:2015年9月16日     
  6. *版 本 号:vc6.0     
  7. */    

 
[cpp]   view plain  copy
  1. <span><span class="comment">问题及代码:</span></span>  
[cpp]   view plain  copy
  1. (1)目的是要测试“建立线性表”的算法CreateList,为查看建表的结果,需要实现“输出线性表”的算法DispList。在研习DispList中发现,要输出线性表,还要判断表是否为空,这样,实现判断线性表是否为空的算法ListEmpty成为必要。这样,再加上main函数,这个程序由4个函数构成。main函数用于写测试相关的代码。  
[cpp]   view plain  copy
  1. #include <stdio.h>  
  2. #include <malloc.h>  
  3.   
  4. #define MaxSize 50    //Maxsize将用于后面定义存储空间的大小  
  5. typedef int ElemType;  //ElemType在不同场合可以根据问题的需要确定,在此取简单的int  
  6. typedef struct  
  7. {  
  8.     ElemType data[MaxSize];  //利用了前面MaxSize和ElemType的定义  
  9.     int length;  
  10. } SqList;  
  11.   
  12. //自定义函数声明部分  
  13. void CreateList(SqList *&L, ElemType a[], int n);//用数组创建线性表  
  14. void DispList(SqList *L);//输出线性表DispList(L)  
  15. bool ListEmpty(SqList *L);//判定是否为空表ListEmpty(L)  
  16. //实现测试函数  
  17. int main()  
  18. {  
  19.     SqList *sq;  
  20.     ElemType x[6]= {5,8,7,2,4,9};  
  21.     CreateList(sq, x, 6);  
  22.     DispList(sq);  
  23.     return 0;  
  24. }  
  25.   
  26. //下面实现要测试的各个自定义函数  
  27. //用数组创建线性表  
  28. void CreateList(SqList *&L, ElemType a[], int n)  
  29. {  
  30.     int i;  
  31.     L=(SqList *)malloc(sizeof(SqList));  
  32.     for (i=0; i<n; i++)  
  33.         L->data[i]=a[i];  
  34.     L->length=n;  
  35. }  
  36.   
  37. //输出线性表DispList(L)  
  38. void DispList(SqList *L)  
  39. {  
  40.     int i;  
  41.     if (ListEmpty(L))  
  42.         return;  
  43.     for (i=0; i<L->length; i++)  
  44.         printf("%d ",L->data[i]);  
  45.     printf("\n");  
  46. }  
  47.   
  48. //判定是否为空表ListEmpty(L)  
  49. bool ListEmpty(SqList *L)  
  50. {  
  51.     return(L->length==0);  
  52. }  
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值