链表反向:
template <typename T>
class Node
{
public:
T data;
Node* next;
};
template <typename T>
class link
{
public:
link()
{
head=new Node<T>();
head->data=1;
head->next=NULL;
Node<T>* pNew=NULL;
Node<T>* p=NULL;
p=head;
while(true)
{
T i;
cin>>i;
if(i!=-1)
{
pNew=new Node<T> ();
pNew->data=i;
pNew->next=NULL;
p->next=pNew;
p=pNew;
}
else
{
break;
}
}
}
void reverseLink()
{
assert(head!=NULL);
Node<T>* previouse=NULL;
Node<T>* p=head;
Node<T>* tmp=NULL;
while(p!=NULL)
{
tmp=p->next;
p->next=previouse;
previouse=p;
p=tmp;
}
head=previouse;
}
void printLink()
{
Node<T>* p=head;
while(p!=NULL)
{
cout<<p->data<<" ";
p=p->next;
}
}
private:
Node<T>* head;
};