I'm trying to figure out how to update my tail raw pointer to a new tail after removing a node in my linked list. (is homework)
I've defined the head and tail as
std::unique_ptr<Node> head ;
Node* tail ;
and in my function for removing a node from the back I have the following implementation.
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;
}
So tail is a raw pointer of type Node. P is a raw pointer of type Node.
Head is a unique pointer of type Node.
I'm setting p = head.get()
so now p points to the head
p = p->next should be iterating down my nodes.
The problem is that p->next != tail
p->next is a pointer to the next node following whatever p is.
I'm trying to set a pointer to a node to be equal to a raw pointer of type node (tail).
Its telling me I can't do this.
I believe its due to p->next not changing into an owning pointer instead of the observing one I declared it to be.
Errors:
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? It does not help at all.tailin a single-linked list is only useful for fast inserts at the end of the list, but is otherwise useless since you can't use it for fast removals at the end of the list.