#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; } // sub method to return the sum of all values in the list that are greater than value int List::subSumMoreThan(int value, Node* subHead) const { // empty list has no values greater than value if(subHead==NULL) return 0; // if current element is greater than value, answer is this value + // sum of values greater than value in the rest of the list if(subHead->data>value) return subHead->data + subSumMoreThan(value,subHead->next); // otherwise the answer is just the sum of values greater than // value in the rest of the list return subSumMoreThan(value,subHead->next); } /* // returns the sum of all values in the list that are greater than value // (recursive) int List::sumMoreThan(int value) const { return subSumMoreThan(value,head); } */ // sub method to duplicate all even values in the list void List::subDuplicateEvens(Node* subHead) { // if the list is empty, there's nothing to do if(subHead==NULL) return; // if this node is even, duplicate it and move forward // one node if(subHead->data%2==0) { subHead->next = new Node(subHead->data,subHead->next); subHead=subHead->next; } // duplicate the even nodes in the rest of the list subDuplicateEvens(subHead->next); } // duplicates all even values in the list // (recursive) void List::duplicateEvens() { subDuplicateEvens(head); } // returns the sum of all values in the list that are greater than value // (iterative) int List::sumMoreThan(int value) const { int sum = 0; // initialize sum for (Node*c=head; c!=NULL; c=c->next) { // go through list if (c->data>value) // it's larger sum+=c->data; } return sum; } /* // duplicates all even values in the list // (iterative) void List::duplicateEvens() { for (Node*c=head; c!=NULL; c=c->next) { // go through list if (c->data%2==0) { // duplicate node c->next = new Node(c->data,c->next); c = c->next; // and move forward one } } } */ // sub method to return true if the list is shorter than the other list, false otherwise bool List::subIsShorterThan(Node* subHead, Node* otherSubHead) const { // if other list empty, list is not shorter if(otherSubHead==NULL) return false; // if list is empty it is shorter if(subHead==NULL) return true; // list is shorter that list iff the rest of list is shorter than the rest of other list return subIsShorterThan(subHead->next,otherSubHead->next); } // returns true if the list is shorter than the other list, false otherwise bool List::isShorterThan(const List &otherList) const { return subIsShorterThan(head,otherList.head); }