//头文件
#ifndef SLINKLIST_H
#define SLINKLIST_H
template
class list
{
public:
virtual void clear() = 0;
virtual int length()const = 0;
virtual void insert(int i, const elemType& x) = 0;
virtual void remove(int i) = 0;
virtual void traverse()const = 0;
virtual ~list() {};
};
template
class sLinkList :public list
{
private:
struct node{
elemType data;
node* next, * tail;
node(const elemType& x, node* n = nullptr)
{
data = x; next = n;
}
node() :next(nullptr) {}
~node() {}
};
node* head;
node* tail;
int currentLength;
node* move(int i)const;
public:
sLinkList();
~sLinkList() {clear(); delete head; delete tail; }
void clear();
int length()const { return currentLength; }
void insert(int i, const elemType &x);
void remove(int i);
void traverse()const;
void erase(const elemType &x, const elemType &y);
};
#endif
//实现文件
#include "sLinkList.h"
#include
using namespace std;
template
typename sLinkList::node* sLinkList::move(int i)const
{
node* p = head;
while (i-- >= 0) p = p->next;
return p;
}
template
sLinkList::sLinkList()
{
head = new node;
tail = new node;
head->next = tail;
currentLength = 0;
}
template
void sLinkList::clear()
{
node* p = head->next, * q;
head->next = nullptr;
while (p != tail) {
q = p->next;
delete p;
p = q;
}
currentLength = 0;
}
template
void sLinkList::insert(int i, const elemType &x)
{
node* pos;
pos = move(i - 1);
pos->next = new node(x, pos->next);
++currentLength;
}
template
void sLinkList::remove(int i)
{
node* pos, * delp;
pos = move(i - 1);
delp = pos->next;
pos->next = delp->next;
delete delp;
--currentLength;
}
template
void sLinkList::erase(const elemType &x, const elemType &y)
{
node* p;
int i = 0;
p = head;
while (p->next!=tail)
{
if (p->next->data >= x && p->next->data <= y) remove(i);
else { p = p->next; i++; }
}
}
template
void sLinkList::traverse()const
{
node* p = head->next;
cout << endl;
while (p != tail) {
cout << p->data << " ";
p = p->next;
}
cout << endl;
}
//主函数
#include "sLinkList.h"
#include
#include
using namespace std;
int main()
{
sLinkList s;
vector v;
int tmp,x,y;
while (cin >> tmp) {
v.push_back(tmp);
if (getchar() == '\n')
break;
}
for (unsigned int i = 0; i < v.size(); i++)
{
s.insert(i, v[i]);
}
cin >> x >> y;
s.erase(x, y);
s.length();
s.traverse();
return 0;
}