#include #include "ListT.h" using namespace std; struct NodeT { int data; NodeT * next; }; ListT::ListT(){ head = nullptr; size = 0; current = nullptr; } NodeT * CopyList(NodeT * other) { // general plan, use // src to walk through the existing list. // target to walk through the new list. // tmp for new node. // NodeT * src = other, * target = nullptr, * tmp = nullptr, * rv = nullptr; while (src != nullptr) { tmp =new NodeT; tmp->data =src->data; tmp->next = nullptr; if (target == nullptr) { rv = tmp; target = tmp; } else { target ->next = tmp; target = target ->next; } src = src->next; } return rv; } void DeleteList(NodeT * list) { NodeT * tmp; tmp = list; while (tmp != nullptr) { list = tmp; tmp = tmp->next; delete list; } return; } ListT::ListT(const ListT & other) { size = other.size; head = CopyList(other.head); current = head; return; } ListT & ListT::operator = (const ListT & other) { if (this != &other) { size = other.size; DeleteList(head); head = CopyList(other.head); current = head; } return *this; } ListT::~ListT() { DeleteList(head); } bool ListT::IsEmpty(void) const{ return size==0; } bool ListT::IsFull(void) const{ return false; } size_t ListT::Length(void) const{ return size; } NodeT * ListT::Find(int key) const { NodeT * pos = head; bool found = false; while(pos != nullptr and not found) { if (pos->data == key) { found = true; } else { pos = pos->next; } } return pos; } bool ListT::IsPresent(int key) const{ NodeT * pos; pos = Find(key); return pos != nullptr; } void ListT::Delete(int key){ NodeT * pos, * tail; if (head != nullptr) { if (head->data == key) { tail = head->next; delete head; head = tail; } else { pos = head->next; tail = head; while (pos != nullptr and pos->data != key) { tail = pos; pos = pos -> next; } if (pos != nullptr) { tail->next = pos->next; delete pos; size --; current = head; } } } } // for now, insert it into the front of the list. void ListT::Insert(int key){ NodeT * tmp = new NodeT; tmp->data = key; tmp->next = head; head = tmp; size++; current = head; } void ListT::Reset(void){ current = head; } bool ListT::HasNext(void) const{ return current != nullptr; } void ListT::Next(void){ if (current != nullptr) { current = current->next;; } } int ListT::GetItem(void) const{ int rv = 0; if (current != nullptr) { rv = current->data; } return rv; }