无法删除链表中的节点 [英] Unable to delete nodes in linked list
本文介绍了无法删除链表中的节点的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
请帮助我告知编写删除链接列表节点的函数是否存在错误.
Hi ,
Please help me to make me know if there is any bug in writing a function in deleting the nodes of a linked list.
#include <iostream>
#include <cstdlib>
///////////////////////////////////////////////////////////////////////////////////////////
typedef struct Clist // Double linked list //
{
struct Clist *Prior;
char cByte;
struct Clist *Next;
} Clist;
///////////////////////////////////////////////////////////////////////////////////////////
void DeleteFrom( Clist** clpInput )
{
///////////////////////////////////////////////////////////////////////////////////
Clist* clpTmpA, * clpTmpB, * clpTmpC;
///////////////////////////////////////////////////////////////////////////////////
clpTmpA = *clpInput;
while( clpTmpA != NULL )
{
clpTmpB = clpTmpA->Next;
free(clpTmpA->Next);
clpTmpA = clpTmpB;
}
}
推荐答案
这对我来说很像家庭作业,我们试图不给这些简单答案.
但我会尝试暗示一下.
如果您具有列表节点A-B-C,并且要删除B,则需要一个指向A和C的指针,删除B,然后将A-C和C-连接起来.答:
除此之外,我建议在代码的开头放置一个断点,并逐行跟随它.
祝您学习顺利,
伊恩.
This smells a lot like homework to me, which we try not to give easy answers to.
But I''ll try and hint a bit.
If you have list nodes A<->B<->C, and you''re removing B, then you need a pointer to A and C, delete B, then connect A->C and C->A.
Other than that, I''d recommend putting a break point at the beginning of your code, and following it line by line.
Good luck with your studies,
Iain.
您需要删除所有节点吗?然后更改
You need to delete all nodes? Then change
clpTmpB = clpTmpA->Next;
free(clpTmpA->Next);
clpTmpA = clpTmpB;
到
to
clpTmpB = clpTmpA->Next;
free(clpTmpA);
clpTmpA = clpTmpB;
这篇关于无法删除链表中的节点的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文