C++ 删除链表
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/4112905/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Delete linked list
提问by starcorn
Hey I wonder about I have written a C++ linked list, where I call a destructor to go through an allocated linked list and delete every node found. However what I found is that although it goes through the linked list and delete every occurance it will still print out values. Although just some scrap values.
嘿,我想知道我写了一个 C++ 链表,在那里我调用析构函数来遍历分配的链表并删除找到的每个节点。然而我发现,虽然它通过链表并删除每一次出现,但它仍然会打印出值。虽然只是一些废品价值。
But shouldn't it be when I delete the linked_list it shouldn't be printable next time?
I'm create a linked list by using the new
, and delete
when I remove the list
但是,当我删除linked_list 时,它不应该是下次不可打印的吗?我正在使用new
,创建一个链表,delete
当我删除列表时
sorted_list::~sorted_list()
{
// Destructor implementation
destroy(this->first);
cout << "Destructor called sorted_list" << endl;
}
void sorted_list::destroy(list_link* item)
{
if (item)
{
destroy(item->next);
delete item;
}
}
print function
打印功能
void sorted_list::print() {
if(this->first)
{
iteratorn *traverse = new iteratorn(this->first);
while( !traverse->iterator_end() )
{
cout << traverse->iterator_get_key() << " ";
traverse->iterator_next();
}
delete traverse;
}
else
cout << "list empty" << endl;
}
回答by Vlad
When you access a destructed object, the behaviour is undefined. In fact, deleting an object doesn't clear the memory, just marks it available, so if you execute some operations on already deleted object, they may do something reasonable. But again, the object is destructed, so you must not access it.
当你访问一个被破坏的对象时,行为是未定义的。实际上,删除一个对象并不会清除内存,只是将其标记为可用,因此如果您对已删除的对象执行某些操作,它们可能会做一些合理的事情。但同样,该对象已被破坏,因此您不得访问它。
Of course, you should not retain any pointers to the object belonging to the linked list after it's destructed, because those objects will be destructed as well.
当然,在链表被销毁后,您不应该保留指向属于链表的对象的任何指针,因为这些对象也会被销毁。
By the way, your sorted_list::destroy
is recursive, which is quite inefficient. You would need perhaps to replace it with iterative approach:
顺便说一句,你sorted_list::destroy
是递归的,这是非常低效的。您可能需要用迭代方法替换它:
void sorted_list::destroy(list_link* item)
{
while (item)
{
list_link* old = item;
item = item->next;
delete old;
}
}
(And you should take into account @Roger Pate's comment and not delete this->first
the second time after calling destroy(this->first);
.)
(你应该考虑@Roger Pate 的评论,不要this->first
在调用后第二次删除destroy(this->first);
。)
回答by Kostas
When declaring the link, which probably looks more or less like this:
在声明链接时,它可能看起来或多或少是这样的:
struct list_link {
int data;
list_link *next;
};
You could slip a destructorin:
你可以插入一个析构函数:
struct list_link {
int data;
list_link *next;
~list_link() { delete next; } // MAKE SURE NULL-TERMINATED LIST
};
This way if you want to delete the list you can simply:
这样,如果您想删除列表,您可以简单地:
delete first;
MAGIC!!
魔法!!
回答by Red.Wave
Missing part is nullifying. After deleting, you must nullify the first node at least. I would nullify every node after delete.
缺失部分无效。删除后,您必须至少取消第一个节点。删除后我会取消每个节点。