#include<iostream>
using namespace std;
typedef struct head
{
int a;
struct head *next;
}NLode;
void intial(NLode **head)
{
*head=(NLode *)malloc(sizeof(NLode));
(*head)->next=NULL;
}
void insert(NLode *head,int a)
{
NLode *p,*q;
p=head;
q=(NLode*)malloc (sizeof(NLode));
while(p->next!=NULL)
{
p=p->next;
}
q->a=a;
q->next=p->next;
p->next=q;
}
NLode * reserve(NLode *head)
{
NLode *p,*q,*r;
p=head;
q=head->next;
p->next=NULL;
while(q!=NULL)
{
r=q;
q=q->next;
r->next=p;
p=r;
}
return r;
}
void printSL(NLode *head)
{
NLode *p;
p=head->next;
while(p!=NULL)
{
cout<<p->a<<" ";
p=p->next;
}
}
void printSLR(NLode *head)
{
while(head!=NULL&&head->next!=NULL)
{
cout<<head->a;
head=head->next;
}
}
void main()
{
NLode *head1,*head2,*head3,*head4;
intial(&head1);
int a,b,num=0,i;
cin>>a;
for(i=0;i<a;i++)
{
cin>>num;
insert(head1,num);
}
cout<<"A:"<<endl;
printSL(head1);
cout<<endl;
cout<<"B:"<<endl;
head4=reserve(head1);
printSLR(head4);
system("pause");
}
链表翻转
最新推荐文章于 2020-02-21 16:09:35 发布