#include<iostream>
using namespace std;
template<class T>
struct LinkNode
{
T data;
LinkNode<T>*link;
LinkNode(LinkNode<T>*ptr=NULL) { link=ptr;}
LinkNode(T item,LinkNode<T>*ptr=NULL) { data=item;link=ptr;}
};
template<class T>
class List
{
private:
LinkNode<T> *first;
public:
List() { first=new LinkNode<T>;}
List(const T &x) { first=new LinkNode<T>(x);} //常值引用
List(List<T>&L);
~List() { makeEmpty();}
void makeEmpty();
int Length() const;
int Search(const T &x); //常值引用
LinkNode<T>*GetHead() { return first;}
LinkNode<T>*Locate(int i);
bool Insert(int i,T x);
bool Remove(int i,T &x);
bool IsEmpty() { return (first->link==NULL)?true:false;}
void Input(int i);
void Output();
};
template<class T>
List<T>::List(List<T>&L)
{
LinkNode<T>*desptr=first=new LinkNode<T>;
LinkNode<T>*srcptr=L.GetHead();
T value;
while(srcptr->link!=NULL)
{
value=srcptr->link->data;
desptr->link=new LinkNode<T>(value);
desptr=desptr->link;
srcptr=srcptr->link;
}
desptr->link=NULL;
}
template<class T>
void List<T>::makeEmpty()
{
LinkNode<T>*current=first;
LinkNode<T>*q;
while(current->link!=NULL)
{
q=current->link;
current->link=q->link;
delete q;
}
}
template<class T>
int List<T>::Length() const
{
int count=0;
LinkNode<T>*current=first->link;
while(current!=NULL)
{
current=current->link;++count;
}
return count;
}
template<class T>
int List<T>::Search(const T &x)
{
LinkNode<T>*current=first->link;
int count=0;
while(current!=NULL)
{
++count;
if(current->data==x)
return count;
else
current=current->link;
}
}
template<class T>
LinkNode<T>*List<T>::Locate(int i)
{
if(i<0) return NULL;
LinkNode<T>*current=first;
for(int j=0;j<i;++j)
{
current=current->link;
if(current==NULL)
return current;
}
return current;
}
template<class T>
bool List<T>::Insert(int i,T x)
{
LinkNode<T>*current=Locate(i);
LinkNode<T>*newNode=new LinkNode<T>(x);
if(newNode==NULL)
{
cerr<<"存储分配错误!"<<endl;
exit(1);
}
newNode->link=current->link;
current->link=newNode;
return true;
}
template<class T>
bool List<T>::Remove(int i,T &x)
{
LinkNode<T>*current=Locate(i-1);
LinkNode<T>*del;
del=current->link;
x=del->data;
current->link=del->link;
return true;
}
template<class T>
void List<T>::Input(int endTag)
{
int val;
LinkNode<T>*last=first;
cin>>val;
LinkNode<T>*newNode;
while(val!=endTag)
{
newNode=new LinkNode<T>(val);
last->link=newNode;
last=newNode;
cin>>val;
}
}
template<class T>
void List<T>::Output()
{
LinkNode<T>*current=first->link;
while(current!=NULL)
{
cout<<current->data<<" ";
current=current->link;
}
}
int main()
{
List<int> A;
A.Input(0);
A.Output();
system("pause");
return 0;
}
using namespace std;
template<class T>
struct LinkNode
{
T data;
LinkNode<T>*link;
LinkNode(LinkNode<T>*ptr=NULL) { link=ptr;}
LinkNode(T item,LinkNode<T>*ptr=NULL) { data=item;link=ptr;}
};
template<class T>
class List
{
private:
LinkNode<T> *first;
public:
List() { first=new LinkNode<T>;}
List(const T &x) { first=new LinkNode<T>(x);} //常值引用
List(List<T>&L);
~List() { makeEmpty();}
void makeEmpty();
int Length() const;
int Search(const T &x); //常值引用
LinkNode<T>*GetHead() { return first;}
LinkNode<T>*Locate(int i);
bool Insert(int i,T x);
bool Remove(int i,T &x);
bool IsEmpty() { return (first->link==NULL)?true:false;}
void Input(int i);
void Output();
};
template<class T>
List<T>::List(List<T>&L)
{
LinkNode<T>*desptr=first=new LinkNode<T>;
LinkNode<T>*srcptr=L.GetHead();
T value;
while(srcptr->link!=NULL)
{
value=srcptr->link->data;
desptr->link=new LinkNode<T>(value);
desptr=desptr->link;
srcptr=srcptr->link;
}
desptr->link=NULL;
}
template<class T>
void List<T>::makeEmpty()
{
LinkNode<T>*current=first;
LinkNode<T>*q;
while(current->link!=NULL)
{
q=current->link;
current->link=q->link;
delete q;
}
}
template<class T>
int List<T>::Length() const
{
int count=0;
LinkNode<T>*current=first->link;
while(current!=NULL)
{
current=current->link;++count;
}
return count;
}
template<class T>
int List<T>::Search(const T &x)
{
LinkNode<T>*current=first->link;
int count=0;
while(current!=NULL)
{
++count;
if(current->data==x)
return count;
else
current=current->link;
}
}
template<class T>
LinkNode<T>*List<T>::Locate(int i)
{
if(i<0) return NULL;
LinkNode<T>*current=first;
for(int j=0;j<i;++j)
{
current=current->link;
if(current==NULL)
return current;
}
return current;
}
template<class T>
bool List<T>::Insert(int i,T x)
{
LinkNode<T>*current=Locate(i);
LinkNode<T>*newNode=new LinkNode<T>(x);
if(newNode==NULL)
{
cerr<<"存储分配错误!"<<endl;
exit(1);
}
newNode->link=current->link;
current->link=newNode;
return true;
}
template<class T>
bool List<T>::Remove(int i,T &x)
{
LinkNode<T>*current=Locate(i-1);
LinkNode<T>*del;
del=current->link;
x=del->data;
current->link=del->link;
return true;
}
template<class T>
void List<T>::Input(int endTag)
{
int val;
LinkNode<T>*last=first;
cin>>val;
LinkNode<T>*newNode;
while(val!=endTag)
{
newNode=new LinkNode<T>(val);
last->link=newNode;
last=newNode;
cin>>val;
}
}
template<class T>
void List<T>::Output()
{
LinkNode<T>*current=first->link;
while(current!=NULL)
{
cout<<current->data<<" ";
current=current->link;
}
}
int main()
{
List<int> A;
A.Input(0);
A.Output();
system("pause");
return 0;
}