c语言链表问题:为什么我写的这个程序只能输出最后一个的输入?
c语言链表问题:为什么我写的这个程序只能输出最后一个的输入?#include<stdio.h>#include<malloc.h>structstudent{intnum...
c语言链表问题:为什么我写的这个程序只能输出最后一个的输入?#include<stdio.h>
#include<malloc.h>
struct student
{
int num;
float score;
struct student *next;
};
struct student *f();
void print(struct student *head);
int n;
void main()
{
struct student *stu;
stu=f();
print(stu);
}
struct student *f()
{
struct student *head;
struct student *p1,*p2;
p1=p2=(struct student *)malloc(sizeof (struct student));
printf("请输入学号:");
scanf("%d",&p1->num);
printf("请输入成绩:");
scanf("%f",&p1->score);
head=NULL;
n=0;
while (p1->num)
{
n++;
if (n=1)
{
head=p1;
}
else
{
p2->next=p1;
}
p2=p1;
p1=(struct student *)malloc(sizeof (struct student));
printf("请输入学号:");
scanf("%d",&p1->num);
printf("请输入成绩:");
scanf("%f",&p1->score);
}
p2->next=NULL;
return head;
}
void print(struct student *head)
{
struct student *p;
printf("共输入%d位学生\n",n);
p=head;
do
{ printf("学号:%d\t成绩:%f\n",p->num,p->score);
p=p->next;
}
while (p);
} 展开
#include<malloc.h>
struct student
{
int num;
float score;
struct student *next;
};
struct student *f();
void print(struct student *head);
int n;
void main()
{
struct student *stu;
stu=f();
print(stu);
}
struct student *f()
{
struct student *head;
struct student *p1,*p2;
p1=p2=(struct student *)malloc(sizeof (struct student));
printf("请输入学号:");
scanf("%d",&p1->num);
printf("请输入成绩:");
scanf("%f",&p1->score);
head=NULL;
n=0;
while (p1->num)
{
n++;
if (n=1)
{
head=p1;
}
else
{
p2->next=p1;
}
p2=p1;
p1=(struct student *)malloc(sizeof (struct student));
printf("请输入学号:");
scanf("%d",&p1->num);
printf("请输入成绩:");
scanf("%f",&p1->score);
}
p2->next=NULL;
return head;
}
void print(struct student *head)
{
struct student *p;
printf("共输入%d位学生\n",n);
p=head;
do
{ printf("学号:%d\t成绩:%f\n",p->num,p->score);
p=p->next;
}
while (p);
} 展开
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询