Node* LinkedList::insert(Node* pos, const std::string& value) {
// pos point to node where to insert before
// this line failes in run
std::unique_ptr<Node> pNode = std::make_unique<Node>(value, std::move(pos->getPrev()->next), pos->getPrev());
pos->prev = pNode.get();
pos->getPrev()->next = std::move(pNode);
return pNode.get();
class Node {
public:
const std::string value; // The data held by the LinkedList
std::unique_ptr<Node> next; // unique_ptr to the next node
Node* prev; // raw (non-owning) ptr to the previous node
public:
Node() : value(), next(nullptr), prev(nullptr) {}
// construct a node with string value, a unique_ptr to the next node, and a pointer to the previous node
Node(const std::string& value, std::unique_ptr<Node> next, Node* prev)
: value(value)
, next(std::move(next))
, prev(prev)
{}
// We can use the default constructor, since unique_ptr takes care of deleting memory
~Node() = default;
// return the value of the node 222
std::string getValue() const { return value; }
// return a raw (non-owning) pointer to the next node
Node* getNext() const { return next.get(); }
// return a raw (non-owning) pointer to the previous node
Node* getPrev() const { return prev; }
// write the value of the node to the ostream
friend std::ostream & operator<<(std::ostream & os, const Node & node);
friendclass LinkedList;
};