#include<iostream>
using namespace std;
template<class T>
class LinkNode{
public:
T data;
LinkNode<T> *pNext;
LinkNode(T data = 0){
this->data = data;
this->pNext = NULL;
}
};
template<class T>
class Link{
private:
LinkNode<T> *pHead;
LinkNode<T> *pTail;
int size;
public:
Link(int size = 0){
this->pHead = NULL;
this->pTail = NULL;
this->size = 0;
}
void push(T data){
LinkNode<T> *x = new LinkNode<T>(data);
if(pTail == NULL)
pHead = pTail = x;
else
{
pTail->pNext = x;
pTail = x;
}
size += 1;
}
void show(){
LinkNode<T> *p = this->pHead;
for(;p!=pTail;p = p->pNext){
cout << p->data << " ";
}
cout << pTail->data;
cout << endl;
}
void invert();
};
template<class T>
void Link<T> :: invert(){
LinkNode<T> *pNewHead = pTail;
LinkNode<T> *p = pHead;
while(pTail != pHead)
{
for(p = pHead;p->pNext != pTail;p = p->pNext);
pTail->pNext = p;
pTail=p;
cout << pTail ->data ;
}
pHead = pNewHead;
}
int main(){
LinkNode<char> *p = new LinkNode<char>('p');
Link<char> *li = new Link<char>;
cout << "请输入各结点数据值(输入0结束):";
char c;
cin >> c;
while(c != '0'){
li-> push(c);
cin >> c;
}
cout << "构建的链表为:" << endl;
li->show();
li->invert();
cout << "倒置后的链表为:" << endl;
li->show();
return 0;
}