帮我看一下这个简单程序
来源:百度知道 编辑:UC知道 时间:2024/06/04 21:28:45
#include<stdio.h>
#include<malloc.h>
struct stu{
int num;
int score;
struct stu * next;
};
struct stu * creat()
{
struct stu *head,*p1,*p2;
p2=p1=(struct stu*)malloc(sizeof(struct stu));
head=p1;p1->next=NULL;
p1=(struct stu*)malloc(sizeof(struct stu));
scanf("%d%d",&p1->num,&p1->score);
while(p1->num!=0){
p2->next=p1;p2=p1;
p1=(struct stu*)malloc(sizeof(struct stu));
scanf("%d%d",&p1->num,&p1->score);}
p2->next=NULL;
free(p1);
return head;
}
void print(struct stu * head)
{
if(head->next==NULL)printf("It's NULL!\n");
while(head->next){
printf("%d %d\n",(head->next)->num,(head->next)->score);
head=head->next;
}
}
int main()
{
struct stu *p;
p=creat();
print(p);
#include<malloc.h>
struct stu{
int num;
int score;
struct stu * next;
};
struct stu * creat()
{
struct stu *head,*p1,*p2;
p2=p1=(struct stu*)malloc(sizeof(struct stu));
head=p1;p1->next=NULL;
p1=(struct stu*)malloc(sizeof(struct stu));
scanf("%d%d",&p1->num,&p1->score);
while(p1->num!=0){
p2->next=p1;p2=p1;
p1=(struct stu*)malloc(sizeof(struct stu));
scanf("%d%d",&p1->num,&p1->score);}
p2->next=NULL;
free(p1);
return head;
}
void print(struct stu * head)
{
if(head->next==NULL)printf("It's NULL!\n");
while(head->next){
printf("%d %d\n",(head->next)->num,(head->next)->score);
head=head->next;
}
}
int main()
{
struct stu *p;
p=creat();
print(p);
这个很不好
scanf("%d%d",&p1->num,&p1->score); //会引起一系列问题
建议
scanf("%d %d",&p1->num,&p1->score);
以空格表示两个数字分开。输入时也是以空格表示两个数字的分隔。
虽然看着有点累
但你的程序应该没问题啊
我编译了一下,没错误,运行也正常
看着是累