[C] 顺序表区间元素删除 →→→→→进入此内容的聊天室

来自 , 2021-03-19, 写在 C, 查看 109 次.
URL http://www.code666.cn/view/2c3ddf4b
  1. #include<stdio.h>
  2. #include<malloc.h>
  3. #include<stdlib.h>
  4.  
  5. typedef int ElemType;  //假设线性表中的元素均为整型
  6. typedef struct
  7. {
  8.     ElemType* elem;   //存储空间基地址
  9.     int length;       //表中元素的个数
  10.     int listsize;     //表容量大小
  11. } SqList;   //顺序表类型定义
  12.  
  13. int ListCreate_Sq(SqList &L)
  14. {
  15.     int n;
  16.     scanf("%d",&n);
  17.     L.elem=new ElemType[n];
  18.     if(!L.elem)
  19.         exit(-2);
  20.     L.length=0;
  21.     L.listsize=n;
  22.     for(int i=0; i<n; i++)
  23.     {
  24.         scanf("%d",&L.elem[i]);
  25.         L.length++;
  26.     }
  27.     return 1;
  28. }
  29. void ListDelete_Sq(SqList &L,ElemType x,ElemType y)
  30. {
  31.     int n=L.length;
  32.     for(int i=0,j=0; i<n; i++)
  33.     {
  34.         if(L.elem[i]>=x&&L.elem[i]<=y)
  35.             L.length--;
  36.         else
  37.             L.elem[j++]=L.elem[i];
  38.     }
  39. }
  40. int main()
  41. {
  42.     SqList L;
  43.     ElemType *p;
  44.     if(!ListCreate_Sq(L))
  45.     {
  46.         printf("ListCreate_Sq: 创建失败!!!\n");
  47.         return -1;
  48.     }
  49.     int x,y;
  50.     scanf("%d%d",&x,&y);
  51.     ListDelete_Sq(L,x,y);
  52.     for(p=L.elem; p<L.elem+L.length-1; ++p)
  53.         printf("%d ",*p);
  54.     printf("%d",*p);
  55.     return 0;
  56. }
  57.  

回复 "顺序表区间元素删除"

这儿你可以回复上面这条便签

captcha