第3周项目4-顺序表应用问题(2)

来源:互联网 发布:js修改字体颜色 编辑:程序博客网 时间:2024/06/07 02:50

问题及代码:

/* *Copyright (c)2016,烟台大学计算机与控制工程学院 *All rights reserved. *文件名称:项目4.cbp *作    者:陈光辉 *完成日期:2016年9月18日 *版 本 号:v1.0 * *问题描述:将所在奇数移到所有偶数的前面,要求算法的时间复杂度为O(n),空间复杂度为O(1)。 *输入描述:无 *程序输出:输出调整后的线性表 */  

list.h

  1. #ifndef LIST_H_INCLUDED  
  2. #define LIST_H_INCLUDED  
  3.   
  4. #define MaxSize 50  
  5. #include <stdio.h>  
  6. #include <malloc.h>  
  7. typedef int ElemType;  
  8. typedef struct  
  9. {  
  10.     ElemType data[MaxSize];  
  11.     int length;  
  12. } SqList;  
  13. void CreateList(SqList *&L, ElemType a[], int n);//用数组创建线性表  
  14. void InitList(SqList *&L);//初始化线性表InitList(L)  
  15. void DestroyList(SqList *&L);//销毁线性表DestroyList(L)  
  16. bool ListEmpty(SqList *L);//判定是否为空表ListEmpty(L)  
  17. int ListLength(SqList *L);//求线性表的长度ListLength(L)  
  18. void DispList(SqList *L);//输出线性表DispList(L)  
  19. bool GetElem(SqList *L,int i,ElemType &e);//求某个数据元素值GetElem(L,i,e)  
  20. int LocateElem(SqList *L, ElemType e);//按元素值查找LocateElem(L,e)  
  21. bool ListInsert(SqList *&L,int i,ElemType e);//插入数据元素ListInsert(L,i,e)  
  22. bool ListDelete(SqList *&L,int i,ElemType &e);//删除数据元素ListDelete(L,i,e)#endif // LIST_H_INCLUDED  
  23. #endif  
  • 功能函数文件list.cpp代码:
  1. #include "list.h"  
  2. //用数组创建线性表  
  3. void CreateList(SqList *&L, ElemType a[], int n)  
  4. {  
  5.     int i;  
  6.     L=(SqList *)malloc(sizeof(SqList));  
  7.     for (i=0; i<n; i++)  
  8.         L->data[i]=a[i];  
  9.     L->length=n;  
  10. }  
  11.   
  12. //初始化线性表InitList(L)  
  13. void InitList(SqList *&L)   //引用型指针  
  14. {  
  15.     L=(SqList *)malloc(sizeof(SqList));  
  16.     //分配存放线性表的空间  
  17.     L->length=0;  
  18. }  
  19.   
  20. //销毁线性表DestroyList(L)  
  21. void DestroyList(SqList *&L)  
  22. {  
  23.     L=(SqList *)malloc(sizeof(SqList));  
  24.     free(L);  
  25. }  
  26.   
  27. //判定是否为空表ListEmpty(L)  
  28. bool ListEmpty(SqList *L)  
  29. {  
  30.     return(L->length==0);  
  31. }  
  32.   
  33. //求线性表的长度ListLength(L)  
  34. int ListLength(SqList *L)  
  35. {  
  36.     return(L->length);  
  37. }  
  38.   
  39. //输出线性表DispList(L)  
  40. void DispList(SqList *L)  
  41. {  
  42.     int i;  
  43.     if (ListEmpty(L)) return;  
  44.     for (i=0; i<L->length; i++)  
  45.         printf("%d ",L->data[i]);  
  46.     printf("\n");  
  47. }  
  48.   
  49. //求某个数据元素值GetElem(L,i,e)  
  50. bool GetElem(SqList *L,int i,ElemType &e)  
  51. {  
  52.     if (i<1 || i>L->length)  return false;  
  53.     e=L->data[i-1];  
  54.     return true;  
  55. }  
  56.   
  57. //按元素值查找LocateElem(L,e)  
  58. int LocateElem(SqList *L, ElemType e)  
  59. {  
  60.     int i=0;  
  61.     while (i<L->length && L->data[i]!=e) i++;  
  62.     if (i>=L->length)  return 0;  
  63.     else  return i+1;  
  64. }  
  65.   
  66. //插入数据元素ListInsert(L,i,e)  
  67. bool ListInsert(SqList *&L,int i,ElemType e)  
  68. {  
  69.     int j;  
  70.     if (i<1 || i>L->length+1)  
  71.         return false;   //参数错误时返回false  
  72.     i--;            //将顺序表逻辑序号转化为物理序号  
  73.     for (j=L->length; j>i; j--) //将data[i..n]元素后移一个位置  
  74.         L->data[j]=L->data[j-1];  
  75.     L->data[i]=e;           //插入元素e  
  76.     L->length++;            //顺序表长度增1  
  77.     return true;            //成功插入返回true  
  78. }  
  79.   
  80. //删除数据元素ListDelete(L,i,e)  
  81. bool ListDelete(SqList *&L,int i,ElemType &e)  
  82. {  
  83.     int j;  
  84.     if (i<1 || i>L->length)  //参数错误时返回false  
  85.         return false;  
  86.     i--;        //将顺序表逻辑序号转化为物理序号  
  87.     e=L->data[i];  
  88.     for (j=i; j<L->length-1; j++) //将data[i..n-1]元素前移  
  89.         L->data[j]=L->data[j+1];  
  90.     L->length--;              //顺序表长度减1  
  91.     return true;              //成功删除返回true  
  92. }  
  • 测试文件main.cpp代码:
  1. #include "list.h"  
  2. #include <stdio.h>  
  3.   
  4. //移动结束后,奇数居左,偶数居右  
  5. void move(SqList *&L)  
  6. {  
  7.     int i=0,j=L->length-1;  
  8.     ElemType tmp;  
  9.     while (i<j)  
  10.     {  
  11.         while ((i<j) && (L->data[j]%2==0))  //从右往左,找到第一个奇数(偶数就忽略不管)  
  12.             j--;  
  13.         while ((i<j) && (L->data[i]%2==1))  //从左往右,找到第一个偶数(奇数就忽略不管)  
  14.             i++;  
  15.         if (i<j)   //如果未到达“分界线”,将右边的奇数和左边的偶数交换  
  16.         {  
  17.             tmp=L->data[i];  
  18.             L->data[i]=L->data[j];  
  19.             L->data[j]=tmp;  
  20.         }  
  21.     }   //待循环上去后,继续查找,并在必要时交换  
  22. }  
  23.   
  24.   
  25. //用main写测试代码  
  26. int main()  
  27. {  
  28.     SqList *sq;  
  29.     ElemType a[12]= {2,0,1,4,5,8,5,0,6,1,1,4};  
  30.     CreateList(sq, a, 12);  
  31.     printf("操作前 ");  
  32.     DispList(sq);  
  33.   
  34.     move(sq);  
  35.   
  36.     printf("操作后 ");  
  37.     DispList(sq);  
  38.     return 0;  
  39. }

知识点总结:

对move函数的实现是这个程序的关键,考察了我们对具体问题的求解方法。


学习心得:

“分界线”的理解刚刚开始是困难的,但是我还是在纸上模拟程序运行结果,这个方法有助于我理解了程序。



0 0