C++ 删除链表中的所有节点
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/9662738/
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
Remove all nodes in linked list
提问by Hold_My_Anger
I have a linked list contains 3 nodes like the image shown:
我有一个链表包含 3 个节点,如图所示:
There is a head pointer and temp1 pointer point to the front of the list, and tail point points at the end of the list.
有一个head指针和temp1指针指向链表的前面,尾点指向链表的尾端。
I want to remove all the nodes, and change it back to its original initial form ( tail = NULL, head = first_node , but the first node doesn't have any value in the data and next field).
我想删除所有节点,并将其改回其原始初始形式( tail = NULL, head = first_node ,但第一个节点在数据和下一个字段中没有任何值)。
Because I want to start putting up some new values in it. To remove all those data, is this code going to remove nodes inside this linked list and left with the first node with no values in data and next field?
因为我想开始在其中设置一些新值。要删除所有这些数据,此代码是否将删除此链表中的节点并留下第一个节点,数据和下一个字段中没有值?
This code is in C++:
此代码在 C++ 中:
while(temp1!=tail)
{
temp1 = temp1->next;
if(temp1->next == tail)
{
tail=temp1;
temp1 = temp1->next;
free(temp1);
}
}
But then, does this mean only the last node will be deleted? are there any way to delete all the nodes except the first one?
但是,这是否意味着只有最后一个节点会被删除?有没有办法删除除第一个节点之外的所有节点?
回答by Sachin Mhetre
To delete all nodes except the first node, you can try below code.
要删除除第一个节点之外的所有节点,您可以尝试以下代码。
temp1 = head->next;
while(temp1!=NULL) // as I am considering tail->next = NULL
{
head->next = temp1->next;
temp1->next = NULL;
free(temp1);
temp1 = head->next;
}
This will delete all nodes except first one. But the data with the first node will remain as it is.
这将删除除第一个节点之外的所有节点。但是第一个节点的数据将保持原样。
回答by Bartek Banachewicz
Disclaimer: I assume it's only for learning purposes and in real-world scenario you would use std::list<>
or similar container.
免责声明:我认为它仅用于学习目的,并且在您将使用std::list<>
或类似容器的实际场景中。
For single-linked list, you can just drop all this burden an let the stdlib manage the pointers:
对于单链表,您可以放弃所有这些负担,让 stdlib 管理指针:
class Node {
std::unique_ptr<Node> next;
};
You can safely use .reset()
method to make operations on the list:
您可以安全地使用.reset()
方法对列表进行操作:
Given current_ptr, the pointer that was managed by *this, performs the following actions, in this order:
- Saves a copy of the current pointer old_ptr = current_ptr
- Overwrites the current pointer with the argument current_ptr = ptr
- If the old pointer was non-empty, deletes the previously managed object if(old_ptr != nullptr) get_deleter()(old_ptr).
给定 current_ptr,由 *this 管理的指针,按以下顺序执行以下操作:
- 保存当前指针的副本 old_ptr = current_ptr
- 用参数 current_ptr = ptr 覆盖当前指针
- 如果旧指针不为空,则删除先前管理的对象 if(old_ptr != nullptr) get_deleter()(old_ptr)。
From http://en.cppreference.com/w/cpp/memory/unique_ptr/reset.
来自http://en.cppreference.com/w/cpp/memory/unique_ptr/reset。
And that's pretty much what you would do when deleting. I believe you can also use unique_ptr::swap()
, to easily manipulate your nodes.
这几乎就是您在删除时会做的事情。我相信您也可以使用unique_ptr::swap()
, 轻松操作您的节点。
回答by Kozlu B.
回答by user2069996
temp1 = head->next;
while(temp1!=NULL) // as I am considering tail->next = NULL
{
head->next = temp1->next;
temp1->next = NULL;
free(temp1);
temp1 = head->next;
}
The logic for this would be more correct if it is this way.
如果是这样的话,这个逻辑会更正确。
After the statement
声明后
free(temp1);
Add the condition
添加条件
if (head -> next != NULL)
temp1 = head->next;
Since after deleting the last node there is no point in reassigning the address of head pointer to temp1.
因为在删除最后一个节点后,将头指针的地址重新分配给 temp1 就没有意义了。