题目描述
从顺序表中删除其值在给定值s-t之间(包含s和t,要求s<t)的所有元素,若s或t不合理或顺序表为空,则显示错误信息并退出运行
cpp
#include<iostream>
using namespace std;
#define Maxsize 1000
typedef struct
{
int data[Maxsize];
int length;
}sqList;
bool del_s_t(sqList& L, int s, int t)
{
int k = 0;
if (L.length == 0 || s > t)
return false;
for (int i = 0; i < L.length; i++)
{
if (L.data[i] >= s && L.data[i] <= t)
{
k++;
}
else L.data[i - k] = L.data[i];
}
L.length -= k;
return true;
}
int main()
{
sqList L = {1,3,4,5,8,9,10};
L.length = 7;
cout << "init seq:";
for (int i = 0; i < L.length; i++)
cout << L.data[i] << " ";
cout << endl;
del_s_t(L, 2,5);
cout << "changed seq:";
for (int i = 0; i < L.length; i++)
cout << L.data[i] << " ";
return 0;
}