#include<iostream>
#include<string>
using namespace std;
/******************这是带有头节点的单链表的逆置××××××××××××××*/
template <class Type>
struct P
{
Type data;
P<Type> *next;
};
template <class Type>
class pc
{
public:
P<Type> *head;
P<Type> *tail;
public:
void init();
void show();
void insert_first();
void nizhi();
void destory();
};
template <class Type>
void pc<Type>::init()
{
head=new P<Type>;
tail=head;
cout<<this->head->data<<endl;
head->next=NULL;
}
template <class Type>
void pc<Type>::show()
{
if(head==NULL)
{
cout<<"链表为空"<<endl;
}
else
{
P<Type> *link=head->next;
cout<<"数据为: ";
while(link)
{
cout<<link->data<<" ";
link=link->next;
}
cout<<endl;
}
}
template <class Type>
void pc<Type>::insert_first()
{
cout<<"请输入数据"<<endl;
P<Type> *link=new P<Type>;
cin>>link->data;
if(head->next==NULL)
{
head->next=link;
link->next=NULL;
tail=link;
}
else
{
head->next=link;
link->next=tail;
tail=link;
}
show();
}
template <class Type>
void pc<Type>::nizhi()
{
P<Type> *p1,*p2,*p3;
p1=head;
p2=p1->next;
while(p2)
{
p3=p2->next;
p2->next=p1;
p1=p2;
p2=p3;
}
head->next=NULL;
p3=p1;
cout<<"逆置之后的数据" ;
while(p1!=head)
{
cout<<p1->data<<" ";
p1=p1->next;
}
cout<<endl;
// return p3;
}
template <class Type>
void pc<Type>::destory()
{
P<Type> *p1=head;
while(head)
{
head=head->next;
delete p1;
p1=head;
}
p1=head=NULL;
if(p1!=NULL||head!=NULL)
{
cout<<"内存有问题"<<endl;
}
}
//template <class Type>
int main()
{
pc<string> code;
code.init();
code.insert_first();
code.insert_first();
code.insert_first();
code.nizhi();
//code.destory();
// code.show();
return 0;
}