#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 *great()//动态链表函数建立
{
book *p1,*p2;
p1=new book;
head=p1;
p2=p1;
cout<<"请输入图书编号以0结束/n";
string str;
cin>>str;
while(!check(str))
{
cout<<"输入的不是数字,请重新输入:"<<endl;
cin>>str;
}
p1->num=atoi(str.c_str());
if(p1->num!=0)
{
cout<<"请输入图书价格:/n";
cin>>str;
while(!check(str))
{
cout<<"输入的不是数字,请重新输入:"<<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结束/n";
cin>>str;
while(!check(str))
{
cout<<"输入的不是数字,请重新输入:"<<endl;
cin>>str;
}
p1->num=atoi(str.c_str());
if(p1->num!=0)
{
cout<<"请输入图书价格:/n";
cin>>str;
while(!check(str))
{
cout<<"输入的不是数字,请重新输入:"<<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;
}
int main()
{
book*head;
head=great();
showbook(head);
cout<<"请输入你要删除的图书编号:";
int booknum;
cin>>booknum;
Delete(head,booknum);
showbook(head);
return 0;
}