65,187
社区成员




#include <iostream>
using namespace std;
class book
{
public:
int num;
float price;
book *next;
};
book *head;
book *creat()
{
book *p1,*p2;
p1=new book;
head=p1;
p2=p1;
while (1)
{
cout<<"请输入编号:";
cin>>p1->num;
if (p1->num!=0)
{
p2=p1;
cout<<"请输入价格:";
cin>>p1->price;
p1=new book;
p2->next=p1;
}
else
{
delete p1;
p2->next=NULL;
break;
}
}
return head;
}
void showbook(book *head)
{
cout<<"图书信息如下:"<<endl;
while(head)
{
cout<<"编号:"<<head->num;
cout<<"价格:"<<head->price<<endl;
head=head->next;
}
}
void deletebook(int num)
{
book *p1;
if (head->num==num)
{
p1=head;
head=head->next; //head是一个全局指针
delete p1;
cout<<"操作成功"<<endl;
return;
} //删除头结点
while(head)
{
if(head->next==NULL)
{
cout<<"找不到要删除的节点"<<endl;
return;
}
if(head->next->num==num)
{
p1=head->next;
head->next=p1->next;
delete p1;
cout<<"操作成功"<<endl;
return;
}
head=head->next;
}
cout<<"找不到需要删除的节点"<<endl;
}
int main()
{
int num;
head=creat();
showbook(head);
cout<<"请输入要删除的节点:";
cin>>num;
deletebook(num);
showbook(head);
system("pause");
return 0;
}