#include #include "DListT.h" using namespace std; struct DListNodeT { DListNodeT * left; DListNodeT * right; string data; }; DListNodeT * CopyList(DListNodeT * src) { DListNodeT * dest = nullptr; DListNodeT * destWalk, * destTail, * srcWalk; srcWalk = src; if (src != nullptr) { dest = new DListNodeT; dest ->data = src->data; destTail = dest; srcWalk = srcWalk->right; while (srcWalk != src) { // grab a new node destWalk = new DListNodeT; destWalk ->data = srcWalk->data; // make the pointer pair work destWalk ->left = destTail; destTail->right = destWalk; // move to the end destTail = destWalk; srcWalk = srcWalk->right; } // finally finish the circle dest->left = destTail; destTail->right = dest; } return dest; } DListT::DListT() { head = nullptr; current = nullptr; nodeCount = 0; return; } DListT::DListT(const DListT & src) { DListNodeT * tmp; nodeCount = src.nodeCount; head = CopyList(src.head); current = head; for(tmp = src.head; tmp!= src.current; tmp=tmp->right) { current = current->right; } return; } void DeleteList(DListNodeT * head) { DListNodeT * tmp = head; if (tmp!= nullptr) { tmp->right->left = nullptr; } while (tmp != nullptr) { head = tmp->left; delete tmp; tmp = head; } return; } DListT::~DListT() { DeleteList(head); return; } DListT & DListT::operator =(const DListT & rhs) { DListNodeT * tmp; DeleteList(head); head = CopyList(rhs.head); nodeCount = rhs.nodeCount; current = head; for(tmp = rhs.head; tmp!= rhs.current; tmp=tmp->right) { current = current->right; } return *this; } void DListT::Home(void) { current = head; return; } void DListT::Left(){ if (current != nullptr) { current = current->left; } return; } void DListT::Right(){ if (current != nullptr) { current = current->right; } return; } string DListT::Data() const{ if (current != nullptr) { return current->data; } else { cout << "Error: Attempt to access Empty List" << endl; } return "No DATA"; } size_t DListT::Size(void) const{ return nodeCount; } // insert after, current points at the new node. void DListT::InsertAfter(string newData){ DListNodeT * tmp; tmp = new DListNodeT; tmp->data = newData; if (current == nullptr) { head = tmp; tmp->left = tmp; tmp->right = tmp; } else { tmp->right = current->right; tmp->right->left = tmp; current ->right = tmp; tmp->left = current; } current = tmp; nodeCount++; return; } // insert, current points at the new node. void DListT::Insert(string newData){ DListNodeT * tmp; tmp = new DListNodeT; tmp->data = newData; if (head == nullptr) { head = tmp; current = tmp; tmp->right = tmp; tmp->left = tmp; } else { tmp->right = current; tmp->left = current->left; tmp->left->right = tmp; tmp->right->left = tmp; if (current == head) { head = tmp; } current = tmp; } nodeCount++; } void DListT::Delete(){ DListNodeT * tmp; if (current != nullptr) { if (nodeCount == 1) { delete current; head = nullptr; current = nullptr; nodeCount = 0; } else { tmp = current; current->left->right = current->right; current->right->left = current->left; if(head == current) { head = current->right; current = head; } else { current = current->left; } delete tmp; nodeCount--; } } return; }