我用c++链表写了个图书管理系统,删除函数有些问题,求大神指教。总是说有一处是nullptr
#include<iostream>#include<string>usingnamespacestd;classbook{public:intnum;floatpric...
#include<iostream>
#include<string>
using namespace std;
class book {
public:
int num;
float price;
book *next;
};
book *head = NULL;
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)//用VS2017编译总是说head->next是nullptr
{
l = head->next;
head->next = l->next;
delete l;
cout << "操作成功!" << endl;
return;
}
head = head->next;
}
cout << "找不到编号" << endl;
}int main()
{
book *head = NULL;
head = creat();
showbook(head);
cout << "请输入要删除的编号:" << endl;
int booknum;
cin >> booknum;
Delete(head, booknum);
showbook(head);
system("pause");
return 0;
} 展开
#include<string>
using namespace std;
class book {
public:
int num;
float price;
book *next;
};
book *head = NULL;
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)//用VS2017编译总是说head->next是nullptr
{
l = head->next;
head->next = l->next;
delete l;
cout << "操作成功!" << endl;
return;
}
head = head->next;
}
cout << "找不到编号" << endl;
}int main()
{
book *head = NULL;
head = creat();
showbook(head);
cout << "请输入要删除的编号:" << endl;
int booknum;
cin >> booknum;
Delete(head, booknum);
showbook(head);
system("pause");
return 0;
} 展开
1个回答
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询