请指点一下这个程序的问题
#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;
p1=new book;
head=p1;
p2=p1;
cout<<"请输入图书的编号,以0结束输入:";
string str;
cin>>str;
while (!check(str))
{
cout<<"编号输入错误,请重新输入,按0结束!"<<endl;
cin>>str;
}
p1->num=atoi(str.c_str());
if (p1->num!=0)
{
cout<<"请输入图书的价格:";
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结束输入:";
cin>>str;
while (!check(str))
{
cout<<"编号输入错误,请重新输入,按0结束!"<<endl;
cin>>str;
}
p1->num=atoi(str.c_str());
if (p1->num!=0)
{
cout<<"请输入图书的价格:";
cin>>str;
while (!check(str))
{
cout<<"价格输入错误,请重新输入,按0结束!"<<endl;
cin>>str;
}
p1->price=atof(str.c_str());
}
else cout<<"录入结束!"<<endl;
p2->next=p1;
}
delete p1;
p2->next=NULL;
return head;
}
void showbook(book *head)
{
if (!head)
{
cout<<"该链表为空链表!"<<endl;
return;
}
while (head)
{
cout<<"Num:"<<head->num<<"\t"<<"Price:"<<head->price<<endl;
head=head->next ;
}
}
void Delete(book *head,int num)
{
book *p1,*p2;
if (head==NULL)
{
cout<<"该链表为空链表!"<<endl;
return;
}
if (head->next==NULL)
{
p1=head;
head=NULL;
::head=head;
delete p1;
cout<<"删除头结点成功!"<<endl;
return;
}
p1=head;
p2=p1;
while (p1)
{
if (head->num==num)
{
p1=head;
head=head->next;
::head=head;
delete p1;
cout<<"头节点删除成功!"<<endl;
return;
}
if (p1->num==num)
{
p2->next=p1->next;
delete p1;
cout<<"节点删除成功!"<<endl;
return;
}
p1=p2->next;
}
}
int main()
{
book *head=NULL;
head=creat();
showbook(head);
int num;
cout<<"请输入删除书籍的编号:";
cin>>num;
Delete(head,num);
showbook(::head);//如果不加::,程序在删除头节点的时候就会崩溃,这是为什么?
return 0;
}