#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 number of elements in the list that are equal to value int List::subCountEqual(int value, Node* subHead) const { // empty list has no values if(subHead==NULL) return 0; // if this element equals value answer is 1 + number of that value in rest of list if(subHead->data==value) return 1 + subCountEqual(value,subHead->next); // otherwise answer is just number of that value in rest of list return subCountEqual(value,subHead->next); } // returns the number of elements in the list that are equal to value // (recursive) int List::countEqual(int value) const { return subCountEqual(value,head); } // sub method to insert the given value after each even value void List::subInsertAfterEvens(int value, Node* subHead) { // nothing to do if list is empty if(subHead==NULL) return; // if this element is even, insert the given value and move // ahead one node if(subHead->data%2==0) { subHead->next = new Node(value,subHead->next); subHead=subHead->next; } // and insert value after all evens in rest of list subInsertAfterEvens(value,subHead->next); } // inserts the given value after each even value // (recursive) void List::insertAfterEvens(int value) { subInsertAfterEvens(value,head); } /* // returns the number of elements in the list that are equal to value // (iterative) int List::countEqual(int value) const { int count = 0; // initialize counter for (Node*c=head; c!=NULL; c=c->next) { // go through list if (c->data==value) // increment count if value = current data value count++; } return count; } // inserts the given value after each even value // (iterative) void List::insertAfterEvens(int value) { for (Node*c=head; c!=NULL; c=c->next) { // go through list if (c->data%2==0) { // current data value is odd c->next = new Node (value, c->next); // add new node after // and move forward one node c=c->next; // to avoid infinite loop if value even } } } */ // sub method to return true if the two lists are the same length, false otherwise bool List::subIsSameLengthAs(Node* subHead, Node* otherSubHead) const { // if both lists are empty, they are the same length if(subHead==NULL && otherSubHead==NULL) return true; // if just one is empty, they are not the same length if(subHead==NULL || otherSubHead==NULL) return false; // the lists have at least one value, so they are the same length iff // the rests of the lists are the same length return subIsSameLengthAs(subHead->next,otherSubHead->next); } // returns true if the two lists are the same length, false otherwise bool List::isSameLengthAs(const List &otherList) const { return subIsSameLengthAs(head,otherList.head); }