第三周项目-4顺序表应用

来源:互联网 发布:国际网络不应该受管制 编辑:程序博客网 时间:2024/05/29 16:52
  1. /*      
  2. *烟台大学计控学院       
  3. *作    者:王智超      
  4. *完成日期:2015年9月18日   
  5. *问题描述:定义一个采用顺序结构存储的线性表,设计算法完成下面的工作:  
  6.          1、删除元素在[x, y]之间的所有元素,要求算法的时间复杂度为O(n),空间复杂度为O(1);  
  7.          2、将所在奇数移到所有偶数的前面,要求算法的时间复杂度为O(n),空间复杂度为O(1)。 
  8. */  

1.删除元素在[x, y]之间的所有元素,要求算法的时间复杂度为O(n),空间复杂度为O(1);

(1) list.h的代码

[cpp] view plain copy
  1. #include<stdio.h>  
  2. #include<malloc.h>  
  3. #define MaxSize 50  
  4. typedef int ElemType;  
  5. typedef struct  
  6. {  
  7.     ElemType data[MaxSize];  
  8.     int length;  
  9. } SqList;  
  10. void CreateList(SqList *&L, ElemType a[], int n);//用数组创建线性表  
  11. void InitList(SqList *&L);//初始化线性表InitList(L)  
  12. void DestroyList(SqList *&L);//销毁线性表DestroyList(L)  
  13. bool ListEmpty(SqList *L);//判定是否为空表ListEmpty(L)  
  14. int ListLength(SqList *L);//求线性表的长度ListLength(L)  
  15. void DispList(SqList *L);//输出线性表DispList(L)  
  16. bool GetElem(SqList *L,int i,ElemType &e);//求某个数据元素值GetElem(L,i,e)  
  17. int LocateElem(SqList *L, ElemType e);//按元素值查找LocateElem(L,e)  
  18. bool ListInsert(SqList *&L,int i,ElemType e);//插入数据元素ListInsert(L,i,e)  
  19. bool ListDelete(SqList *&L,int i,ElemType &e);//删除数据元素ListDelete(L,i,e)  
  20. void delx2y(SqList *&L, ElemType x,  ElemType y);  

(2)list.cpp中的代码

