#include
using namespace std;
template
class linearlist
{
public:
virtual bool isempty() const=0;//纯虚函数
virtual int length() const=0;
virtual bool find(int i,T x) const=0;
virtual int search(T x) const=0;
virtual bool insert(int i,T x)=0;
virtual bool Delete(int i)=0;
virtual bool update(int i,T x)=0;
virtual void output(ostream & out)const=0;
protected:
int n;
}; //虚基类
template
class singlelist;
template
class node
{
T element;
node
friend class singlelist
};
template
class singlelist:public linearlist
{
public:
singlelist(){first=NULL;n=0;}
~singlelist();
bool isempty()const;
int length()const;
bool find(int i,T x)const;
int search(T x)const;
bool insert(int i,T x);
bool Delete(int i);
bool update(int i,T x);
//void clear();
void output(ostream& out)const;
private:
node
};
// member fuction
template
singlelist
{
node
while(first)
{
p=first->link;
delete first;
first=p;
}
}
template
int singlelist
{
return n;
}
template
bool singlelist
{
return n==0;
}
template
bool singlelist
{
if(i<0||i>n-1)
{
cout<<"out of bounds";
return false;
}
node
for(int j=-1;j {
if(p->element==x)
return true;
p=p->link; //p->a[i]
}
return false;
}
template
int singlelist
{
int j;
node
for(j=0;p&&p->element!=x;j++)
p=p->link;
if(p)
return j;
return -1;
}
template
bool singlelist
{
if(i<-1||i>n-1)
{
cout<<"out of bounds";
return false;
}
node
q->element=x;
node
for(int j=0;j p=p->link;
if(i>-1)
{
q->link=p->link;
p->link=q;
}
else
{
q->link=first;
first=q;
}
n++;
return true;
}
template
bool singlelist
{
if(!n)
{
cout<<"underflow"<
}
if(i<0||i>n-1)
{
cout<<"out of bounds"<
}
node
for(int j=0;j
if(i==0)
first=first->link;
else
{
p=q->link;
q->link=p->link;
}
delete p;
n--;
return true;
}
template
bool singlelist
{
if(i<0||i>n-1)
{
cout<<"out of bounds"<
}
node
for(int j=0;j p=p->link;
p->element=x;return true;
}
template
void singlelist
{
node
while(p)
{
out<
p=p->link;
}
out<
//主函数
int main()
{
int ret;
singlelist
(*a).insert(-1,2);
ret=a->find(0,2);//存在时提示 不能将参数 2 从“int”转换为“int &”
cout<
a->output(cout);
return 0;
}
修改1:纯虚函数在派生类singlelist中实现的修改,包括函数参数不同修改回来了
修改2:singlelist
其他的没仔细看,呵呵