哪位高手帮小弟我看看这个C++程序 急

谁帮我看看这个C++程序 急急急!!当我删除头结点时就崩溃了,怎么回事啊???# includeiostreamusing namesp

谁帮我看看这个C++程序 急急急!!

当我删除头结点时就崩溃了,怎么回事啊???

# include<iostream>
using namespace std;

struct book
{
public:
int num;
float price;
book *next;
};
book *head = NULL;
book *create()
{
book *p1,*p2;
p1 = new book;
head = p1;
p2 = p1;
cout<<"请输入图书编号,以0结束\n";
cin>>p1->num;
if(p1->num != 0)
{
cout<<"请输入图书的名字及价格\n";
cin>>p1->price;
}
else
{
delete p1;
p2 = NULL;
p2->next = NULL;
head = NULL;
head->next = NULL;
return head;
}
while(p1->num != 0)
{
p2 = p1;
p1 = new book;
cout<<"请输入图书编号,以0结束\n";
cin>>p1->num;
if(p1->num != 0)
{
cout<<"请输入图书的名字及价格\n";
cin>>p1->price;
}
p2->next = p1;
}
delete p1;
p2->next = NULL;
return head;
}
void showbook(book *head)
{
  cout<<"编号"<<" "<<"价格"<<endl;
  while(head)
  {
  cout<<head->num<<" "<<head->price<<endl;
  head = head->next;
  }
}

void Delete(book *head,int num)
{
book *l;
if(head->num == num)
{
l = head;
head = head->next;
delete l;
cout<<"删除操作成功!\n";
return;
}
while(head)
{
if(head->next == NULL)
{
cout<<"未找到要删除的编号\n";
return;
}
if(head->next->num == num)
{
l = head->next;
head->next = l->next;
delete l;
cout<<"删除操作成功\n";
return;
}
head = head->next;
}
cout<<"未找到要删除的编号\n";
}
int main()
{
book *head = NULL;
int denum;
head = create();
showbook(head);
cout<<"请输入要删除的编号\n";
cin>>denum;
Delete(head,denum);
showbook(head);
return 0;
}


[解决办法]
void Delete(book *head,int num)
改为void Delete(book*& head,int num);
仔细看看两个函数的区别哦,我使用的是指针的引用,你使用的是简单的指针!
[解决办法]

C/C++ code
# include<iostream>using namespace std;struct book{public:    int num;    float price;    book *next;};book *head = NULL;book *create(){    book *p1,*p2;    p1 = new book;    head = p1;    p2 = p1;    cout<<"请输入图书编号,以0结束\n";    cin>>p1->num;    if(p1->num != 0)    {        cout<<"请输入图书的名字及价格\n";        cin>>p1->price;    }    else    {        delete p1;        p2 = NULL;        p2->next = NULL;        head = NULL;        head->next = NULL;        return head;    }    while(p1->num != 0)    {        p2 = p1;        p1 = new book;        cout<<"请输入图书编号,以0结束\n";        cin>>p1->num;        if(p1->num != 0)        {            cout<<"请输入图书的名字及价格\n";            cin>>p1->price;        }        p2->next = p1;    }    delete p1;    p2->next = NULL;    return head;}void showbook(book *head){    cout<<"编号"<<" "<<"价格"<<endl;    while(head)    {        cout<<head->num<<" "<<head->price<<endl;        head = head->next;    }}void Delete(book **head,int num){    book *l;    if((*head)->num == num)    {        l = *head;        *head = (*head)->next;        delete l;        cout<<"删除操作成功!\n";        return;    }    while(*head)    {        if((*head)->next == NULL)        {            cout<<"未找到要删除的编号\n";            return;        }        if((*head)->next->num == num)        {            l = (*head)->next;            (*head)->next = l->next;            delete l;            cout<<"删除操作成功\n";            return;        }        *head = (*head)->next;    }    cout<<"未找到要删除的编号\n";}int main(){    book *head = NULL;    int denum;    head = create();    showbook(head);    cout<<"请输入要删除的编号\n";    cin>>denum;    Delete(&head,denum);    showbook(head);    return 0;}