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

来源:互联网 发布:结构化面试 知乎 编辑:程序博客网 时间:2024/04/27 13:14
  1. /* 
  2.  *Copyright (c) 2016,烟台大学计算机学院 
  3.  *All rights reserved. 
  4.  *文件名称:test.cpp 
  5.  *作者:衣龙川
  6.  *完成日期:2016年9月18日 
  7.  *版本号:vc++6.0 
  8.  * 
  9.  *问题描述:线性表 
  10.  *输入描述:无 
  11.  *程序输出:输出线性表的内容、长度、查找第n个整数 
  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. int ListLength(SqList *L); //求线性表的长度ListLength(L)  
  29. bool GetElem(SqList *L,int i,ElemType &e); //求某个数据元素值GetElem(L,i,e)  
  30. int LocateElem(SqList *L, ElemType e); //按元素值查找LocateElem(L,e)  
  31.   
  32. //实现测试函数  
  33. int main()  
  34. {  
  35.     SqList *sq;  
  36.     ElemType x[6]= {5,8,7,2,4,9};  
  37.     ElemType a;  
  38.     int loc;  
  39.     CreateList(sq, x, 6);  
  40.     DispList(sq);  
  41.   
  42.     printf("表长度:%d\n", ListLength(sq));  //测试求长度  
  43.   
  44.     if(GetElem(sq, 3, a))  //测试在范围内的情形  
  45.         printf("找到了第3个元素值为:%d\n", a);  
  46.     else  
  47.         printf("第3个元素超出范围!\n");  
  48.   
  49.     if(GetElem(sq, 15, a))  //测试不在范围内的情形  
  50.         printf("找到了第15个元素值为:%d\n", a);  
  51.     else  
  52.         printf("第15个元素超出范围!\n");  
  53.   
  54.     if((loc=LocateElem(sq, 8))>0)  //测试能找到的情形  
  55.         printf("找到了,值为8的元素是第 %d 个\n", loc);  
  56.     else  
  57.         printf("值为8的元素木有找到!\n");  
  58.   
  59.     if((loc=LocateElem(sq, 17))>0)  //测试不能找到的情形  
  60.         printf("找到了,值为17的元素是第 %d 个\n", loc);  
  61.     else  
  62.         printf("值为17的元素木有找到!\n");  
  63.   
  64.     return 0;  
  65. }  
  66.   
  67. void CreateList(SqList *&L, ElemType a[], int n)  
  68. {  
  69.     int i;  
  70.     L=(SqList *)malloc(sizeof(SqList));  
  71.     for (i=0; i<n; i++)  
  72.         L->data[i]=a[i];  
  73.     L->length=n;  
  74. }  
  75.   
  76. //输出线性表DispList(L)  
  77. void DispList(SqList *L)  
  78. {  
  79.     int i;  
  80.     if (ListEmpty(L))  
  81.         return;  
  82.     for (i=0; i<L->length; i++)  
  83.         printf("%d ",L->data[i]);  
  84.     printf("\n");  
  85. }  
  86.   
  87. //判定是否为空表ListEmpty(L)  
  88. bool ListEmpty(SqList *L)  
  89. {  
  90.     return(L->length==0);  
  91. }  
  92.   
  93. //求线性表的长度ListLength(L)  
  94. int ListLength(SqList *L)  
  95. {  
  96.     return(L->length);  
  97. }  
  98.   
  99. //求某个数据元素值GetElem(L,i,e)  
  100. bool GetElem(SqList *L,int i,ElemType &e)  
  101. {  
  102.     if (i<1 || i>L->length)  
  103.         return false;  
  104.     e=L->data[i-1];  
  105.     return true;  
  106. }  
  107.   
  108. //按元素值查找LocateElem(L,e)  
  109. int LocateElem(SqList *L, ElemType e)  
  110. {  
  111.     int i=0;  
  112.     while (i<L->length && L->data[i]!=e) i++;  
  113.     if (i>=L->length)  
  114.         return 0;  
  115.     else  
  116.         return i+1;  
  117. }  

运行结果:


 


知识点总结:通过这两个程序,由简到复杂,从基本的程序开始编写,每个功能要一步步的完成,才能做好一个大的程序。

1 0