第三周项目4

来源:互联网 发布:mac os x 10.11.6 cdr 编辑:程序博客网 时间:2024/05/17 22:56

/*  

*Copyright  (c)2017,烟台大学计算机与控制工程学院      

*All rights reservrd.      

*文件名称 :test.cpp      

*作者:潘亚楠  

*完成时间:2017年12月14日      

*版本号:v1.0      

*问题描述:删除元素在【x,y】质检的所有元素

问题及代码:

  1. #include <stdio.h>    
  2. #include <malloc.h>    
  3.   
  4. #define MaxSize 50    
  5. typedef int ElemType;    
  6. typedef struct    
  7. {    
  8.     ElemType data[MaxSize];    
  9.     int length;    
  10. } SqList;    
  11.   
  12. //自定义函数声明部分    
  13. void CreateList(SqList *&L, ElemType a[], int n);    
  14. void DispList(SqList *L);    
  15. bool ListEmpty(SqList *L);    
  16. int ListLength(SqList *L);    
  17. bool GetElem(SqList *L,int i,ElemType &e);    
  18. int LocateElem(SqList *L, ElemType e);     
  19. void InitList(SqList *&L);    
  20. bool ListInsert(SqList *&L,int i,ElemType e);    
  21. bool ListDelete(SqList *&L,int i,ElemType &e);    
  22. void DestroyList(SqList *&L);   
  23. void delx2y(SqList *&L, ElemType x,  ElemType y);  
  24.   
  25. void CreateList(SqList *&L, ElemType a[], int n)    //用数组创建线性表    
  26. {    
  27.     int i;    
  28.     L=(SqList *)malloc(sizeof(SqList));    
  29.     for (i=0; i<n; i++)    
  30.         L->data[i]=a[i];    
  31.     L->length=n;    
  32. }    
  33.   
  34.   
  35. void DispList(SqList *L)       //输出线性表DispList(L)    
  36. {    
  37.     int i;    
  38.     if (ListEmpty(L))    
  39.         return;    
  40.     for (i=0; i<L->length; i++)    
  41.         printf("%d ",L->data[i]);    
  42.     printf("\n");    
  43. }    
  44.   
  45.   
  46. bool ListEmpty(SqList *L)      //判定是否为空表ListEmpty(L)    
  47. {    
  48.     return(L->length==0);    
  49. }    
  50.   
  51.   
  52. int ListLength(SqList *L)      //求线性表的长度ListLength(L)    
  53. {    
  54.     return(L->length);    
  55. }    
  56.   
  57.   
  58. bool GetElem(SqList *L,int i,ElemType &e)      //求某个数据元素值GetElem(L,i,e)    
  59. {    
  60.     if (i<1 || i>L->length)    
  61.         return false;    
  62.     e=L->data[i-1];    
  63.     return true;    
  64. }    
  65.   
  66.   
  67. int LocateElem(SqList *L, ElemType e)      //按元素值查找LocateElem(L,e)    
  68. {    
  69.     int i=0;    
  70.     while (i<L->length && L->data[i]!=e) i++;    
  71.     if (i>=L->length)    
  72.         return 0;    
  73.     else    
  74.         return i+1;    
  75. }    
  76. void InitList(SqList *&L)           //初始化线性表    
  77. {    
  78.     L=(SqList *)malloc(sizeof(SqList));    
  79.     L->length=0;    
  80. }    
  81. bool ListInsert(SqList *&L,int i,ElemType e)    
  82. {    
  83.     int j;    
  84.     if(i<1||i>L->length+1)    
  85.         return false;    
  86.     i--;    
  87.     for(j=L->length;j>i;j--)    
  88.         L->data[j]=L->data[j-1];    
  89.     L->data[i]=e;    
  90.     L->length++;    
  91.     return true;    
  92. }    
  93. bool ListDelete(SqList *&L,int i,ElemType &e)    
  94. {    
  95.     int j;    
  96.     if(i<1||i>L->length)    
  97.         return false;    
  98.     i--;    
  99.     e=L->data[i];    
  100.     for(j=i;j<L->length-1;j++)    
  101.         L->data[j]=L->data[j+1];    
  102.     L->length--;    
  103.     return true;    
  104. }    
  105. void DestroyList(SqList *&L)    
  106. {    
  107.     free(L);    
  108. }    
  109. //删除线性表中,元素值在x到y之间的元素  
  110. void delx2y(SqList *&L, ElemType x,  ElemType y)  
  111. {  
  112.     int k=0,i; //k记录非x的元素个数  
  113.     ElemType t;  
  114.     if(x>y)  
  115.     {  
  116.         t=x;  
  117.         x=y;  
  118.         y=t;  
  119.     }  
  120.     for (i=0; i<L->length; i++)  
  121.         if (L->data[i]<x || L->data[i]>y )  //复制不在[x, y]之间的元素  
  122.         {  
  123.             L->data[k]=L->data[i];  
  124.             k++;  
  125.         }  
  126.     L->length=k;  
  127. }  
  128.   
  129. //用main写测试代码  
  130. int main()  
  131. {  
  132.     SqList *sq;  
  133.     ElemType a[10]= {5,8,7,0,2,4,9,6,7,3};  
  134.     CreateList(sq, a, 10);  
  135.     printf("删除前 ");  
  136.     DispList(sq);  
  137.   
  138.     delx2y(sq, 4, 7);  
  139.   
  140.     printf("删除后 ");  
  141.     DispList(sq);  
  142.     return 0;  
  143. }  
运行结果:




原创粉丝点击