我正在尝试实现两个成员函数,即 push_front
和 destructor
。我已经编写了 push_front
函数的代码。但我似乎,我在任何地方都做错了。我该如何解决这个问题?如何在 Linked-List
前正确插入节点?
template <typename T>
class Node {
public:
Node():next_p_{nullptr} {}
Node(T v,Node* n):data_{v},next_p_{n} {}
~Node() {};
void setData( T &v ) { data_ = v; }
const T &getData() const { return data_; }
void setNext( Node *n ) { next_p_ = n; }
Node *getNext() const { return next_p_; }
private:
T data_;
Node *next_p_;
};
template <typename T>
class LinkedList {
private:
Node<T> *head_ptr_;
public:
LinkedList():head_ptr_{nullptr} {}
~LinkedList() {
}
// Insert at the front of the linked list (UPDATED)
void push_front( T v ){
Node *new_node = new Node;
new_node->setNext(*head_ptr_) = v;
new_node->next_p_ = this->head_ptr_;
this->head_ptr_ = new_node;
}
};