Edit the question to include desired behavior, a specific problem or error, and the shortest code necessary to reproduce the problem. This will help others answer the question.
Closed 2 years ago.
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
接受
或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
发布评论
评论(2)
您应该从
delete
运算符中排除当前节点,因为代码已经是对delete
的响应。再次这样做就像盘旋。因此,将您的代码修改为:
无需将
上一个
设置为null
,因为它将用尽范围。但是,需要从其后继者中脱离上一个
,因此为避免,灾难将再次遵循该链条。该代码假设
此
是列表的第一个节点,这是执行delete
on的自然而然的事情。如果由于某种原因您将在列表中间的某个地方的节点上执行delete
,目的是丢弃整个列表,那么您还需要重复上述循环的节点。当前节点。You should exclude the current node from the
delete
operator, as the code is already a response to such adelete
. Doing it again is like running in circles.So modify your code to this:
There is no need to set
previous
toNULL
, since it will run out of scope. But it is needed to detachprevious
from its successors, so to avoid the the destructor will follow that chain again.This code assumes that
this
is the first node of the list, which would be the natural thing to executedelete
on. If for some reason you would executedelete
on a node somewhere in the middle of a list, with the aim to discard the whole list, then you would need to repeat the above loop also for the nodes that precede the current node.您不得删除当前节点本身,因为否则代码会调用未定义的行为。
您可以编写
列表定义为指向头节点的指针
,如果
相反,
You may not delete the current node itself because otherwise the code invokes undefined behavior.
Instead you could write
And if the list is defined as a pointer to the head node then you need to write
Here is a demonstration program
The program output is