顺序表区间元素删除
若一个线性表L采用顺序存储结构存储,其中所有的元素为整数。设计一个算法,删除元素值在[x,y]之间的所有元素,要求算法的时间复杂度为O(n),空间复杂度为O(1)。
输入格式:
三行数据,第一行是顺序表的元素个数,第二行是顺序表的元素,第三行是x和y。
输出格式:
删除元素值在[x,y]之间的所有元素后的顺序表。
输入样例:
10
5 1 9 10 67 12 8 33 6 2
3 10
输出样例:
1 67 12 33 2
标程
#include<map>
#include<list>
#include<cmath>
#include<queue>
#include<stack>
#include<cstdio>
#include<vector>
#include<iomanip>
#include<cstring>
#include<iterator>
#include<iostream>
#include<algorithm>
#define R register
#define LL long long
#define pi 3.141
#define INF 1400000000
using namespace std;
struct Node {
int* elem;
int number; //顺序表中的数据
int length; //顺序表长度
};
Node List;
inline void InitList(Node &List){
List.elem = (int*)malloc(1000 * sizeof(int));
List.length = 0;
}
inline int read() {
int number = 0;
int f = 1;
char ch = getchar();
while (ch < '0' || ch > '9') {
if (ch == '-') {
f = -1;
}
ch = getchar();
}
while (ch >= '0' && ch <= '9') {
number = number * 10 + ch - '0', ch = getchar();
}
return number * f;
}
inline void Delete_Data(Node& List, int left, int right) {
for (R int i = 0; i < List.length; ++i) {
if (List.elem[i] >= left && List.elem[i] <= right) {
for (R int j = i + 1; j < List.length; ++j) {
List.elem[j - 1] = List.elem[j];
}
--List.length, --i;
}
}
}
int main() {
int N = read();
InitList(List);
for (R int i = 0; i < N; ++i) {
List.elem[i] = read();
++List.length;
}
int left = read(), right = read();
Delete_Data(List, left, right);
bool flag = false;
for (R int i = 0; i < List.length; ++i) {
if (!flag) {
flag = !flag;
}
else {
printf(" ");
}
printf("%d", List.elem[i]);
}
return 0;
}