数据结构上机时间第三周项目4—顺序表应用
【项目 - 顺序表应用】
定义一个采用顺序结构存储的线性表,设计算法完成删除元素在[x, y]之间的所有元素,要求算法的时间复杂度为O(n),空间复杂度为O(1) ;
项目中用到的算法库为list.cpp和list.h点此查看算法库信息
项目源代码如下:
#include "list.h"#include <stdio.h>//删除线性表中,元素值在x到y之间的元素void delx2y(SqList *&L, ElemType x, ElemType y){ int k=0,i; //k记录非x的元素个数 ElemType t; if(x>y) { t=x; x=y; y=t; } for (i=0; i<L->length; i++) if (L->data[i]<x || L->data[i]>y ) //复制不在[x, y]之间的元素 { L->data[k]=L->data[i]; k++; } L->length=k;}//用main写测试代码int main(){ SqList *sq; ElemType a[10]= {5,8,7,0,2,4,9,6,7,3}; CreateList(sq, a, 10); printf("删除前 "); DispList(sq); delx2y(sq, 4, 7); printf("删除后 "); DispList(sq); return 0;} 运行结果如图所示: