a->find(1,3);//注释掉这行时,错误是 warning C4700: 使用了未初始化的局部变量“a”,而且运行被终止!!
#include<iostream>usingnamespacestd;template<classT>classlinearlist{public:virtualboo...
#include<iostream>
using namespace std;
template <class T>
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)=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 T> //提前声明
class singlelist;
template<class T>
//结点类
class node
{
T element;
node<T>*link;
friend class singlelist<T>; //前面已经声明
};
template<class T>
//单链表实现
class singlelist:public linearlist<T>
{
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<T>* first;
};
// member fuction
template <class T>
singlelist<T>::~singlelist()
{
node<T>*p;
while(first){
p=first->link;
delete first;
first=p;
}
}
template<class T>
int singlelist<T>::length() const
{
return n;
}
template<class T>
bool singlelist<T>::isempty()const
{return n==0;}
template<class T>
bool singlelist<T>::find(int i, T &x) const//find the a[i]
{
if(i<0||i>n-1){
cout<<"out of bounds";return false;}
note<T>* p=first;
for(int j=0;j<i;j++) //i次,first is the a[0] addresss.
p=p->link; //p->a[i]
}
template<class T>
int singlelist<T>::search(T x) const
{
int j;
node<T>* p=first;
for(j=0;p&&p->element!=x;j++)p=p->link;
if(p)return j;
return -1;
}
template<class T>
bool singlelist<T>::insert(int i, T x)
{
if(i<-1||i>n-1){
cout<<"out of bounds";return false;}
node<T>* q=new node<T>;q->element=x;
node<T>*p=first;
for(int j=0;j<i;j++)p=p->link;
if(i>-1){
q->link=p->link;
p->link=q;
}
else{
q->link=first;
first=q;
}
n++;return true;
}
template<class T>
bool singlelist<T>::Delete(int i)
{
if(!n){
cout<<"underflow"<<endl;return false;}
if(i<0||i>n-1){
cout<<"out of bounds"<<endl;return false; }
node<T>*p=first,*q=first;
for(int j=0;j<i-1;j++)q=q->link;
if(i==0)first=first->link;
else{
p=q->link;
q->link=p->link;}
delete p;
n--;return true;
}
template<class T>
bool singlelist<T>::update(int i,T x)
{
if(i<0||i>n-1){
cout<<"out of bounds"<<endl;return false;}
node<T>*p=first;
for(int j=0;j<i;j++)p=p->link;
p->element=x;return true;
}
template<class T>
void singlelist<T>::output(ostream& out)const
{
node<T>*p=first;
while(p){
out<<p->element<<" ";
p=p->link;
}
out<<endl;
}
//主函数
int main()
{singlelist<int> *a;
(*a).insert(1,2);
a->find(1,3);//存在时提示 不能将参数 2 从“int”转换为“int &”
a->output(cout);
return 0;}
我用的vs2008。请各位帮忙啦! 展开
using namespace std;
template <class T>
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)=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 T> //提前声明
class singlelist;
template<class T>
//结点类
class node
{
T element;
node<T>*link;
friend class singlelist<T>; //前面已经声明
};
template<class T>
//单链表实现
class singlelist:public linearlist<T>
{
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<T>* first;
};
// member fuction
template <class T>
singlelist<T>::~singlelist()
{
node<T>*p;
while(first){
p=first->link;
delete first;
first=p;
}
}
template<class T>
int singlelist<T>::length() const
{
return n;
}
template<class T>
bool singlelist<T>::isempty()const
{return n==0;}
template<class T>
bool singlelist<T>::find(int i, T &x) const//find the a[i]
{
if(i<0||i>n-1){
cout<<"out of bounds";return false;}
note<T>* p=first;
for(int j=0;j<i;j++) //i次,first is the a[0] addresss.
p=p->link; //p->a[i]
}
template<class T>
int singlelist<T>::search(T x) const
{
int j;
node<T>* p=first;
for(j=0;p&&p->element!=x;j++)p=p->link;
if(p)return j;
return -1;
}
template<class T>
bool singlelist<T>::insert(int i, T x)
{
if(i<-1||i>n-1){
cout<<"out of bounds";return false;}
node<T>* q=new node<T>;q->element=x;
node<T>*p=first;
for(int j=0;j<i;j++)p=p->link;
if(i>-1){
q->link=p->link;
p->link=q;
}
else{
q->link=first;
first=q;
}
n++;return true;
}
template<class T>
bool singlelist<T>::Delete(int i)
{
if(!n){
cout<<"underflow"<<endl;return false;}
if(i<0||i>n-1){
cout<<"out of bounds"<<endl;return false; }
node<T>*p=first,*q=first;
for(int j=0;j<i-1;j++)q=q->link;
if(i==0)first=first->link;
else{
p=q->link;
q->link=p->link;}
delete p;
n--;return true;
}
template<class T>
bool singlelist<T>::update(int i,T x)
{
if(i<0||i>n-1){
cout<<"out of bounds"<<endl;return false;}
node<T>*p=first;
for(int j=0;j<i;j++)p=p->link;
p->element=x;return true;
}
template<class T>
void singlelist<T>::output(ostream& out)const
{
node<T>*p=first;
while(p){
out<<p->element<<" ";
p=p->link;
}
out<<endl;
}
//主函数
int main()
{singlelist<int> *a;
(*a).insert(1,2);
a->find(1,3);//存在时提示 不能将参数 2 从“int”转换为“int &”
a->output(cout);
return 0;}
我用的vs2008。请各位帮忙啦! 展开
展开全部
#include<iostream>
using namespace std;
template <class T>
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 T> //提前声明
class singlelist;
template<class T>//结点类
class node
{
T element;
node<T>*link;
friend class singlelist<T>; //前面已经声明
};
template<class T>//单链表实现
class singlelist:public linearlist<T>
{
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<T>* first;
};
// member fuction
template <class T>
singlelist<T>::~singlelist()
{
node<T>*p;
while(first)
{
p=first->link;
delete first;
first=p;
}
}
template<class T>
int singlelist<T>::length() const
{
return n;
}
template<class T>
bool singlelist<T>::isempty()const
{
return n==0;
}
template<class T>
bool singlelist<T>::find(int i, T x) const//find the a[i]
{
if(i<0||i>n-1)
{
cout<<"out of bounds";
return false;
}
node<T>* p=first;
for(int j=-1;j<i;j++) //i次,first is the a[0] addresss.
{
if(p->element==x)
return true;
p=p->link; //p->a[i]
}
return false;
}
template<class T>
int singlelist<T>::search(T x) const
{
int j;
node<T>* p=first;
for(j=0;p&&p->element!=x;j++)
p=p->link;
if(p)
return j;
return -1;
}
template<class T>
bool singlelist<T>::insert(int i, T x)
{
if(i<-1||i>n-1)
{
cout<<"out of bounds";
return false;
}
node<T>* q=new node<T>;
q->element=x;
node<T>*p=first;
for(int j=0;j<i;j++)
p=p->link;
if(i>-1)
{
q->link=p->link;
p->link=q;
}
else
{
q->link=first;
first=q;
}
n++;
return true;
}
template<class T>
bool singlelist<T>::Delete(int i)
{
if(!n)
{
cout<<"underflow"<<endl;
return false;
}
if(i<0||i>n-1)
{
cout<<"out of bounds"<<endl;
return false;
}
node<T>*p=first,*q=first;
for(int j=0;j<i-1;j++)
q=q->link;
if(i==0)
first=first->link;
else
{
p=q->link;
q->link=p->link;
}
delete p;
n--;
return true;
}
template<class T>
bool singlelist<T>::update(int i,T x)
{
if(i<0||i>n-1)
{
cout<<"out of bounds"<<endl;
return false;
}
node<T>*p=first;
for(int j=0;j<i;j++)
p=p->link;
p->element=x;return true;
}
template<class T>
void singlelist<T>::output(ostream& out)const
{
node<T>*p=first;
while(p)
{
out<<p->element<<" ";
p=p->link;
}
out<<endl;
}
//主函数
int main()
{
int ret;
singlelist<int> *a=new singlelist<int>();
(*a).insert(-1,2);
ret=a->find(0,2);//存在时提示 不能将参数 2 从“int”转换为“int &”
cout<<ret<<endl;
a->output(cout);
return 0;
}
修改1:纯虚函数在派生类singlelist中实现的修改,包括函数参数不同修改回来了
修改2:singlelist<int> *a=new singlelist<int>();初始化一下
其他的没仔细看,呵呵
using namespace std;
template <class T>
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 T> //提前声明
class singlelist;
template<class T>//结点类
class node
{
T element;
node<T>*link;
friend class singlelist<T>; //前面已经声明
};
template<class T>//单链表实现
class singlelist:public linearlist<T>
{
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<T>* first;
};
// member fuction
template <class T>
singlelist<T>::~singlelist()
{
node<T>*p;
while(first)
{
p=first->link;
delete first;
first=p;
}
}
template<class T>
int singlelist<T>::length() const
{
return n;
}
template<class T>
bool singlelist<T>::isempty()const
{
return n==0;
}
template<class T>
bool singlelist<T>::find(int i, T x) const//find the a[i]
{
if(i<0||i>n-1)
{
cout<<"out of bounds";
return false;
}
node<T>* p=first;
for(int j=-1;j<i;j++) //i次,first is the a[0] addresss.
{
if(p->element==x)
return true;
p=p->link; //p->a[i]
}
return false;
}
template<class T>
int singlelist<T>::search(T x) const
{
int j;
node<T>* p=first;
for(j=0;p&&p->element!=x;j++)
p=p->link;
if(p)
return j;
return -1;
}
template<class T>
bool singlelist<T>::insert(int i, T x)
{
if(i<-1||i>n-1)
{
cout<<"out of bounds";
return false;
}
node<T>* q=new node<T>;
q->element=x;
node<T>*p=first;
for(int j=0;j<i;j++)
p=p->link;
if(i>-1)
{
q->link=p->link;
p->link=q;
}
else
{
q->link=first;
first=q;
}
n++;
return true;
}
template<class T>
bool singlelist<T>::Delete(int i)
{
if(!n)
{
cout<<"underflow"<<endl;
return false;
}
if(i<0||i>n-1)
{
cout<<"out of bounds"<<endl;
return false;
}
node<T>*p=first,*q=first;
for(int j=0;j<i-1;j++)
q=q->link;
if(i==0)
first=first->link;
else
{
p=q->link;
q->link=p->link;
}
delete p;
n--;
return true;
}
template<class T>
bool singlelist<T>::update(int i,T x)
{
if(i<0||i>n-1)
{
cout<<"out of bounds"<<endl;
return false;
}
node<T>*p=first;
for(int j=0;j<i;j++)
p=p->link;
p->element=x;return true;
}
template<class T>
void singlelist<T>::output(ostream& out)const
{
node<T>*p=first;
while(p)
{
out<<p->element<<" ";
p=p->link;
}
out<<endl;
}
//主函数
int main()
{
int ret;
singlelist<int> *a=new singlelist<int>();
(*a).insert(-1,2);
ret=a->find(0,2);//存在时提示 不能将参数 2 从“int”转换为“int &”
cout<<ret<<endl;
a->output(cout);
return 0;
}
修改1:纯虚函数在派生类singlelist中实现的修改,包括函数参数不同修改回来了
修改2:singlelist<int> *a=new singlelist<int>();初始化一下
其他的没仔细看,呵呵
本回答被提问者采纳
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询