c语言链表问题,求高手指点
#defineNULL0structstudent{longnum;floatscore;structstudent*next;};intn;structstudent*...
#define NULL 0
struct student
{
long num;
float score;
struct student *next;
};
int n;
struct student *creat(void)
{
struct student *head,*p1,*p2;
p1=p2=(struct student*)malloc(sizeof(struct student));
scanf("%ld,%f",&p1->num,&p1->score);
head=NULL;
n=0;
while(p1->num!=0)
{
n=n+1;
if(n==1)head=p1;
else p2->next=p1;
p2=p1;
p1=(struct student*)malloc(sizeof(struct student));
scanf("%ld,%f",&p1->num,&p1->score);
}
p2->next=NULL;
return head;
}
void shuchu(struct student *p)
{
while(p->next!=0)
{
printf("the num is %ld,the score is %f\n",p->num,p->score);
p=p->next;
}
}
int main ( )
{
struct student *q;
q=creat( );
shuchu(q);
return 0;
}
我的程序最后一组数据无法输出,是我shuchu函数里面我不知道条件怎么设置才能全部输出,还有我想输完数据后回车以后还能再输该怎么办 展开
struct student
{
long num;
float score;
struct student *next;
};
int n;
struct student *creat(void)
{
struct student *head,*p1,*p2;
p1=p2=(struct student*)malloc(sizeof(struct student));
scanf("%ld,%f",&p1->num,&p1->score);
head=NULL;
n=0;
while(p1->num!=0)
{
n=n+1;
if(n==1)head=p1;
else p2->next=p1;
p2=p1;
p1=(struct student*)malloc(sizeof(struct student));
scanf("%ld,%f",&p1->num,&p1->score);
}
p2->next=NULL;
return head;
}
void shuchu(struct student *p)
{
while(p->next!=0)
{
printf("the num is %ld,the score is %f\n",p->num,p->score);
p=p->next;
}
}
int main ( )
{
struct student *q;
q=creat( );
shuchu(q);
return 0;
}
我的程序最后一组数据无法输出,是我shuchu函数里面我不知道条件怎么设置才能全部输出,还有我想输完数据后回车以后还能再输该怎么办 展开
展开全部
struct student *creat(void)
{
struct student *head,*p1,*p2;creat
p1=(struct student*)malloc(sizeof(struct student));
scanf("%ld,%f",&p1->num,&p1->score);
head=p1;
p2=p1;
while(p1->num!=0)
{
p1=(struct student*)malloc(sizeof(struct student));
p1->next=NULL;
p2->next=p1;
p2=p1;
scanf("%ld,%f",&p1->num,&p1->score);
}
p2->next=NULL;
return head;
}
你的creat函数有问题 我把你的creat函数改成上面的程序 就OK了。
{
struct student *head,*p1,*p2;creat
p1=(struct student*)malloc(sizeof(struct student));
scanf("%ld,%f",&p1->num,&p1->score);
head=p1;
p2=p1;
while(p1->num!=0)
{
p1=(struct student*)malloc(sizeof(struct student));
p1->next=NULL;
p2->next=p1;
p2=p1;
scanf("%ld,%f",&p1->num,&p1->score);
}
p2->next=NULL;
return head;
}
你的creat函数有问题 我把你的creat函数改成上面的程序 就OK了。
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询