Question: Main.cpp #include #include List.h int main() { return 0; } List.h #ifndef HW10_25_LIST_H #define HW10_25_LIST_H #include #include using Item = std::string; class List { private:

Main.cpp
#include
int main() { return 0; }
List.h
#ifndef HW10_25_LIST_H #define HW10_25_LIST_H
#include
using Item = std::string;
class List { private:
class ListNode { public: Item item; ListNode * next; ListNode(Item i, ListNode *n=nullptr) { item = i; next = n; } };
ListNode * head = nullptr; ListNode * tail = nullptr;
public: class iterator { ListNode *node; iterator(ListNode *n) : node {n} {} friend class List; public: Item& operator*() { return node->item; } iterator& operator++() { node = node->next; return *this; } bool operator!=(const iterator& other) const { return node != other.node; } };
public: List() = default; List(const List& other); List& operator=(const List& rhs); List(List&& other); List& operator=(List&& rhs); ~List(); bool empty() const { return head==nullptr; } void push_back(const Item& a); void push_front(const Item& a);
iterator begin() const { return iterator(head); } iterator end() const { return iterator(nullptr); }
friend std::vector
#endif //HW10_25_LIST_H
List.cpp
#include "List.h"
// IMPLEMENT MOVE CONSTRUCTOR
// IMPLEMENT MOVE ASSIGNMENT OPERATOR
List::List(const List& other) { auto p = other.head; while (p) { push_back(p->item); p = p->next; } }
List& List::operator=(const List& rhs) { if (&rhs == this) return *this; // delete old list auto p = head; while (p) { auto tmp = p->next; delete p; p = tmp; } head = tail = nullptr; // copy from rhs p = rhs.head; while (p) { push_back(p->item); } // return reference to self return *this; }
List::~List() { auto p = head; while (p) { auto tmp = p->next; delete p; p = tmp; } }
void List::push_back(const Item& a) { auto node {new ListNode(a)}; if ( head == nullptr ) { // empty list head = node; tail = node; } else { tail->next = node; tail = node; } }
void List::push_front(const Item& a) { auto node {new ListNode(a)}; if (head == nullptr) { head = node; tail = node; } else { node->next = head; head = node; } }
10.25 Move constructor and assignment for linked list Write a move constructor and a move assignment operator for the List class
Step by Step Solution
There are 3 Steps involved in it
Get step-by-step solutions from verified subject matter experts
