双向链表 std::unique_ptr class 在删除节点时没有按预期工作
Doubly linked list std::unique_ptr class doesn't work as expected on node removal
受 CppCon2016 中 Herb Sutter 的演讲启发,which can be found in this link.
我决定实现一个双向链表,如视频中所示,带有智能指针。
以下实现几乎与 remove() 方法中的 one-line 代码不同。
我调试了这段代码,之前的节点在删除后没有更新为 null(作为头节点应该是)。
就好像智能指针之间的所有权转移是错误的。
下面是 header 文件和测试 main() 的代码:
LinkedList.h
#ifndef LINKEDLIST_H
#define LINKEDLIST_H
#include <iostream>
#include <memory>
#include <initializer_list>
namespace DLL {
template <typename T> class LinkedList{
private:
struct ListNode{
std::unique_ptr<ListNode> next; //2 uniq_ptr can't point to one another.
ListNode* prev = nullptr; //weakptr needs to be cast back to a shared_ptr to check its state.
T data{}; //Initialize empty;
ListNode(const T& element){
this->data = element;
}
};
public:
std::unique_ptr<ListNode> head;
ListNode* tail = nullptr;
LinkedList(){}
~LinkedList(){}
void append(const T& element){
ListNode* curr = nullptr;
if (head.get() == nullptr){ //If list is empty.
head = std::make_unique<ListNode>(element);
}
else if(head.get() -> next.get() == nullptr){ //If list has one element
head.get() -> next = std::make_unique<ListNode>(element);
curr = head.get() -> next.get(); //Sets raw pointer to the first element.
curr -> prev = head.get();
tail = curr;
}
else{
tail -> next = std::make_unique<ListNode>(element);
curr = tail -> next.get(); //Sets raw pointer to the last element.
curr -> prev = tail;
tail = curr;// The new last element is the tail.
}
}
int remove(const T& element){
ListNode* curr = nullptr;
if (head.get() == nullptr){ //If list is empty.
return -1; //Error: Can't remove from empty list.
}
//List has one or more elements.
curr = head.get();
while(curr != nullptr){
if(curr -> data == element){ //Found element
if(curr -> prev == nullptr){ //is head
//head.reset(head.get()->next.get()); Doesn't work
//Line below doesn't work too
head = std::move(curr->next); //Head now points to the next element
//New head's previous element doesn't point to nothing, as it should.
}
else if(curr -> next.get() == nullptr){ //is tail
tail = curr -> prev; //Reference the previous element
tail -> next.release(); //Release the old tail element
if(head.get() == tail){
tail = nullptr; //tail and head should not be the same.
} //List contains one element
}
else{//is intermediate
//The next node should point to the previous one
curr -> next -> prev = curr -> prev;
curr -> prev -> next = std::move(curr -> next);
//The prev node now points to the next one of current.
}
return 1; //Element found in list
}
curr = curr -> next.get(); //Traverse the next element
}
return 0; //Element not found in list
}
void print() {
ListNode* curr = head.get(); //Start from the start of the list.
std::cout << "[ ";
while (curr != nullptr) {
std::cout << curr -> data << " ";
curr = curr -> next.get();
}
std::cout << "]" << std::endl;
}
};
}
#endif
main.cpp
int main() { //Temporary Test Main will be split from the implementation file in the future
DLL::LinkedList <int> list; //Empty list
list.append(1);
list.append(4);
list.append(5);
list.append(6);
list.print();
list.remove(5);
list.remove(1); //When 1 is removed the 4 doesn't properly update as head, meaning the previous pointer of 4 is not null
list.remove(4);
list.remove(6);
list.print();
retunn 0;
}
对于这种问题,我很抱歉,我搜索了很多,但没有找到类似的问题。我调试了好几天,但无法修复所有权行。
我尝试包含最少量的代码,以重现错误如果 header 是一个长代码段,我很抱歉。
我用 g++: g++ -std=c++14 main.cpp -o out
和 VS2015 编译器编译。
make_unique
调用
需要 C++14 标志
在检查迭代器的前一个指针是否为空(基本上检查头部)的部分中,您有以下行:
head = std::move(curr->next);
将 header 指针移动到元素的下一个指针。但是,您无法将新头指针的先前指针更新为空。所以代码应该是这样的:
if(curr -> data == element){ //Found element
if(curr -> prev == nullptr){ //is head
head = std::move(curr->next); //Head now points to the next element
if (head)
head->prev = nullptr;
else
tail = nullptr;
}
}
由于您在成为新头指针的项目上使用 std::move
(这是正确的),因此您基本上保持该节点中包含的数据相同。在这种情况下你需要明确 - std::unique_ptr
包装器对它拥有的 object 的底层实现一无所知,所以它无法知道更新 prev
指针在这种情况下。
受 CppCon2016 中 Herb Sutter 的演讲启发,which can be found in this link.
我决定实现一个双向链表,如视频中所示,带有智能指针。
以下实现几乎与 remove() 方法中的 one-line 代码不同。
我调试了这段代码,之前的节点在删除后没有更新为 null(作为头节点应该是)。
就好像智能指针之间的所有权转移是错误的。
下面是 header 文件和测试 main() 的代码:
LinkedList.h
#ifndef LINKEDLIST_H
#define LINKEDLIST_H
#include <iostream>
#include <memory>
#include <initializer_list>
namespace DLL {
template <typename T> class LinkedList{
private:
struct ListNode{
std::unique_ptr<ListNode> next; //2 uniq_ptr can't point to one another.
ListNode* prev = nullptr; //weakptr needs to be cast back to a shared_ptr to check its state.
T data{}; //Initialize empty;
ListNode(const T& element){
this->data = element;
}
};
public:
std::unique_ptr<ListNode> head;
ListNode* tail = nullptr;
LinkedList(){}
~LinkedList(){}
void append(const T& element){
ListNode* curr = nullptr;
if (head.get() == nullptr){ //If list is empty.
head = std::make_unique<ListNode>(element);
}
else if(head.get() -> next.get() == nullptr){ //If list has one element
head.get() -> next = std::make_unique<ListNode>(element);
curr = head.get() -> next.get(); //Sets raw pointer to the first element.
curr -> prev = head.get();
tail = curr;
}
else{
tail -> next = std::make_unique<ListNode>(element);
curr = tail -> next.get(); //Sets raw pointer to the last element.
curr -> prev = tail;
tail = curr;// The new last element is the tail.
}
}
int remove(const T& element){
ListNode* curr = nullptr;
if (head.get() == nullptr){ //If list is empty.
return -1; //Error: Can't remove from empty list.
}
//List has one or more elements.
curr = head.get();
while(curr != nullptr){
if(curr -> data == element){ //Found element
if(curr -> prev == nullptr){ //is head
//head.reset(head.get()->next.get()); Doesn't work
//Line below doesn't work too
head = std::move(curr->next); //Head now points to the next element
//New head's previous element doesn't point to nothing, as it should.
}
else if(curr -> next.get() == nullptr){ //is tail
tail = curr -> prev; //Reference the previous element
tail -> next.release(); //Release the old tail element
if(head.get() == tail){
tail = nullptr; //tail and head should not be the same.
} //List contains one element
}
else{//is intermediate
//The next node should point to the previous one
curr -> next -> prev = curr -> prev;
curr -> prev -> next = std::move(curr -> next);
//The prev node now points to the next one of current.
}
return 1; //Element found in list
}
curr = curr -> next.get(); //Traverse the next element
}
return 0; //Element not found in list
}
void print() {
ListNode* curr = head.get(); //Start from the start of the list.
std::cout << "[ ";
while (curr != nullptr) {
std::cout << curr -> data << " ";
curr = curr -> next.get();
}
std::cout << "]" << std::endl;
}
};
}
#endif
main.cpp
int main() { //Temporary Test Main will be split from the implementation file in the future
DLL::LinkedList <int> list; //Empty list
list.append(1);
list.append(4);
list.append(5);
list.append(6);
list.print();
list.remove(5);
list.remove(1); //When 1 is removed the 4 doesn't properly update as head, meaning the previous pointer of 4 is not null
list.remove(4);
list.remove(6);
list.print();
retunn 0;
}
对于这种问题,我很抱歉,我搜索了很多,但没有找到类似的问题。我调试了好几天,但无法修复所有权行。 我尝试包含最少量的代码,以重现错误如果 header 是一个长代码段,我很抱歉。
我用 g++: g++ -std=c++14 main.cpp -o out
和 VS2015 编译器编译。
make_unique
调用
在检查迭代器的前一个指针是否为空(基本上检查头部)的部分中,您有以下行:
head = std::move(curr->next);
将 header 指针移动到元素的下一个指针。但是,您无法将新头指针的先前指针更新为空。所以代码应该是这样的:
if(curr -> data == element){ //Found element
if(curr -> prev == nullptr){ //is head
head = std::move(curr->next); //Head now points to the next element
if (head)
head->prev = nullptr;
else
tail = nullptr;
}
}
由于您在成为新头指针的项目上使用 std::move
(这是正确的),因此您基本上保持该节点中包含的数据相同。在这种情况下你需要明确 - std::unique_ptr
包装器对它拥有的 object 的底层实现一无所知,所以它无法知道更新 prev
指针在这种情况下。