#include<stdio.h>
#include<stdlib.h>
#define NULL 0
struct student
{long num;
float score;
struct student *next;
};
int n;
struct student *creat(void)
{
struct student *head,*p1,*p2;
head=NULL;
n=0;
p1=(struct student *)malloc(sizeof(struct student));
p2=p1;
scanf("%ld %f",&p1->num,&p1->score);
while(p1->num!=0)
{
n++;
if(head==NULL) 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);
}
int main()
{
struct student *pt;
pt=creat();
}
為啥會出現這樣的錯誤
uj5u.com熱心網友回復:
if(head==NULL) head=p1;else p2->next=p1;
假設head不為空,那么這個鏈表和head就沒啥關系了,最后回傳head顯然是不正確的。
p2=p1; p2=head試試看吧
轉載請註明出處,本文鏈接:https://www.uj5u.com/houduan/234062.html
標籤:新手樂園
上一篇:零起點演算法104:第幾天?
下一篇:萌新關于結構體的求助
