C++建立动态链表输出学生学号和成绩时出错
#include<iostream>#define NULL 0
using namespace std;
struct Student{
int num;
float score;
struct Student *next;
};
Student *create(void)
{
Student *head=NULL,*p1,*p2;
int n=0;
p1=p2=new Student;
cin>>p1->num>>p1->score;
while(p1->num!=0)
{
n=n+1;
if (n==1)
head=p1;
else
p1=new Student;
p2->next=p1;
p2=p1;
cin>>p1->num>>p1->score;
}
p2=NULL;
return head;
}
int main()
{
Student *head;
head=create();
do
{
cout<<head->num<<' '<<head->score<<endl;
head=head->next;
}while(head!=NULL);
return 0;
}
想建立一个动态链表以输出学生学号和成绩,但运行时出错,不知该怎么改。