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

来源:互联网 发布:mac搜狗输入罗马数字 编辑:程序博客网 时间:2024/06/05 10:34
  1. 问题描述及代码:  
  2. [cpp] view plain copy  
  3. 1.  /*        
  4. 2.  *烟台大学计控学院         
  5. 3.  *作    者:陈晓琳      
  6. 4.  *完成日期:2016年9月20日     
  7. 5.  *问题描述:定义一个采用顺序结构存储的线性表,设计算法完成下面的工作:    
  8. 6.           1、删除元素在[x, y]之间的所有元素,要求算法的时间复杂度为O(n),空间复杂度为O(1);    
  9. 7.           2、将所在奇数移到所有偶数的前面,要求算法的时间复杂度为O(n),空间复杂度为O(1)。   
  10. 8.  */    
  11. 1.删除元素在[x, y]之间的所有元素,要求算法的时间复杂度为O(n),空间复杂度为O(1);  
  12. (1) list.h的代码  
  13. [cpp] view plain copy  
  14. 1.  #include<stdio.h>    
  15. 2.  #include<malloc.h>    
  16. 3.  #define MaxSize 50    
  17. 4.  typedef int ElemType;    
  18. 5.  typedef struct    
  19. 6.  {    
  20. 7.      ElemType data[MaxSize];    
  21. 8.      int length;    
  22. 9.  } SqList;    
  23. 10. void CreateList(SqList *&L, ElemType a[], int n);//用数组创建线性表    
  24. 11. void InitList(SqList *&L);//初始化线性表InitList(L)    
  25. 12. void DestroyList(SqList *&L);//销毁线性表DestroyList(L)    
  26. 13. bool ListEmpty(SqList *L);//判定是否为空表ListEmpty(L)    
  27. 14. int ListLength(SqList *L);//求线性表的长度ListLength(L)    
  28. 15. void DispList(SqList *L);//输出线性表DispList(L)    
  29. 16. bool GetElem(SqList *L,int i,ElemType &e);//求某个数据元素值GetElem(L,i,e)    
  30. 17. int LocateElem(SqList *L, ElemType e);//按元素值查找LocateElem(L,e)    
  31. 18. bool ListInsert(SqList *&L,int i,ElemType e);//插入数据元素ListInsert(L,i,e)    
  32. 19. bool ListDelete(SqList *&L,int i,ElemType &e);//删除数据元素ListDelete(L,i,e)    
  33. 20. void delx2y(SqList *&L, ElemType x,  ElemType y);    
  34. (2)list.cpp中的代码  
  35. [cpp] view plain copy  
  36. 1.  #include"list.h"    
  37. 2.  void delx2y(SqList *&L, ElemType x,  ElemType y)    
  38. 3.  {    
  39. 4.      int k=0,i;//k记录非x的元素个数    
  40. 5.      ElemType t;    
  41. 6.      if(x>y)    
  42. 7.      {    
  43. 8.          t=x;    
  44. 9.          y=x;    
  45. 10.         y=t;    
  46. 11.     }    
  47. 12.     for(i=0;i<L->length;i++)    
  48. 13.         if(L->data[i]<x||L->data[i]>y)//复制不在[x,y]之间的数    
  49. 14.         {    
  50. 15.             L->data[k]=L->data[i];    
  51. 16.             k++;    
  52. 17.         }    
  53. 18.         L->length=k;    
  54. 19. }    
  55. 20.     
  56. 21.     
  57. 22.     
  58. 23. //用数组创建线性表    
  59. 24. void CreateList(SqList *&L, ElemType a[], int n)    
  60. 25. {    
  61. 26.     int i;    
  62. 27.     L=(SqList *)malloc(sizeof(SqList));    
  63. 28.     for (i=0; i<n; i++)    
  64. 29.         L->data[i]=a[i];    
  65. 30.     L->length=n;    
  66. 31. }    
  67. 32.     
  68. 33. //初始化线性表InitList(L)    
  69. 34. void InitList(SqList *&L)   //引用型指针    
  70. 35. {    
  71. 36.     L=(SqList *)malloc(sizeof(SqList));    
  72. 37.     //分配存放线性表的空间    
  73. 38.     L->length=0;    
  74. 39. }    
  75. 40.     
  76. 41. //销毁线性表DestroyList(L)    
  77. 42. void DestroyList(SqList *&L)    
  78. 43. {    
  79. 44.     free(L);    
  80. 45. }    
  81. 46.     
  82. 47. //判定是否为空表ListEmpty(L)    
  83. 48. bool ListEmpty(SqList *L)    
  84. 49. {    
  85. 50.     return(L->length==0);    
  86. 51. }    
  87. 52.     
  88. 53. //求线性表的长度ListLength(L)    
  89. 54. int ListLength(SqList *L)    
  90. 55. {    
  91. 56.     return(L->length);    
  92. 57. }    
  93. 58.     
  94. 59. //输出线性表DispList(L)    
  95. 60. void DispList(SqList *L)    
  96. 61. {    
  97. 62.     int i;    
  98. 63.     if (ListEmpty(L)) return;    
  99. 64.     for (i=0; i<L->length; i++)    
  100. 65.         printf("%d ",L->data[i]);    
  101. 66.     printf("\n");    
  102. 67. }    
  103. 68.     
  104. 69. //求某个数据元素值GetElem(L,i,e)    
  105. 70. bool GetElem(SqList *L,int i,ElemType &e)    
  106. 71. {    
  107. 72.     if (i<1 || i>L->length)  return false;    
  108. 73.     e=L->data[i-1];    
  109. 74.     return true;    
  110. 75. }    
  111. 76.     
  112. 77. //按元素值查找LocateElem(L,e)    
  113. 78. int LocateElem(SqList *L, ElemType e)    
  114. 79. {    
  115. 80.     int i=0;    
  116. 81.     while (i<L->length && L->data[i]!=e) i++;    
  117. 82.     if (i>=L->length)  return 0;    
  118. 83.     else  return i+1;    
  119. 84. }    
  120. 85.     
  121. 86. //插入数据元素ListInsert(L,i,e)    
  122. 87. bool ListInsert(SqList *&L,int i,ElemType e)    
  123. 88. {    
  124. 89.     int j;    
  125. 90.     if (i<1 || i>L->length+1)    
  126. 91.         return false;   //参数错误时返回false    
  127. 92.     i--;            //将顺序表逻辑序号转化为物理序号    
  128. 93.     for (j=L->length; j>i; j--) //将data[i..n]元素后移一个位置    
  129. 94.         L->data[j]=L->data[j-1];    
  130. 95.     L->data[i]=e;           //插入元素e    
  131. 96.     L->length++;            //顺序表长度增1    
  132. 97.     return true;            //成功插入返回true    
  133. 98. }    
  134. 99.     
  135. 100.    //删除数据元素ListDelete(L,i,e)    
  136. 101.    bool ListDelete(SqList *&L,int i,ElemType &e)    
  137. 102.    {    
  138. 103.        int j;    
  139. 104.        if (i<1 || i>L->length)  //参数错误时返回false    
  140. 105.            return false;    
  141. 106.        i--;        //将顺序表逻辑序号转化为物理序号    
  142. 107.        e=L->data[i];    
  143. 108.        for (j=i; j<L->length-1; j++) //将data[i..n-1]元素前移    
  144. 109.            L->data[j]=L->data[j+1];    
  145. 110.        L->length--;              //顺序表长度减1    
  146. 111.        return true;              //成功删除返回true    
  147. 112.    }    
  148.   
  149. (3)main.cpp的代码  
  150. [cpp] view plain copy  
  151. 1.  #include"list.h"    
  152. 2.  int main()    
  153. 3.  {    
  154. 4.      SqList *sq;    
  155. 5.      ElemType a[10]= {5,8,7,0,2,4,9,6,7,3};    
  156. 6.      CreateList(sq, a, 10);    
  157. 7.      printf("删除前 ");    
  158. 8.      DispList(sq);    
  159. 9.      
  160. 10.     delx2y(sq, 4, 7);    
  161. 11.     
  162. 12.     printf("删除后 ");    
  163. 13.     DispList(sq);    
  164. 14.     return 0;    
  165. 15. }    
  166.   
  167. 运行结果:  
  168. <img src="http://img.blog.csdn.net/20160918112614980?watermark/2/text/aHR0cDovL2Jsb2cuY3Nkbi5uZXQv/font/5a6L5L2T/fontsize/400/fill/I0JBQkFCMA==/dissolve/70/gravity/Center" alt="" />  
  169. 2、将所在奇数移到所有偶数的前面,要求算法的时间复杂度为O(n),空间复杂度为O(1)。  
  170. (1)move.cpp中的代码  
  171. [cpp] view plain copy  
  172. 1.  #include"list.h"    
  173. 2.  void move(SqList *&L)    
  174. 3.  {    
  175. 4.      int i=0,j=L->length-1;    
  176. 5.      ElemType tmp;    
  177. 6.      while (i<j)    
  178. 7.      {    
  179. 8.          while ((i<j) && (L->data[j]%2==0))  //从右往左,找到第一个奇数(偶数就忽略不管)    
  180. 9.              j--;    
  181. 10.         while ((i<j) && (L->data[i]%2==1))  //从左往右,找到第一个偶数(奇数就忽略不管)    
  182. 11.             i++;    
  183. 12.         if (i<j)   //如果未到达“分界线”,将右边的奇数和左边的偶数交换    
  184. 13.         {    
  185. 14.             tmp=L->data[i];    
  186. 15.             L->data[i]=L->data[j];    
  187. 16.             L->data[j]=tmp;    
  188. 17.         }    
  189. 18.     }   //待循环上去后,继续查找,并在必要时交换    
  190. 19. }    
  191. (2)main.cpp的代码  
  192. [cpp] view plain copy  
  193. 1.  #include"list.h"    
  194. 2.  int main()    
  195. 3.  {    
  196. 4.      SqList *sq;    
  197. 5.      ElemType a[10]= {5,8,7,0,2,4,9,6,7,3};    
  198. 6.      CreateList(sq, a, 10);    
  199. 7.      printf("操作前 ");    
  200. 8.      DispList(sq);    
  201. 9.      
  202. 10.     move(sq);    
  203. 11.     
  204. 12.     printf("操作后 ");    
  205. 13.     DispList(sq);    
  206. 14.     return 0;    
  207. 15. }    
  208.   
  209. 运行结果:  
  210. <img src="http://img.blog.csdn.net/20160918112618298?watermark/2/text/aHR0cDovL2Jsb2cuY3Nkbi5uZXQv/font/5a6L5L2T/fontsize/400/fill/I0JBQkFCMA==/dissolve/70/gravity/Center" alt="" />  
  211. 知识点总结:  
  212.  利用程库的多文件组织让此程序变得不再复杂,建立新变量将x与y进行交换,最后将删除后的信息重新排序输出,这是删除数字  
  213. 奇偶数排列则是从右到左和从左到右对奇偶数分别排列,最后当i<j时同样也是引入新变量将数组进行交换。  
  214. 学习心得:  
  215. 以后的学习路上还是要多多总结
0 0
原创粉丝点击