第三周项目一2

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