[cpp] view plain copy
  1. #include"list.h"  
  2. void delx2y(SqList *&L, ElemType x,  ElemType y)  
  3. {  
  4.     int k=0,i;//k记录非x的元素个数  
  5.     ElemType t;  
  6.     if(x>y)  
  7.     {  
  8.         t=x;  
  9.         y=x;  
  10.         y=t;  
  11.     }  
  12.     for(i=0;i<L->length;i++)  
  13.         if(L->data[i]<x||L->data[i]>y)//复制不在[x,y]之间的数  
  14.         {  
  15.             L->data[k]=L->data[i];  
  16.             k++;  
  17.         }  
  18.         L->length=k;  
  19. }  
  20.   
  21.   
  22.   
  23. //用数组创建线性表  
  24. void CreateList(SqList *&L, ElemType a[], int n)  
  25. {  
  26.     int i;  
  27.     L=(SqList *)malloc(sizeof(SqList));  
  28.     for (i=0; i<n; i++)  
  29.         L->data[i]=a[i];  
  30.     L->length=n;  
  31. }  
  32.   
  33. //初始化线性表InitList(L)  
  34. void InitList(SqList *&L)   //引用型指针  
  35. {  
  36.     L=(SqList *)malloc(sizeof(SqList));  
  37.     //分配存放线性表的空间  
  38.     L->length=0;  
  39. }  
  40.   
  41. //销毁线性表DestroyList(L)  
  42. void DestroyList(SqList *&L)  
  43. {  
  44.     free(L);  
  45. }  
  46.   
  47. //判定是否为空表ListEmpty(L)  
  48. bool ListEmpty(SqList *L)  
  49. {  
  50.     return(L->length==0);  
  51. }  
  52.   
  53. //求线性表的长度ListLength(L)  
  54. int ListLength(SqList *L)  
  55. {  
  56.     return(L->length);  
  57. }  
  58.   
  59. //输出线性表DispList(L)  
  60. void DispList(SqList *L)  
  61. {  
  62.     int i;  
  63.     if (ListEmpty(L)) return;  
  64.     for (i=0; i<L->length; i++)  
  65.         printf("%d ",L->data[i]);  
  66.     printf("\n");  
  67. }  
  68.   
  69. //求某个数据元素值GetElem(L,i,e)  
  70. bool GetElem(SqList *L,int i,ElemType &e)  
  71. {  
  72.     if (i<1 || i>L->length)  return false;  
  73.     e=L->data[i-1];  
  74.     return true;  
  75. }  
  76.   
  77. //按元素值查找LocateElem(L,e)  
  78. int LocateElem(SqList *L, ElemType e)  
  79. {  
  80.     int i=0;  
  81.     while (i<L->length && L->data[i]!=e) i++;  
  82.     if (i>=L->length)  return 0;  
  83.     else  return i+1;  
  84. }  
  85.   
  86. //插入数据元素ListInsert(L,i,e)  
  87. bool ListInsert(SqList *&L,int i,ElemType e)  
  88. {  
  89.     int j;  
  90.     if (i<1 || i>L->length+1)  
  91.         return false;   //参数错误时返回false  
  92.     i--;            //将顺序表逻辑序号转化为物理序号  
  93.     for (j=L->length; j>i; j--) //将data[i..n]元素后移一个位置  
  94.         L->data[j]=L->data[j-1];  
  95.     L->data[i]=e;           //插入元素e  
  96.     L->length++;            //顺序表长度增1  
  97.     return true;            //成功插入返回true  
  98. }  
  99.   
  100. //删除数据元素ListDelete(L,i,e)  
  101. bool ListDelete(SqList *&L,int i,ElemType &e)  
  102. {  
  103.     int j;  
  104.     if (i<1 || i>L->length)  //参数错误时返回false  
  105.         return false;  
  106.     i--;        //将顺序表逻辑序号转化为物理序号  
  107.     e=L->data[i];  
  108.     for (j=i; j<L->length-1; j++) //将data[i..n-1]元素前移  
  109.         L->data[j]=L->data[j+1];  
  110.     L->length--;              //顺序表长度减1  
  111.     return true;              //成功删除返回true  
  112. }  


(3)main.cpp的代码

[cpp] view plain copy
  1. #include"list.h"  
  2. int main()  
  3. {  
  4.     SqList *sq;  
  5.     ElemType a[10]= {5,8,7,0,2,4,9,6,7,3};  
  6.     CreateList(sq, a, 10);  
  7.     printf("删除前 ");  
  8.     DispList(sq);  
  9.   
  10.     delx2y(sq, 4, 7);  
  11.   
  12.     printf("删除后 ");  
  13.     DispList(sq);  
  14.     return 0;  
  15. }  


运行结果:

2、将所在奇数移到所有偶数的前面,要求算法的时间复杂度为O(n),空间复杂度为O(1)。

(1)move.cpp中的代码

[cpp] view plain copy
  1. #include"list.h"  
  2. void move(SqList *&L)  
  3. {  
  4.     int i=0,j=L->length-1;  
  5.     ElemType tmp;  
  6.     while (i<j)  
  7.     {  
  8.         while ((i<j) && (L->data[j]%2==0))  //从右往左,找到第一个奇数(偶数就忽略不管)  
  9.             j--;  
  10.         while ((i<j) && (L->data[i]%2==1))  //从左往右,找到第一个偶数(奇数就忽略不管)  
  11.             i++;  
  12.         if (i<j)   //如果未到达“分界线”,将右边的奇数和左边的偶数交换  
  13.         {  
  14.             tmp=L->data[i];  
  15.             L->data[i]=L->data[j];  
  16.             L->data[j]=tmp;  
  17.         }  
  18.     }   //待循环上去后,继续查找,并在必要时交换  
  19. }  

(2)main.cpp的代码

[cpp] view plain copy
  1. #include"list.h"  
  2. int main()  
  3. {  
  4.     SqList *sq;  
  5.     ElemType a[10]= {5,8,7,0,2,4,9,6,7,3};  
  6.     CreateList(sq, a, 10);  
  7.     printf("操作前 ");  
  8.     DispList(sq);  
  9.   
  10.     move(sq);  
  11.   
  12.     printf("操作后 ");  
  13.     DispList(sq);  
  14.     return 0;  
  15. }  


运行结果:


0 0