第三周——【项目4 - 顺序表应用】

来源:互联网 发布:homebrew mysql 启动 编辑:程序博客网 时间:2024/04/28 05:34

题目要求:

删除元素在[x, y]之间的所有元素,要求算法的时间复杂度为O(n),空间复杂度为O(1);
(头文件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;}


结果:

0 0