琏表出错问题,高手进来帮忙下C++

#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;
}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值