首先输入顺序表大小,再依次依次输入顺序表的各个结点的元素,再输入两个数x,y,代表删除大于等于下,小于等于y的结点,
template<class T>
int List<T>::Search(T x)
{
int i = 0;
LinkNode<T>*current = first->link;
while (current != NULL)
{
if (current->data == x)
{
i++;
break;
}
i++;
current = current->link;
}
return i;
}
template<class T>
LinkNode<T>* List<T>::Locate(int i) //定位函数,返回第i个结点的地址,i从一开始,因为涉及到remove
{
int n = Length();
if (i < 0|| i>n)
{
return NULL;
}
LinkNode<T>*current = first;
int k = 0;
while (current != NULL && k < i)
{
current = current->link;
k++;
}
return current;
}
template<class T>
bool List<T>::getData(int i, T&x)
{
if (i < 0||i==0||i>Length())
return false;
LinkNode<T>*current = Locate(i);
if (current == NULL)
return false;
else
{
x = current->data;
return true;
}
}
template<class T>
bool List<T>::Remove(int i, T&x)
{
LinkNode<T>*current = Locate(i - 1); // i - 1 限制了i从1开始
LinkNode<T>*del = current->link;
if (current == NULL || current->link == NULL)
{
return false;
}
current->link = del->link;
x = del->data;
delete del;
return true;
}
emplate<class T>
void List<T>::output()
{
LinkNode<T>*current = first->link;
if (current == NULL)
{
cout << "{}" << endl; 记得考虑 空顺序表时的输出
}
else
{
cout << "{ " << current->data;
current = current->link;
while (current != NULL)
{
cout << ' ' << current->data;
current = current->link;
}
cout << " }" << endl;
}
}
template<class T>
void List<T>::inputFront(T endTag)
{
LinkNode<T>*newNode;
T val;
makeEmpty();
cin >> val;
while (val != endTag)
{
newNode = new LinkNode<T>(val);
newNode->link = first->link;
first->link = newNode;
cin >> val;
}
}
template<class T>
void List<T>::inputRear(int num)
{
int i = 0;
LinkNode<T> *newNode, *last;
T val;
makeEmpty();
last = first;
while (i<num)
{
cin >> val;
newNode = new LinkNode<T>(val);
last->link = newNode;
last = newNode;
i++;
}
last->link = NULL;
}
template<class T>
bool Delete(List<T> L, int num, int x, int y)
{
bool A, B;
int i, l, mid;
for (i = 1; i <num+1; i++)
{
A = L.getData(i, mid);
if (A = false)
{
return false;
}
if ((mid > x || mid == x) && (mid < y || mid == y))
{
B = L.Remove(i, x);
if (B = false)
{
return false;
}
i--; 删除第i个以后,第i+1个变成第i个,所以i--;
num--; 顺序表长度-1,所以num--,控制循环次数;
}
}
L.output();
return true;
}
int main()
{
bool A;
int num, s, t;
List<int> L;
cin >> num;
L.inputRear(num);
cin >> s >> t;
if (s > t)
{
cout << "s or t error!";
return 0;
}
A = Delete(L, num, s, t);
return 0;
}