#include "stdstuff.h" #include "List.h" // Utility method to delete all elements of a list. // Note: used by destructor and operator=. void List::deleteList() { // delete the list Node *c; while (head != NULL) { c = head; head = head -> next; delete c; } // head is now NULL head=NULL; } // Utility method to copy from one list to another. // The list we are writing to must have already been deleted if it existed. // Note: used by copy constructor and operator= void List::copyFrom (const List &otherList){ Node *c, *tail, *n; tail=NULL; head=NULL; for (c=otherList.head; c!=NULL; c=c->next) { // walk through other list n = new Node(c->data, NULL); // take the current element and make a node if (tail==NULL) head=n; // add as first element else tail->next = n; // add at end tail=n; } } //Copy Constructor List::List(const List &otherList) { // use helper method to copy the list copyFrom(otherList); } List::~List () { // use helper method to delete the list deleteList(); } void List::outputList () const { Node *c = head; int i = 0; cout << "The list contains:" << endl; while (c != NULL) { cout << " " << c -> data; if (++i == 10) { cout << endl; i = 0; } c = c -> next; } if (i != 0) cout << endl; } void List::insert (int data) { head = new Node (data, head); } //Assignment of Lists List& List::operator=(const List &otherList) { // delete the current list deleteList(); // use helper method to copy list copyFrom(otherList); return *this; }