#include<iostream.h>
enum resultcode{Underflow,OutOfBounds};
template<class T>
struct Node
{
Node(){link=NULL;}
Node(T e,Node* next)
{
element=e;
link=next;
}
T element;
Node* link;
};
template<class T>
class LinkList
{
public:
LinkList()
{
first=NULL;
n=0;
}
~LinkList()
{
n=0;
}
void Insert(int pos,const T& x);
void Remove(int pos);
void Retrieve(int pos,T& x)const;
void Replace(int pos,const T& x);
int length()const{return n;}
bool IsEmpty()const{return first==NULL;}
protected:
Node<T> *SetPos(int pos) const;
void Output(ostream& out)const;
Node<T> *first;
int n;
friend ostream& operator<<(ostream& out,const LinkList<T>& l);
};
template<class T>
Node<T>* LinkList<T>::SetPos(int pos)const
{
Node<T>* q=first;
for(int i=0;i<pos;i++)
q=q->link;
return q;
}
template<class T>
void LinkList<T>::Insert(int pos,const T& x)
{
Node<T> *q,*p;
if(pos<0||pos>n)
throw OutOfBounds;
p=new Node<T>(x,NULL);
if(pos>0)
{
q=SetPos(pos-1);
p->link=q->link;
q->link=p;
}
else
{
p->link=first;
first=p;
}
n++;
}
template<class T>
void LinkList<T>::Remove(int pos)
{
Node<T>* q,*p;
if(IsEmpty())
throw Underflow;
if(pos<0||pos>n)
throw OutOfBounds;
if(pos==0)
{
p=first;
first=first->link;
}
else
{
q=SetPos(pos-1);
p=q->link;
q->link=p->link;
}
delete p;
n--;
}
template<class T>
void LinkList<T>::Retrieve(int pos,T& x)const
{
if(IsEmpty())
throw Underflow;
if(pos<0||pos>n)
throw OutOfBounds;
Node<T>* q=SetPos(pos);
x=q->element;
}
template<class T>
void LinkList<T>::Replace(int pos,const T& x)
{
if(IsEmpty())
throw Underflow;
if(pos<0||pos>n)
throw OutOfBounds;
Node<T>* q=SetPos(pos);
q->element=x;
}
template<class T>
ostream& operator<<(ostream& out,const LinkList<T>& l)
{
l.Output(out);
return out;
}
template<class T>
void LinkList<T>::Output(ostream& out)const
{
out<<"The list contains:";
for(;first;first=first->link)
out<<' '<<first->element;
out<<endl;
}
void main()
{
try{
LinkList<int> A;
cout<<"请输入8个整数:";
for(int i=0;i<8;i++)
{
int j;
cin>>j;
A.Insert(i,j);
}
cout<<"线性表为:"<<A;
A.Remove(3);
cout<<A;
cout<<"用11代替第四个位置:";
A.Replace(4,11);
cout<<A;
cout<<"第五个位置的值为:";
int k=A.Retrieve(5,k);
cout<<k<<endl;
}
catch(resultcode err){
switch(err)
{
case Underflow:cout<<"underflow"<<endl;break;
case OutOfBounds:cout<<"outofbounds"<<endl;break;
}
}
}
下面是错误
--------------------Configuration: 6598 - Win32 Debug--------------------
Compiling...
3.cpp
C:\Program Files\Microsoft Visual Studio\MyProjects\6598\3.cpp(131) : error C2244: 'LinkList<T>::Output' : unable to resolve function overload
C:\Program Files\Microsoft Visual Studio\MyProjects\6598\3.cpp(142) : error C2065: 'j' : undeclared identifier
C:\Program Files\Microsoft Visual Studio\MyProjects\6598\3.cpp(143) : error C2065: 'A' : undeclared identifier
C:\Program Files\Microsoft Visual Studio\MyProjects\6598\3.cpp(143) : error C2228: left of '.Insert' must have class/struct/union type
C:\Program Files\Microsoft Visual Studio\MyProjects\6598\3.cpp(146) : error C2228: left of '.Remove' must have class/struct/union type
C:\Program Files\Microsoft Visual Studio\MyProjects\6598\3.cpp(149) : error C2228: left of '.Replace' must have class/struct/union type
C:\Program Files\Microsoft Visual Studio\MyProjects\6598\3.cpp(152) : error C2228: left of '.Retrieve' must have class/struct/union type
Error executing cl.exe.
3.obj - 7 error(s), 0 warning(s)
该怎么解决呢??