#include #include #include #include class Jump_List { private: struct Node; public: Jump_List() = default; ~Jump_List() { while (size() > 0) { pop_front(); } } /* BEGIN SOLUTION */ Jump_List(Jump_List const& other) : first { other.first->clone() }, count { other.count } { } Jump_List(Jump_List&& other) : first { other.first }, count { other.count } { other.first = nullptr; other.count = 0; } Jump_List& operator=(Jump_List const& other) { Jump_List copy { other }; *this = std::move(copy); return *this; } Jump_List& operator=(Jump_List&& other) { std::swap(first, other.first); std::swap(count, other.count); return *this; } /* END SOLUTION */ int& at(int index) { Node* current { first }; if (index >= count) { throw std::out_of_range { "Jump_List out of range" }; } if (index % 2 == 1) { current = current->next; --index; } while (index > 0) { current = current->jump; index -= 2; } return current->value; } void push_front(int value) { Node* tmp { new Node { value } }; tmp->next = first; if (first != nullptr) { tmp->jump = first->next; } first = tmp; ++count; } void pop_front() { Node* tmp { first }; first = first->next; delete tmp; --count; } int size() const { return count; } void print(std::ostream& os) const { Node* current { first }; os << "size " << size() << ": "; while (current != nullptr) { os << current->value << " "; current = current->next; } os << std::endl; } private: struct Node { /* BEGIN SOLUTION */ Node* clone() { Node* copy { new Node { value } }; if (next != nullptr) { copy->next = next->clone(); copy->jump = copy->next->next; } return copy; } /* END SOLUTION */ int value { }; Node* jump { }; Node* next { }; }; Node* first { }; int count { }; }; int main() { Jump_List l1 { }; for (int i { 9 }; i >= 0; --i) { l1.push_front(i); } Jump_List l2 { l1 }; for (int i { 0 }; i < 4; ++i) { l1.pop_front(); } l1.print(std::cout); l2.print(std::cout); }