链表报错missing ';' before 'while',求解
#include <iostream>#include <string>
using namespace std;
class book
{
public:
int num;
float price;
book *next; //指向下一个节点
};
book *head=NULL;
bool check(string str)
{
for(int i=0;i<str.length();i++)
{
if ((str[i]>'9'||str[i]<'0')&&(str[i]!='.'))
return false;
}
return true;
}
book *creat()//用来建立动态链表
{
book *p1,*p2;//p2作为下一个节点指针,p1作为本节点指针
p1=new book;
head=p1;
p2=p1;
cout<<"请输入图书的编号,以0结束"<<endl;
string str;
cin>>str;
while(!check(str))
{
cout<<"输入的不是数字,请重新输入,按0返回"<<endl;
cin>>str;
}
p1->num=atoi(str.c_str());
if (p1->num!=0)
{
cout<<"请输入图书的价格"<<endl;
cin>>str;
while(!check(str))
{
cout<<"输入的不是数字,请重新输入,按0返回"<<endl;
cin>>str;
}
p1->price=atof(str.c_str());
}
else
{
delete p1;
p2=NULL;p2->next=NULL;
head=NULL;
return head;
}
while (p1->num!=0)
{
p2=p1;
p1=new book;
cout<<"请输入图书的编号,以0结束"<<endl;
cin>>str;
while(!check(str))
{
cout<<"输入的不是数字,请重新输入,按0返回"<<endl;
cin>>str;
}
p1->num=atoi(str.c_str());
if (p1->num!=0)
{
cout<<"请输入图书的价格"<<endl;
cin>>str;
while(!check(str))
{
cout<<"输入的不是数字,请重新输入,按0返回"<<endl;
cin>>str;
}
p1->price=atof(str.c_str());
}
p2->next=p1;
}
delete p1;
p2->next=NULL;
return head;
}
void showbook(book* head)
{
cout<<endl;
cout<<"图书信息如下:"<<endl;
while (head)
{
cout<<"图书编号:"<<head->num<<"\t";
cout<<"价格:"<<head->price<<endl;
head=head->next;
}
}
void Delete(book*head,int num)
{
book*l;
if (head->num==num)
{
l=head;
head=head->next;
::head=head;
delete l;
cout<<"操作成功"<<endl;
return;
}
while(head)
{
if (head->next==NULL)
{
cout<<"找不到要删除的编号。"<<endl;
return;
}
if (head->next->num==num)
{
l=head->next;
head->next=l->next;
delete l;
cout<<"操作成功"<<endl;
return;
}
head=head->next;
}
cout<<"找不到要删除的编号"<<endl;
}
void insert(book*head,int num,float price)
{
book*list=new book;
list->num=num;
list->price=price;
list->next=head;
::head=list;
}
book*temp=NULL;
////////////////////////////////////////////////////////////////////////////////////////////////
while((num>head->num)&&(head->next!=NULL))//此处报错missing ';' before 'while',该怎么改呀/
temp=head; /
head=head->next;/
}
if (num>head->num)
{
head->next=list;
}
else
{
temp->next=list;
list->next=head;
}
///////////////////////////////////////////////////////////////////////////////////////////////////////
int getBookNum(book*head)
{
int num=0;
while(head)
{
num++;
head=head->next;
}
return num;
}
int main()
{
//book *head=NULL;
head=creat();
showbook(head);
cout<<"请输入您要删除的图书编号:";
int BookNum;
cin>>BookNum;
Delete(head,BookNum);
showbook(head);
cout<<"请输入您要插入图书的编号:";
cin>>BookNum;
cout<<"请输入您要插入图书的价格:";
float BookPrice;
cin>>BookPrice;
insert(head,BookNum,BookPrice);
showbook(head);
cout<<"图书数目是:"<<getBookNum(head)<<endl;
return 0;
}
去掉方块中的代码,其余正确运行,方框中的代码是插入节点