主题:求助:链表问题!
源程序如下:
/*链表程序1*/
#include<stdio.h>
struct student{
int num;
char name[40];
float score;
struct student *next;
};
main()
{
int n=0;
struct student *p1,*p2,*head,*print;
p1=p2=(struct student *)malloc(sizeof(struct student));
/*输入数据*/
scanf("%d %s %f",&p1->num,p1->name,&p1->score);
while(p1->num!=0)
{
n++;
if(n==1)head=p1;
else p2->next=p1;
p2=p1;
p1=(struct student *)malloc(sizeof(struct student));
scanf("%d %s %f",&p1->num,p1->name,&p1->score);
}
p2->next=0;
/*Over!*/
printf("Done!Press any key to print!\n");
getch();
/*输出*/
print=head;
while(print!=0)
{
printf("Num:%d Name:%s Score:%f\n",&print->num,print->name,print->score);
print=print->next;
}
printf("Done!\n");
getch();
}
本意:输入多个学号 姓名 分数直至输入0时结束,后输出已存入内容。
举例:
输入:
1 Lina 100
2 John 100
3 Luna 100
0
输出:
Num:1 Name: Lina Score:100.00
Num:2 Name: John Score:100.00
Num:3 Name: Luna Score:100.00
但结果不符,请帮助我解决这个问题,谢谢!
/*链表程序1*/
#include<stdio.h>
struct student{
int num;
char name[40];
float score;
struct student *next;
};
main()
{
int n=0;
struct student *p1,*p2,*head,*print;
p1=p2=(struct student *)malloc(sizeof(struct student));
/*输入数据*/
scanf("%d %s %f",&p1->num,p1->name,&p1->score);
while(p1->num!=0)
{
n++;
if(n==1)head=p1;
else p2->next=p1;
p2=p1;
p1=(struct student *)malloc(sizeof(struct student));
scanf("%d %s %f",&p1->num,p1->name,&p1->score);
}
p2->next=0;
/*Over!*/
printf("Done!Press any key to print!\n");
getch();
/*输出*/
print=head;
while(print!=0)
{
printf("Num:%d Name:%s Score:%f\n",&print->num,print->name,print->score);
print=print->next;
}
printf("Done!\n");
getch();
}
本意:输入多个学号 姓名 分数直至输入0时结束,后输出已存入内容。
举例:
输入:
1 Lina 100
2 John 100
3 Luna 100
0
输出:
Num:1 Name: Lina Score:100.00
Num:2 Name: John Score:100.00
Num:3 Name: Luna Score:100.00
但结果不符,请帮助我解决这个问题,谢谢!