顺序表 删除指定范围

题目:设计一个高效的算法,从顺序表L中删除所有值介于x和y之间(包括x和y)的所有元素(假设y>=x),要求时间复杂度为O(n),空间复杂度为O(1)。
struct _seqlist{
    ElemType elem[MAXSIZE];
    int last;
};
typedef struct _seqlist SeqList;

void del_x2y(SeqList* L, ElemType x, ElemType y)
{
    int i = 0, j = 0;
    for (i = 0; i < MAXSIZE; i++) {
        if (L->elem[i] < x || L->elem[i] > y) {
            L->elem[j++] = L->elem[i];//在范围外,则继续赋值
        } 
    }
    L->last=j;
}

猜你喜欢

转载自blog.csdn.net/weixin_45454859/article/details/104699372