#include #include "ListT.h" #include #include using namespace std; struct NodeT { ItemT data; NodeT * next{nullptr}; }; ListT::~ListT() { NodeT * tmp; tmp = head; while (tmp != nullptr) { head = tmp; tmp = tmp->next; delete head; } } size_t ListT::Size() { return size; } void ListT::PushFront(ItemT i){ NodeT * tmp {new NodeT{i, nullptr}}; tmp->next = head; head = tmp; if (tail == nullptr) { tail = tmp; } ++size; } void ListT::PopFront() { NodeT * tmp{head}; if (head != nullptr) { head = head->next; delete tmp; --size; } if (head == nullptr) { tail = nullptr; } } ListT::ListIteratorT::ListIteratorT(NodeT * newValue = nullptr): value{newValue}{}; ListT::ListIteratorT ListT::Begin() { return ListIteratorT{head}; } ListT::ListIteratorT ListT::End() { return nullptr; } ItemT & ListT::ListIteratorT::operator*() { if ( value != nullptr) { return value->data; } else { throw out_of_range("Invalid Iterator"); } } ListT::ListIteratorT & ListT::ListIteratorT::operator++() { if (value != nullptr) { value = value->next; } else { throw out_of_range("Invalid Iterator"); } return * this; } ListT::ListIteratorT ListT::ListIteratorT::operator +(size_t i) { while (value != nullptr and i > 0) { value = value ->next; --i; } return *this; } bool ListT::ListIteratorT::operator ==(const ListIteratorT & other) { return value == other.value; } bool ListT::ListIteratorT::operator !=(const ListIteratorT & other) { return value != other.value; } ListT::ListIteratorT ListT::DeleteNext(ListIteratorT i) { if (i.value == nullptr) { throw out_of_range("Invalid Iterator"); } NodeT * tmp{i.value->next}; if (tmp != nullptr) { i.value->next = tmp->next; --size; if (tail == tmp) { tail = i.value; } delete tmp; } // should we throw an exception if there is nothing to delete? return i; } ListT::ListIteratorT ListT::InsertNext(ListIteratorT i, ItemT data) { if (i.value == nullptr) { throw out_of_range("Invalid Iterator"); } NodeT * tmp{new NodeT{data, i.value->next}}; if (nullptr == i.value->next) { tail = tmp; } i.value->next = tmp; ++size; return ListIteratorT{tmp}; }