第三周项目四-顺序表应用问题

  1. *Copyright (c)2016,烟台大学计算机与控制工程学院    
  2. *All rights reserved.    
  3. *文件名称:项目4.cbp    
  4. *作    者:陈鹏鹏    
  5. *完成日期:2016年9月18日    
  6. *版 本 号:v1.0    
  7. *问题描述:删除元素在[x, y]之间的所有元素,要求算法的时间复杂度为O(n),空间复杂度为O(1);   
  8. *输入描述:无    
  9. *程序输出:输出调整后的线性表   
  10. */    
    • 头文件list.h代码:

    [cpp]  view plain  copy
    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代码:

    [cpp]  view plain  copy
    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测试文件代码:

    [cpp]  view plain  copy
    1. #include "list.h"  
    2. #include <stdio.h>  
    3.   
    4. //删除线性表中,元素值在x到y之间的元素  
    5. void delx2y(SqList *&L, ElemType x,  ElemType y)  
    6. {  
    7.     int k=0,i; //k记录非x的元素个数  
    8.     ElemType t;  
    9.     if(x>y)//使输出到下面的数都为前面小 后面小的形式  
    10.     {  
    11.         t=x;  
    12.         x=y;  
    13.         y=t;  
    14.     }  
    15.     for (i=0; i<L->length; i++)  
    16.         if (L->data[i]<x || L->data[i]>y )  //复制不在[x, y]之间的元素  
    17.         {  
    18.             L->data[k]=L->data[i];          //k增加 并且每增加一个新赋值一个  
    19.             k++;  
    20.         }  
    21.     L->length=k;  
    22. }  
    23.   
    24. //用main写测试代码  
    25. int main()  
    26. {  
    27.     SqList *sq;  
    28.     ElemType a[12]= {2,0,1,4,5,8,5,0,6,1,1,4};  
    29.     CreateList(sq, a, 12);  
    30.     printf("删除前 ");  
    31.     DispList(sq);  
    32.   
    33.     delx2y(sq, 1, 5);//主要运用此函数删除  
    34.   
    35.     printf("删除后 ");  
    36.     DispList(sq);  
    37.     return 0;  
    38. }  
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值