第三周项目一(2)

来源:互联网 发布:matlab for mac 2014b 编辑:程序博客网 时间:2024/06/05 20:29
  1. /* 
  2. 烟台大学计算机学院 
  3.  
  4. 文件名称:xm1.cpp 
  5.  
  6. 作者:李恩
  7.  
  8. 完成日期:2017年9月20日 
  9.  
  10. 问题描述:顺序表建立,查找 
  11.  
  12. 输入描述:无 
  13.  
  14. 输出描述:顺序表元素,顺序表位置 
  15.  
  16. */  
  17.   
  18.   
  19.   
  20. #include <stdio.h>  
  21. #include <malloc.h>  
  22.   
  23. #define MaxSize 50//存储空间大小宏定义  
  24.   
  25.   
  26. typedef int ElemType;  //定义ElemType为int  
  27. typedef struct  
  28. {  
  29.     ElemType data[MaxSize];  //利用了前面MaxSize和ElemType的定义  
  30.     int length;  
  31. } SqList;  
  32.   
  33. void CreateList(SqList *&L, ElemType a[], int n);//用数组创建线性表  
  34. void DispList(SqList *L);//输出线性表DispList(L)  
  35. bool ListEmpty(SqList *L);//判定是否为空表ListEmpty(L)  
  36. int ListLength(SqList *L); //求线性表的长度ListLength(L)  
  37. bool GetElem(SqList *L,int i,ElemType &e); //求某个数据元素值GetElem(L,i,e)  
  38. int LocateElem(SqList *L, ElemType e); //按元素值查找LocateElem(L,e)  
  39.   
  40. int main()//主函数  
  41. {  
  42.      SqList *sq;  
  43.     ElemType x[6]= {1,2,3,4,5,6};  
  44.     ElemType a;  
  45.     ElemType b;  
  46.     CreateList(sq, x, 6);  
  47.     DispList(sq);  
  48.     printf("表长度:%d\n", ListLength(sq));  
  49.     if(GetElem(sq,4,a))//测试在范围内  
  50.     {  
  51.         printf("顺序表第4个元素:%d \n",a);  
  52.     }  
  53.   
  54.     else  
  55.     {  
  56.   
  57.         printf("第4个元素超出范围\n");  
  58.     }  
  59.   
  60.     if(GetElem(sq, 3, a))  //测试不在范围内的情形  
  61.         printf("找到了第3个元素值为:%d\n", a);  
  62.     else  
  63.         printf("第3个元素超出范围!\n");  
  64.   
  65.         if(b=LocateElem(sq,3)>0)//测试找到  
  66.         {  
  67.                printf("值为3的元素是第 %d 个\n",b);  
  68.   
  69.   
  70.         }  
  71.         else  
  72.         {  
  73.              printf("值为8的元素木有找到!\n");  
  74.         }  
  75.        if((b=LocateElem(sq, 17))>0)  //测试不能找到的  
  76.         printf("找到了,值为17的元素是第 %d 个\n", b);  
  77.     else  
  78.         printf("值为17的元素没有找到!\n");  
  79.   
  80.         return 0;  
  81.   
  82.   
  83.   
  84.   
  85.   
  86.   
  87.   
  88.   
  89. }  
  90.   
  91. void CreateList(SqList *&L, ElemType a[], int n)  
  92. {  
  93.     int i;  
  94.     L=(SqList *)malloc(sizeof(SqList));  
  95.     for (i=0; i<n; i++)  
  96.         L->data[i]=a[i];  
  97.     L->length=n;  
  98. }//创建线性表  
  99.   
  100. void DispList(SqList *L)  
  101. {  
  102.     int i;  
  103.     if (ListEmpty(L))  
  104.         return;  
  105.     for (i=0; i<L->length; i++)  
  106.         printf("%d ",L->data[i]);  
  107.     printf("\n");  
  108. }//输出线性表  
  109.   
  110. bool ListEmpty(SqList *L)  
  111. {  
  112.     return(L->length==0);  
  113. }//判断为空表  
  114. //求线性表的长度ListLength(L)  
  115. int ListLength(SqList *L)  
  116. {  
  117.     return(L->length);  
  118. }  
  119.   
  120. //求某个数据元素值GetElem(L,i,e)  
  121. bool GetElem(SqList *L,int i,ElemType &e)  
  122. {  
  123.     if (i<1 || i>L->length)  
  124.         return false;  
  125.     e=L->data[i-1];  
  126.     return true;  
  127. }  
  128.   
  129. //按元素值查找LocateElem(L,e)  
  130. int LocateElem(SqList *L, ElemType e)  
  131. {  
  132.     int i=0;  
  133.     while (i<L->length && L->data[i]!=e) i++;  
  134.     if (i>=L->length)  
  135.         return 0;  
  136.     else  
  137.         return i+1;  
  138. }  
原创粉丝点击