写一个非形式算法。
大家给我个思路啊!
#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();
}