#include
#include
# define LEN sizeof(struct biao)
struct biao
{
int num;
char name[20];
struct biao *next;
};
int n;
struct biao *create()
{
struct biao *head,*p1,*p2;
n=0;
p1=p2=(struct biao *)malloc(LEN);
scanf("%d%s",&p1->num,p1->name);
head=NULL;
while(p1->num!=0)
{
n=n+1;
if(n==1)head=p1;
else p2->next=p1;
p2=p1;
p1=(struct biao *)malloc(LEN);
scanf("%d%s",&p1->num,p1->name);
}
p2->next=NULL;
return(head);
}
void print(struct biao *head)
{
struct biao *p;
p=head;
if(p!=NULL)
do
{
printf("num:%d\tname:%s\t\n",p->num,p->name);
p=p->next;
}while(p!=NULL);
else printf("链表为空!\n");
}
struct biao *sort(struct biao *head)/*此函数为按逆序排列函数*/
{
struct biao *p1,*p2,*p3;
p1=head;
if(p1==NULL)goto end;
else
{
if(p1->next==NULL)goto end;
else
{
p2=p1->next;
while(p2->next!=NULL)
{
if(p1==head){p3=p2->next;p1->next=NULL;p2->next=p1;p1=p2;p2=p3;p3=p3->next;}
else
{
p2->next=p1;
p1=p2;p2=p3;p3=p3->next;
}
}
}
}
if(p2->next==NULL){head=p2;p2->next=p1;}
end:
return head;
}
int main()
{
struct biao *p;
printf("说明:\n链表成员包括“编号”和“名称”。\n请对链表赋值:");
p=create();
print(p);
p=sort(p);
printf("逆序排列完毕!\n");
print(p);
return 0;
}