#include #include "ListT.h" using namespace std; struct NodeT { int data; NodeT * next; }; ListT::ListT(){ size = 0; head = nullptr; return; } void CopyList(NodeT * & dest, NodeT * src) { NodeT * tmp = nullptr; NodeT * travel = nullptr; NodeT * follow = nullptr; for(travel = src; travel != nullptr; travel = travel->next) { tmp = new NodeT; tmp->next = nullptr; tmp->data = travel->data; if ( follow == nullptr) { follow = tmp; dest = tmp; } else { follow->next = tmp; follow = tmp; } } return; } ListT::ListT(const ListT & rhs){ CopyList(head, rhs.head); size = rhs.size; return; } void DeleteList(NodeT * head){ NodeT * tmp; while( head != nullptr) { tmp = head; head = head->next; delete tmp; } return; } ListT::~ListT(){ DeleteList(head); head = nullptr; return; } ListT & ListT::operator = (const ListT & rhs){ // delete existing dynamic memory DeleteList(head); head = nullptr; // copy both static and dynamic memory from rhs CopyList(head, rhs.head); size = rhs.size; 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) { // delete the first item tmp = head; head = head->next; delete tmp; size --; } else { tmp = head->next; follow = head; while(tmp!= nullptr and tmp->data != item) { follow = tmp; tmp = tmp->next; } if (tmp != nullptr) { follow -> next = tmp->next; size--; delete tmp; } } } 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; for(tmp = head; tmp != nullptr; tmp=tmp->next) { cout << tmp -> data << " "; } return; }