[c++]代码库
Book* CreateHead()
{
Book *head,*p1,*p2;//定义三个指向Book的类,返回值给head
p1=new Book;
head=p1;
p2=p1;//将三个结点一起在椎中创建
cout<<"请输入图书编号,以0结束"<<endl;
cin>>p1->num;
if ( p1->num!=0 )
{
cout<<"请输入图书价格"<<endl;
cin>>p1->price;
}
else
{
delete p1;
p2=NULL;
return head;
}
while ( p1->num!=0 )
{
p2=p1;//这步很重要,意思为将P2设为当前结点,然后用p1继续创建,用培p2->next指向p1
p1=new Book;
cout<<"请输入图书编号,以0结束"<<endl;
cin>>p1->num;
if ( p1->num!=0 )
{
cout<<"请输入图书价格"<<endl;
cin>>p1->price;
}
p2->next=p1;
}
delete p1;
p2->next=NULL;
return head;
}