当链表里有三本书的数据时,删除第二个会显示找不到该图书,但是实际上第一第二个都被删除了,并且直接结束程序(原本完成删除操作后返回主菜单)。
函数如下:
void Delete(struct book* link,int ISBN)
{
struct book *p1,*p2;
int h=0,n;
p2=link;
p1=link->next;
while(p1)
{
if(p1->ISBN==ISBN)
{
h=1;
printf("下列为该图书信息:\n");
printf("ISBN号:%d\n",ISBN);
printf("书名:%s\n",p1->name);
printf("作者:%s\n",p1->writer);
printf("出版社:%s\n",p1->press);
printf("出版日期:%d年%d月%d日\n",p1->date[0],p1->date[1],p1->date[2]);
printf("价格:%.2lf\n\n",p1->price);
printf("您确定要删除该图书吗?\n1:yes!\n2:no!(返回主菜单)\n");
while(1)
{
scanf("%d",&n);
if(n==1)
{
if(p1->next!=NULL)
p2->next=p1->next;
else
p2->next=NULL;
free(p1);
printf("图书删除成功!");
break;
}
else if(n==2)
break;
else
printf("指令错误,请重新输入。");
}
return;
}
else if(p1->next!=NULL)
{
p2=p1;
p1=p1->next;
}
else
break;
}
if(h==0)
printf("未找到该图书!");
}
修改如下,供参考:
void Delete(book* link, int ISBN)
{
book* p1, * p2;
int h = 0, n;
p2 = link;
p1 = link->next;
while (p1)
{
if (p1->ISBN == ISBN)
{
h = 1;
printf("下列为该图书信息:\n");
printf("ISBN号:%d\n", ISBN);
printf("书名:%s\n", p1->name);
printf("作者:%s\n", p1->writer);
printf("出版社:%s\n", p1->press);
printf("出版日期:%d年%d月%d日\n", p1->date[0], p1->date[1], p1->date[2]);
printf("价格:%.2f\n\n", p1->price); // printf("价格:%.2lf\n\n", p1->price); 修改
printf("您确定要删除该图书吗?\n1:yes!\n2:no!(返回主菜单)\n");
while (1)
{
scanf("%d", &n);
if (n == 1)
{
//if (p1->next != NULL) 修改
p2->next = p1->next;
//else 修改
// p2->next = NULL; 修改
free(p1);
printf("图书删除成功!\n");
break;
}
else if (n == 2)
break;
else
printf("指令错误,请重新输入。\n1:yes!\n2:no!(返回主菜单)\n");
}
break; //return; 修改
}
//else if (p1->next != NULL)修改
//{ 修改
p2 = p1;
p1 = p1->next;
//} 修改
//else 修改
//break; 修改
}
if (h == 0)
printf("未找到该图书!\n");
}
该回答引用NewBing
你好,根据你提供的代码,我看到你的删除函数有一些问题。在删除节点时,你需要将被删除节点的前一个节点指向被删除节点的下一个节点,而你的代码中没有考虑到这一点。我建议你修改代码如下:
void Delete(struct book* link,int ISBN)
{
struct book *p1,*p2;
int h=0,n;
p2=link;
p1=link->next;
while(p1)
{
if(p1->ISBN==ISBN)
{
h=1;
printf("下列为该图书信息:\n");
printf("ISBN号:%d\n",ISBN);
printf("书名:%s\n",p1->name);
printf("作者:%s\n",p1->writer);
printf("出版社:%s\n",p1->press);
printf("出版日期:%d年%d月%d日\n",p1->date[0],p1->date[1],p1->date[2]);
printf("价格:%.2lf\n\n",p1->price);
printf("您确定要删除该图书吗?\n1:yes!\n2:no!(返回主菜单)\n");
while(1)
{
scanf("%d",&n);
if(n==1)
{
if(p1->next!=NULL)
p2->next=p1->next;
else
p2->next=NULL;
free(p1);
printf("图书删除成功!");
break;
}
else if(n==2)
break;
else
printf("指令错误,请重新输入。");
}
return;
}
else if(p1->next!=NULL)
{
p2=p1;
p1=p1->next;
}
else
break;
}
if(h==0)
printf("未找到该图书!");
}
源: 与必应的对话, 2023/4/13
(1) C语言中关于如何删除链表的某个节点问题 - CSDN博客. https://blog.csdn.net/qq_45836922/article/details/103621007 访问时间 2023/4/13.
(2) C语言使用free()函数释放链表节点时程序崩溃_free函数释放链表_qq_44948414的博客-CSDN博客. https://blog.csdn.net/qq_44948414/article/details/118001020 访问时间 2023/4/13.
(3) 删除链表中的节点(C语言) - CSDN博客. https://blog.csdn.net/L6666688888/article/details/123096714 访问时间 2023/4/13.
(4) 删除链表的节点(C++简单区)_c语言链表怎么删除一个节点_M-DoubleC的博客-CSDN博客. https://bing.com/search?q=c%e8%af%ad%e8%a8%80+%e9%93%be%e8%a1%a8%e5%88%a0%e9%99%a4%e8%8a%82%e7%82%b9 访问时间 2023/4/13.
(5) c语言链表怎么删除多个节点啊 - 百度知道. https://zhidao.baidu.com/question/272120182.html 访问时间 2023/4/13.
(6) 删除链表的节点(C++简单区)_c语言链表怎么删除一个节点_M-DoubleC的博客-CSDN博客. https://blog.csdn.net/mcc6688/article/details/108724745 访问时间 2023/4/13.
(7) 单向链表之删除节点(C语言实现) - CSDN博客. https://blog.csdn.net/peng_apple/article/details/79614518 访问时间 2023/4/13.