C++动态连表建立函数

来源:百度知道 编辑:UC知道 时间:2024/05/22 05:04:49
#include<iostream>
#define LEN sizeof(struct stu)
using namespace std;
struct stu
{
long num;
float score;
struct stu *next;
};
int n ;
struct stu *creat(void)
{
struct stu *head,*p1,*p2;
n=0;
p1=p2=(struct stu *)new(LEN);
cin>>p1->num>>p1->score;
head=0;
while(p1->num!=0)
{n=n+1;
if(n==1)head=p1;
else p2->next=p1;
p2=p1;
p1=(struct stu*)new(LEN);
cin>>p1->num>>p1->score;
}
p2->next=0;
return (head);
}
int main()
{
creat();
return 0;
}
有错误

E:\C++\新建文件夹\新建文件夹 (2)\新建文件夹\新建文件夹\新建文件夹 (2)\新建文件夹\新建文件夹\新建文件夹\新建文件夹\新建文件夹\新建文件夹\新建文件夹\Cpp1.cpp(19) : error C2143: syntax error : missing ';' before '{'
E:&

#include<iostream>
#define LEN sizeof(struct stu)
using namespace std;
struct stu
{
long num;
float score;
struct stu *next;
};
int n ;
struct stu *creat(void)
{
struct stu *head,*p1,*p2;
n=0;
p1=p2=new stu;
cin>>p1->num>>p1->score;
head=0;
while(p1->num!=0)
{n=n+1;
if(n==1)head=p1;
else p2->next=p1;
p2=p1;
p1=new stu;
cin>>p1->num>>p1->score;
}
p2->next=0;
return (head);
}
int main()
{
creat();
return 0;
}
貌似你吧new当malloc用了啊。