请用一个单链表来保存学生数据,链表中的每个结点包含的数据信息有学生的学号和一门课的成绩。编写一个crea
然后我的程序是这样写的#include<stdio.h>#include<stdlib.h>structstud{intnum;floatscore;structstud...
然后我的程序是这样写的
#include<stdio.h>
#include<stdlib.h>
struct stud
{
int num;
float score;
struct stud *next;
};
int main()
{
struct stud *create();
void print(struct stud *head);
struct stud *head;
head=create();
print(head);
}
struct stud *create()
{
struct stud *head,*p1,*p2;
int n=0;
head = NULL;
p2 = p1 = (struct stud*)malloc(sizeof(struct stud));
printf("Please input student's num and score:\n");
scanf("%d %f",&p1->num ,&p1->score );
while(p1->num !=0)
{
n=n+1;
if(n==1)
head = p1;
else
p2->next = p1;
p2 = p1;
p1 = (struct stud*)malloc(sizeof(struct stud));
scanf("%d %f",&p1->num ,&p1->score );
}
p2->next =NULL;
return head;
}
void print(struct stud *head)
{
struct stud *p;
p=head;
printf("Output student's num and score:\n");
while(p!=NULL)
{
printf("%-5d%5.1f\n",p->num ,p->score );
p=p->next ;
}
}
但是需要输入两个0才能结束运行,请帮忙改下只需要输入一个0就可以结束的 展开
#include<stdio.h>
#include<stdlib.h>
struct stud
{
int num;
float score;
struct stud *next;
};
int main()
{
struct stud *create();
void print(struct stud *head);
struct stud *head;
head=create();
print(head);
}
struct stud *create()
{
struct stud *head,*p1,*p2;
int n=0;
head = NULL;
p2 = p1 = (struct stud*)malloc(sizeof(struct stud));
printf("Please input student's num and score:\n");
scanf("%d %f",&p1->num ,&p1->score );
while(p1->num !=0)
{
n=n+1;
if(n==1)
head = p1;
else
p2->next = p1;
p2 = p1;
p1 = (struct stud*)malloc(sizeof(struct stud));
scanf("%d %f",&p1->num ,&p1->score );
}
p2->next =NULL;
return head;
}
void print(struct stud *head)
{
struct stud *p;
p=head;
printf("Output student's num and score:\n");
while(p!=NULL)
{
printf("%-5d%5.1f\n",p->num ,p->score );
p=p->next ;
}
}
但是需要输入两个0才能结束运行,请帮忙改下只需要输入一个0就可以结束的 展开
1个回答
展开全部
#include<stdio.h>
#include<stdlib.h>
struct stud
{
int num;
float score;
struct stud *next;
};
int main()
{
struct stud *create();
void print(struct stud *head);
struct stud *head;
head=create();
print(head);
}
struct stud *create()
{
struct stud *head,*p1,*p2;
int n=0;
head = NULL;
p2 = p1 = (struct stud*)malloc(sizeof(struct stud));
printf("Please input student's num and score:\n");
scanf("%d",&p1->num ); //修改的地方
if( p1->num == 0 ){ p2->next=NULL;return head;}
scanf("%f",&p1->score);
while(p1->num !=0)
{
n=n+1;
if(n==1)
head = p1;
else
p2->next = p1;
p2 = p1;
p1 = (struct stud*)malloc(sizeof(struct stud));
scanf("%d",&p1->num ); //修改的地方
if( p1->num == 0 ){ p2->next=NULL;return head;}
scanf("%f",&p1->score);
}
p2->next =NULL;
return head;
}
void print(struct stud *head)
{
struct stud *p;
p=head;
printf("Output student's num and score:\n");
while(p!=NULL)
{
printf("%-5d%5.1f\n",p->num ,p->score );
p=p->next ;
}
}
#include<stdlib.h>
struct stud
{
int num;
float score;
struct stud *next;
};
int main()
{
struct stud *create();
void print(struct stud *head);
struct stud *head;
head=create();
print(head);
}
struct stud *create()
{
struct stud *head,*p1,*p2;
int n=0;
head = NULL;
p2 = p1 = (struct stud*)malloc(sizeof(struct stud));
printf("Please input student's num and score:\n");
scanf("%d",&p1->num ); //修改的地方
if( p1->num == 0 ){ p2->next=NULL;return head;}
scanf("%f",&p1->score);
while(p1->num !=0)
{
n=n+1;
if(n==1)
head = p1;
else
p2->next = p1;
p2 = p1;
p1 = (struct stud*)malloc(sizeof(struct stud));
scanf("%d",&p1->num ); //修改的地方
if( p1->num == 0 ){ p2->next=NULL;return head;}
scanf("%f",&p1->score);
}
p2->next =NULL;
return head;
}
void print(struct stud *head)
{
struct stud *p;
p=head;
printf("Output student's num and score:\n");
while(p!=NULL)
{
printf("%-5d%5.1f\n",p->num ,p->score );
p=p->next ;
}
}
本回答被提问者采纳
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询