#include #include "ListT.h" using namespace std; struct NodeT { int data; NodeT * next; }; ListT::ListT(){ head = nullptr; size = 0; return; } // assume dest is not a valid pointer void CopyList(NodeT * & dest, NodeT * src) { NodeT * tmp = nullptr; NodeT * travler = nullptr; NodeT * follow = nullptr; dest = nullptr; for(travler = src; travler != nullptr; travler = travler->next) { tmp = new NodeT; tmp -> data = travler -> data; tmp -> next = nullptr; if (follow == nullptr) { follow = tmp; dest = tmp; } else { follow ->next = tmp; follow = tmp; } } return; } ListT::ListT(const ListT & rhs){ // copy the static data size = rhs.size; // copy the dynamic data CopyList(head, rhs.head); return; } void DeleteList(NodeT * head){ NodeT * tmp; while (head != nullptr) { tmp = head; head=head->next; delete tmp; } return; } ListT::~ListT(){ // delete the dynamic data in this instance. DeleteList(head); head = nullptr; return; } ListT & ListT::operator = (const ListT & rhs){ // delete the dynamic data in this instance. DeleteList(head); head = nullptr; // copy the static data size = rhs.size; // copy the dynamic data CopyList(head, rhs.head); return * this; } void ListT::Insert(int item){ NodeT * tmp = nullptr; tmp = new NodeT; tmp->data = item; tmp->next = head; head = tmp; size ++; return; } void ListT::Delete(int item){ NodeT * tmp = nullptr; NodeT * follow = nullptr; if (head != nullptr) { if (head->data == item) { tmp = head; head = head->next; delete tmp; size--; } else { follow = head; tmp = head->next; // find intem in the list (or get to the end) while(tmp!= nullptr and tmp->data != item) { follow = tmp; tmp=tmp->next; } if (tmp != nullptr) { follow->next = tmp->next; delete tmp; size --; } } } return; } bool ListT::Find(int item) const{ NodeT * tmp = nullptr; bool found = false; for(tmp = head; tmp != nullptr and not found; tmp=tmp->next) { if (tmp->data == item) { found = true; } } return found; } size_t ListT::Size(void) const{ return size; } void ListT::PrintList(void) const{ NodeT * tmp = nullptr; for(tmp = head; tmp != nullptr; tmp=tmp->next) { cout << tmp->data << " " ; } return; }