#include "stdstuff.h" #include "List.h" // private copy method used by copy constructor and operator= // creates a duplicate of the given linked list void List::copy (const List &otherList) { Node *c, *tail, *n; // so far the list we're creating does not have a last node tail = head = NULL; // for all nodes in the other stack's list for (c = otherList.head; c != NULL; c = c -> next) { // create a copy of the node n = new Node(c -> data, NULL); // make the last node in the list being created point to it if (tail == NULL) { // no last node yet head = n; // new node goes at the start of the list } else { tail -> next = n; } // our new node is now the last node tail = n; } } // private deleteAll method used by destructor and operator= // deletes nodes, one at a time void List::deleteAll () { Node *t; while (head != NULL) { t = head; head = head -> next; delete t; } } // copy constructor just copies list List::List (const List &otherList) { copy(otherList); } // the destructor deletes all nodes List::~List () { deleteAll(); } // prints the list to cout one node at a time, starting with the head 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; } // inserts a node into the list at the head of the list void List::insert (int data) { head = new Node (data, head); } // assigns one linked list to another List& List::operator= (const List &otherList) { deleteAll (); // nix the linked list for the object being assigned to copy (otherList); // copy the other list return *this; // as always for operator=, so a=b=c works } // returns the sum of the values of the list int List::sumValues() const { return subSumValues(head); } // returns the sum of the values of the sublist int List::subSumValues(Node *subHead) const { // an empty list's values sum to 0 if (subHead==NULL) return 0; // otherwise the sum is the current value, // plus the sum of the values in the rest of the list return subHead->data + subSumValues(subHead->next); } // returns the number of values in the list int List::numValues() const { return subNumValues(head); } // returns the number of values in the sublist int List::subNumValues(Node *subHead) const { // an empty list's has 0 values if (subHead==NULL) return 0; // otherwise the number of values is 1, // plus the number in the rest of the list return 1 + subNumValues(subHead->next); } // returns true if the values of the list are sorted // in ascending order, false otherwise bool List::isSortedAscending() { return subIsSortedAscending(head); } // returns true if the values of the sublist are sorted // in ascending order, false otherwise bool List::subIsSortedAscending(Node *subHead) { // if the list has 0 or 1 values it is sorted if (subHead==NULL || subHead->next==NULL) return true; // otherwise if the current value is greater than the // next one, the list is not sorted if (subHead->data > (subHead->next)->data) return false; // otherwise the list is sorted iff the rest of the list is sorted return subIsSortedAscending(subHead->next); }