我想知道如何在删除链接列表中的节点后将我的尾部原始指针更新为新尾部。 (是家庭作业)C++ - 指向链接列表中的unique_ptr节点的原始指针
我定义的头部和尾部为
std::unique_ptr<Node> head ;
Node* tail ;
,在我从后面我有以下的执行删除节点功能。
int Deque::remove_back(){
if (empty()) {throw std::runtime_error(std::string("Empty"));};
std::unique_ptr<Node> old;
Node* p = head.get();
int return_value = tail->val;
while (p->next != tail)
{p = p->next)}
old = move(tail);
tail = p;
return return_value;
}
所以尾巴是类型节点的原始指针。 P是Node类型的原始指针。
Head是Node类型的唯一指针。
我设置P = head.get()
所以现在p指向头
P = P->接下来应该被遍历了我的节点。
的问题是,p->next != tail
对 - >下是一个指向下列任何p是下一个节点。
我试图设置一个节点的指针,以等于类型节点(尾)的原始指针。
它告诉我我不能这样做。
我相信它是由于p-> next没有改变成一个拥有指针而不是我声明的观察指针。
错误:
Deque.cpp|68|error: no match for 'operator!=' (operand types are 'std::unique_ptr<Node>' and 'Node*')|
Deque.cpp|69|error: cannot convert 'std::unique_ptr<Node>' to 'Node*' in assignment|
Deque.cpp|71|error: no match for 'operator=' (operand types are 'std::unique_ptr<Node>' and 'std::remove_reference<Node*&>::type {aka Node*}')|
你是否需要使用'tail'?它根本没有帮助。 – Slava
不幸的是。 :( – TigerCode
单链表中的“tail”仅用于列表末尾的快速插入,但在其他情况下无用,因为您无法将其用于列表末尾的快速移除。 –