第三周 项目四——顺序表应用(1)

问题及代码:

[cpp]  view plain  copy
  在CODE上查看代码片 派生到我的代码片
  1. /*      
  2. Copyright (c)2016,烟台大学计算机与控制工程学院      
  3. All rights reserved.      
  4. 文件名称:顺序表的应用.cpp      
  5. 作    者:   周国亮 
  6. 完成日期:2016年9月17日      
  7. 版 本 号:v1.0         
  8. 问题描述: 
  9. 输入描述:无     
  10. 程序输出:若干数据。   
  11. */   
  12. #include "111.h"    
  13. //用数组创建线性表    
  14. void CreateList(SqList *&L, ElemType a[], int n)    
  15. {    
  16.     int i;    
  17.     L=(SqList *)malloc(sizeof(SqList));    
  18.     for (i=0; i<n; i++)    
  19.         L->data[i]=a[i];    
  20.     L->length=n;    
  21. }    
  22. //初始化线性表InitList(L)    
  23. void InitList(SqList *&L)   //引用型指针    
  24. {    
  25.     L=(SqList *)malloc(sizeof(SqList));    
  26.     //分配存放线性表的空间    
  27.     L->length=0;    
  28. }    
  29. //销毁线性表DestroyList(L)    
  30. void DestroyList(SqList *&L)    
  31. {    
  32.     L=(SqList *)malloc(sizeof(SqList));    
  33.     free(L);    
  34. }    
  35.     
  36. //判定是否为空表ListEmpty(L)    
  37. bool ListEmpty(SqList *L)    
  38. {    
  39.     return(L->length==0);    
  40. }    
  41. //求线性表的长度ListLength(L)    
  42. int ListLength(SqList *L)    
  43. {    
  44.     return(L->length);    
  45. }    
  46. //输出线性表DispList(L)    
  47. void DispList(SqList *L)    
  48. {    
  49.     int i;    
  50.     if (ListEmpty(L)) return;    
  51.     for (i=0; i<L->length; i++)    
  52.         printf("%d ",L->data[i]);    
  53.     printf("\n");    
  54. }    
  55.     
  56. //求某个数据元素值GetElem(L,i,e)    
  57. bool GetElem(SqList *L,int i,ElemType &e)    
  58. {    
  59.     if (i<1 || i>L->length)  return false;    
  60.     e=L->data[i-1];    
  61.     return true;    
  62. }    
  63.     
  64. //按元素值查找LocateElem(L,e)    
  65. int LocateElem(SqList *L, ElemType e)    
  66. {    
  67.     int i=0;    
  68.     while (i<L->length && L->data[i]!=e) i++;    
  69.     if (i>=L->length)  return 0;    
  70.     else  return i+1;    
  71. }    
  72.     
  73. //插入数据元素ListInsert(L,i,e)    
  74. bool ListInsert(SqList *&L,int i,ElemType e)    
  75. {    
  76.     int j;    
  77.     if (i<1 || i>L->length+1)    
  78.         return false;   //参数错误时返回false    
  79.     i--;            //将顺序表逻辑序号转化为物理序号    
  80.     for (j=L->length; j>i; j--) //将data[i..n]元素后移一个位置    
  81.         L->data[j]=L->data[j-1];    
  82.     L->data[i]=e;           //插入元素e    
  83.     L->length++;            //顺序表长度增1    
  84.     return true;            //成功插入返回true    
  85. }    
  86.     
  87. //删除数据元素ListDelete(L,i,e)    
  88. bool ListDelete(SqList *&L,int i,ElemType &e)    
  89. {    
  90.     int j;    
  91.    if (i<1 || i>L->length)  //参数错误时返回false    
  92.         return false;    
  93.     i--;        //将顺序表逻辑序号转化为物理序号    
  94.     e=L->data[i];    
  95.    for (j=i; j<L->length-1; j++) //将data[i..n-1]元素前移    
  96.         L->data[j]=L->data[j+1];    
  97.     L->length--;              //顺序表长度减1    
  98.     return true;              //成功删除返回true    
  99. }    


[cpp]  view plain  copy
  在CODE上查看代码片 派生到我的代码片
  1. #ifndef LIST_H_INCLUDED    
  2. #define LIST_H_INCLUDED    
  3. #define MaxSize 50    
  4. #include <stdio.h>    
  5. #include <malloc.h>    
  6. typedef int ElemType;    
  7. typedef struct    
  8. {    
  9.    ElemType data[MaxSize];    
  10.     int length;    
  11. } SqList;    
  12. void CreateList(SqList *&L, ElemType a[], int n);//用数组创建线性表    
  13. void InitList(SqList *&L);//初始化线性表InitList(L)    
  14. void DestroyList(SqList *&L);//销毁线性表DestroyList(L)    
  15. bool ListEmpty(SqList *L);//判定是否为空表ListEmpty(L)    
  16. int ListLength(SqList *L);//求线性表的长度ListLength(L)    
  17. void DispList(SqList *L);//输出线性表DispList(L)    
  18. bool GetElem(SqList *L,int i,ElemType &e);//求某个数据元素值GetElem(L,i,e)    
  19. int LocateElem(SqList *L, ElemType e);//按元素值查找LocateElem(L,e)    
  20. bool ListInsert(SqList *&L,int i,ElemType e);//插入数据元素ListInsert(L,i,e)    
  21. bool ListDelete(SqList *&L,int i,ElemType &e);//删除数据元素ListDelete(L,i,e)    
  22. #endif // LIST_H_INCLUDED    

[cpp]  view plain  copy
  在CODE上查看代码片 派生到我的代码片
  1. #include "111.h"    
  2. #include <stdio.h>    
  3. //删除线性表中,元素值在x到y之间的元素    
  4. void delx2y(SqList *&L, ElemType x,  ElemType y)    
  5. {    
  6.    int k=0,i; //k记录非x的元素个数    
  7.    ElemType t;    
  8.     if(x>y)//使输出到下面的数都为前面小 后面小的形式    
  9.    {    
  10.        t=x;    
  11.         x=y;    
  12.         y=t;    
  13.    }    
  14.    for (i=0; i<L->length; i++)    
  15.        if (L->data[i]<x || L->data[i]>y )  //复制不在[x, y]之间的元素    
  16.      {    
  17.           L->data[k]=L->data[i];          //k增加 并且每增加一个新赋值一个    
  18.            k++;    
  19.         }    
  20.    L->length=k;    
  21. }    
  22. int main()    
  23. {    
  24.     SqList *sq;    
  25.     ElemType a[12]= {0,2,4,1,5,6,7,8,9,10,3,11};    
  26.     CreateList(sq, a, 12);    
  27.     printf("删除前 ");    
  28.     DispList(sq);    
  29.     delx2y(sq, 1, 7);//主要运用此函数删除    
  30.     printf("删除后 ");    
  31.     DispList(sq);    
  32.    return 0;    
  33. }    
  34.      
  35. 运行结果:

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值