#include <iostream>
using namespace std;
#define N 5
struct linka
{
int data;
linka* next;
};
linka*head;
linka* Create()
{
linka*p=NULL;
linka*q=NULL;
head=NULL;
int n=0,i;
for(;n<N;n++)
{ cout<<"please enter the data:"<<endl;
cin>>i;
p=new linka;
p->data=i;
if(head==NULL)
head=p;
else
q->next=p;
q=p;
}
if(head!=NULL)
q->next=NULL;
return head;
}
void DisplayList(linka* head)
{
while(head!=NULL)
{
cout<<head->data<<" ";
head=head->next;
}
}
linka*reverse1(linka* head)
{
if(head ==NULL)
return 0;
linka*pre, *cur, *ne;
pre=head;
cur=head->next;
while(cur)
{
ne = cur->next;
cur->next = pre;
pre = cur;
cur = ne;
}
head->next = NULL;
head = pre;
return head;
}
linka* reverse2(linka* head)
{
//linka* temp=new ActList;
if(NULL==head|| NULL==head->next) return head;
linka* p;
linka* q;
linka* r;
p = head;
q = head->next;
head->next = NULL;
while(q){
r = q->next;
q->next = p;
p = q;
q = r;
}
head=p;
return head;
}
linka* reverse3(linka* head)
{
linka* p;
linka* q;
p=head->next;
while(p->next!=NULL){
q=p->next;
p->next=q->next;
q->next=head->next;
head->next=q;
}
p->next=head;//相当于成环
head=p->next->next;//新head变为原head的next
p->next->next=NULL;//断掉环
return head;
}
int main()
{ linka*p;
p=Create();
cout<<"show the former program:"<<endl;
DisplayList(p);
cout<<endl;
/* cout<<"show the reversed1 program:"<<endl;
DisplayList(reverse1(p));
cout<<endl;
cout<<"show the reversed2 program:"<<endl;
DisplayList(reverse2(p));
cout<<endl;*/
cout<<"show the reversed3 program:"<<endl;
DisplayList(reverse3(p));
cout<<endl;
return 0;
}
评论0