第三周项目4(2)

来源:互联网 发布:我的世界手机版火车js 编辑:程序博客网 时间:2024/06/03 12:48

问题及代码:

[cpp] view plain copy
 在CODE上查看代码片派生到我的代码片
  1. /*   
  2. * Copyright (c)2016,烟台大学计算机与控制工程学院   
  3. * All rights reserved.   
  4. * 文件名称:项目4.cpp   
  5. * 作    者:董雪  
  6. * 完成日期:2016年9月18日   
  7. * 版 本 号:v1.0    
  8. *问题描述:将所在奇数移到所有偶数的前面,要求算法的时间复杂度为O(n),空间复杂度为O(1)。  
  9. *输入描述:无     
  10. *程序输出:重新排列后的线性表    
  11. */     

头文件list.h:

[cpp] view plain copy
 在CODE上查看代码片派生到我的代码片
  1. #define MaxSize 50      
  2. #include <stdio.h>      
  3. #include <malloc.h>      
  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)    

源文件list.cpp:

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

源文件main.cpp:

[cpp] view plain copy
 在CODE上查看代码片派生到我的代码片
  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[10]= {5,8,7,0,2,4,9,6,7,3};    
  30.     CreateList(sq, a, 10);    
  31.     printf("操作前 ");    
  32.     DispList(sq);    
  33.     
  34.     move(sq);    
  35.     
  36.     printf("操作后 ");    
  37.     DispList(sq);    
  38.     return 0;    
  39. }    
运行结果截图:
知识点总结:利用算法库可以帮助我们更好的理解程序
学习心得:要经常使用新的知识去解决问题,加强理解
0 0
原创粉丝点击