#include<iostream>
using namespace std;
struct node
{
int data;
node *link;
};
node *create_link()
{
node *head,*p1,*p2;
int n=0;
head=NULL;
p1=p2=new node;
cin>>p1->data;
if(p1->data==0) return NULL;
while(p1->data!=0)
{
n++;
if(n==1) head=p1;
else p2->link=p1;
p2=p1;
p1=new node;
cin>>p1->data;
}
p2->link=head;
delete(p1);
return head;
}
node *reverse_link(node *head)
{
node *p1,*p2,*p3;
if(head=NULL) return head;
p1=p3=head;
p2=NULL;
while(p3->link!=head) p3=p3->link;
while(head!=p3)
{
head=p1->link;
if(p2==NULL) p1->link=p3;
else p1->link=p2;
p2=p1;
p1=head;
}
p3->link=p2;
return head;
}
void out_link(node *head)
{
node *p;
p=head;
cout<<"Start-->";
do
{
if(p==NULL) return;
cout<<p->data<<"-->";
p=p->link;
}while(p!=head);
cout<<"End"<<endl<<endl;
}
void main()
{
node *head;
cout<<"Please input datas and end with 0:"<<endl;
head=create_link();
cout<<endl<<"The previous datas are:"<<endl;
out_link(head);
head=reverse_link(head);
cout<<"Reverse the link:"<<endl;
out_link(head);
}
using namespace std;
struct node
{
int data;
node *link;
};
node *create_link()
{
node *head,*p1,*p2;
int n=0;
head=NULL;
p1=p2=new node;
cin>>p1->data;
if(p1->data==0) return NULL;
while(p1->data!=0)
{
n++;
if(n==1) head=p1;
else p2->link=p1;
p2=p1;
p1=new node;
cin>>p1->data;
}
p2->link=head;
delete(p1);
return head;
}
node *reverse_link(node *head)
{
node *p1,*p2,*p3;
if(head=NULL) return head;
p1=p3=head;
p2=NULL;
while(p3->link!=head) p3=p3->link;
while(head!=p3)
{
head=p1->link;
if(p2==NULL) p1->link=p3;
else p1->link=p2;
p2=p1;
p1=head;
}
p3->link=p2;
return head;
}
void out_link(node *head)
{
node *p;
p=head;
cout<<"Start-->";
do
{
if(p==NULL) return;
cout<<p->data<<"-->";
p=p->link;
}while(p!=head);
cout<<"End"<<endl<<endl;
}
void main()
{
node *head;
cout<<"Please input datas and end with 0:"<<endl;
head=create_link();
cout<<endl<<"The previous datas are:"<<endl;
out_link(head);
head=reverse_link(head);
cout<<"Reverse the link:"<<endl;
out_link(head);
}