void fanxu_link_list(nodelink *head)/*尾插倒序*/
{
nodelink p,pre,p1;
p1=*head;
if(p1==NULL)
{
printf("The link is empty!");
exit(1);
}
p=p1->next;
p1->next=NULL;
if(p!=NULL)
pre=p;
while(p!=NULL)
{
p=p->next;
pre->next=p1;
p1=pre;
pre=p;
}
*head=p1;
}
看不明白!
以前写过
#include<malloc.h>
#define LEN sizeof(DATA)
typedef struct PERSON
{
int num;
struct PERSON *next;
}DATA;
DATA *creat(void)
{
DATA *head,*p1,*p2;
p1=p2=(DATA *)malloc(LEN);
scanf("%d",&p1->num);
head=NULL;
while(p1->num)
{
if(head==NULL)head=p1;
else p2->next=p1;
p2=p1;
p1=(DATA *)malloc(LEN);
scanf("%d",&p1->num);
}
p2->next=NULL;
return head;
}
DATA *sort(DATA *head)
{
DATA *head1,*p1;
int flag=0;
head1=NULL;
p1=head;
while(p1)
{
flag=flag+1;
head=p1->next;
if(flag==1)
{
head1=p1;
head1->next=NULL;
}
else
{
p1->next=head1;
head1=p1;
}
p1=head;
}
return head1;
}
main()
{
DATA *head,*p;
printf("\ninput data [num]:\n");
head=creat();
head=sort(head);
printf("\noutput data have been sorted:\n");
p=head;
if(head==NULL)printf("\nLink null!\n");
else
{
while(p)
{
printf("%d ",p->num);
p=p->next;
}
}
getch();
}