I am still trying to learn more about copy and move constructors. I have a linked list class that I want to deep copy using copy and move constructors but I'm having issues. First, to deep copy List
class, do I only copy head_ and tail_
in the constructors. I know the code is horrendous, maybe I shouldn't jump into high-level stuff right away.
Any help is appreciated!
template<typename T>
class List
{
public:
class Node {
public:
Node(T value) : value_(value) {}
T value_;
Node* next_;
Node* prev_;
};
Node* head_;
Node* tail_;
//! Default constructor
List() :tail_(nullptr) {}
//! Copy constructor
List(const List& lst) : head_(nullptr) {
//not sure what goes in here
}
}
//! Move constructor
List(List&& move) {
head_ = move.head_;
move.head_ = nullptr;
tail_ = move.tail_;
move.tail_ = nullptr;
}
//! Copy assignment operator
List& operator= (const List& list) {
tail_ = nullptr;
head_ = tail_;
Node* current = list.head_;
Node* next = list.head_->next_;
Node* replace = head_;
while (next != list.tail_) {
current = current->next_;
next = next->next_;
replace->next_ = tail_;
replace->next_->value_;
replace = replace->next_;
}
return *this;
}
//! Move assignment operator
List& operator= (List&& other) {
tail_ = nullptr;
head_ = tail_;
head_->next_ = other.head_->next_;
Node* current = other.head_;
Node* next = other.head_->next_;
while (next != other.tail_) {
current = current->next_;
next = next->next_;
}
current->next_ = tail_;
other.head_->next_ = other.tail_;
return *this;
}