链表的调试
/*指针2_22题*//*单向链表的降序排列*/
#include<stdio.h>
#include<stdlib.h>
#define LEN sizeof(struct member)
/*足球队员记录的结构体*/
struct member
{
char name[20];
char club[20];
float height;
int old;
struct member *next;
};
/*建立一个动态的单向链表*/
struct member *creat()
{
struct member *head,*p1,*p2;
p1=p2=(struct member*)malloc(LEN);
head=NULL;
printf("输入足球队员的记录(姓名,所属俱乐部,身高,年龄):\n");
scanf("%s,%s,%f,%d",p1->name,p1->club,&p1->height,&p1->old);
while(p1->old!=0) /*队员的年龄为0表示结束*/
{
(head==NULL)?(head=p1):(p2->next=p1);
p2=p1;
p1=(struct member*)malloc(LEN);
printf("输入足球队员的记录(姓名,所属俱乐部,身高,年龄):\n");
scanf("%s,%s,%f,%d",p1->name,p1->club,&p1->height,&p1->old);
}
p2->next=NULL;
return head;
}
/*单向链表的排序(身高从大到小)*/
struct member *paixu(struct member *head)
{
struct member *p,*p1,*p2,*temp1,*temp2;
for(p1=head;p1->next!=NULL;p1=p1->next)
{
p=p1;
for(p2=p->next;p2!=NULL;p2=p2->next)
if(p->old<p2->old)p=p2; /*p向身高最高的结点*/
/*要与头结点交换*/
if((p1=head)&&(p!=p1))
{
head=p2;
temp1=p2->next;
p2->next=p1->next;
p2=p1;
p1->next=temp1;
}
/*不与头结点交换*/
if((p1!=head)&&(p!=p1))
{
temp1=p1;
p1=p1->next;
temp2=p2->next;
p2->next=temp1;
p1->next=temp2;
}
}
return head;
}
/*输出链表*/
void print(struct member *head)
{
struct member *p;
p=head;
while(p!=NULL)
{
printf("姓名:%s,所属俱乐部:%s,升高:%f,年龄:%d",p->name,p->club,&p->height,&p->old);
p=p->next;
}
}
int main()
{
struct member *head;
head=creat();
head=paixu(head);
print(head);
return 0;
}
问题是:编译连接过程都没有出现问题,但是在执行的时候,总是陷入输入的死循环。。。。