#include #include "ListT.h" using namespace std; struct NodeT { int data; NodeT * next; }; ListT::ListT(){ size = 0; head = nullptr; current = nullptr; } NodeT * CopyList(NodeT * src) { NodeT * head; NodeT * trace1, * trace2; NodeT * tmp; if (src == nullptr) { head = nullptr; } else { // special case, set the head pointer. head = new NodeT; head->data = src->data; head->next = nullptr; // now copy the rest of the list. trace1 = src->next; trace2 = head; while(trace1 != nullptr) { // build a new node. tmp = new NodeT; tmp->data = trace1->data; tmp->next = nullptr; // link it into the list trace2->next = tmp; // advance the two pointers. trace2 = tmp; trace1 = trace1->next; } } return head; } void AdjustCurrent(NodeT * srcList, NodeT * destList, NodeT * srcCurrent, NodeT * & destCurrent) { NodeT *tmp; if (srcCurrent == nullptr) { destCurrent = nullptr; } else { tmp = srcList; destCurrent = destList; while(tmp != nullptr and tmp!= srcCurrent) { tmp = tmp->next; destCurrent = destCurrent->next; } } } ListT::ListT(const ListT & src) { // copy static data size = src.size; // copy dynamic data head = CopyList(src.head); AdjustCurrent(src.head, head, src.current, current); } void DeleteList(NodeT * list) { NodeT * tmp; while(list != nullptr) { tmp = list; list = list->next; delete tmp; } } ListT & ListT::operator =(const ListT & other){ if (this != &other) { DeleteList(head); head = CopyList(other.head); size = other.size; AdjustCurrent(other.head, head, other.current, current); } return *this; } ListT::~ListT(){ DeleteList(head); } void ListT::Insert(int i){ NodeT * tmp; NodeT * seek, * tail; tmp = new(NodeT); tmp->data = i; tmp->next = nullptr; size++; if (head == nullptr or head->data > i) { tmp->next = head; head = tmp; } else { seek = head->next; tail = head; while (seek != nullptr and seek->data < i) { tail = seek; seek = seek->next; } tail ->next = tmp; tmp->next = seek; } } void ListT::Delete(int i){ NodeT * tmp, *tail; if (head != nullptr) { if (head->data == i) { tmp = head; head = tmp->next; delete tmp; size --; } else { tmp = head->next; tail = head; while(tmp != nullptr and tmp->data != i) { tail = tmp; tmp = tmp->next; } if (tmp != nullptr) { tail->next = tmp->next; delete tmp; size--; } } } } bool ListT::IsPresent(int i){ NodeT * tmp; tmp = head; while (tmp != nullptr and tmp->data != i) { tmp = tmp -> next; } return tmp != nullptr; } int ListT::Size(){ return size; } void ListT::Home(){ current = head; } bool ListT::IsLast(){ return current == nullptr; } void ListT::Next(){ if (current != nullptr) { current = current->next; } } int ListT::Current(){ if (current != nullptr) { return current->data; } return -1; }