#include "stdstuff.h" #include "List.h" // the destructor destroys the list one node at a time, starting from the head List::~List () { Node *t; while (head != NULL) { t = head; head = head -> next; delete t; } } // 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); } // Method "countOddValues" returns the number of nodes in the list containing odd values. // Note that "containing" refers to the *contents* of the node, not its position in the list. // If the list contains no odd values, it returns 0. int List::countOddValues() const { int count = 0; for (Node* c = head; c!=NULL; c=c->next) { // check every node if (c->data%2!=0) // if it's odd, count it count++; } return count; } // Method "deleteValuesMultThree" deletes every node containing // a value that is an exact multiple of 3 (i.e. ..., -9, -6, -3, 0, 3, 6, ...). // If the list contains no values that are multiples of 3, it has no effect. void List::deleteValuesMultThree() { Node *c = head, *p = NULL, *temp; while (c!=NULL) { if (c->data%3==0) { // node contains a multiple of 3, so delete it and move to next node if (p==NULL) // if deleting first node head = c->next; // head now points to 2nd else p->next = c->next; // otherwise previous node points to next node temp = c; c = c->next; // move on to next node delete temp; // and delete the one containing a multiple of 3 } else { // just move on to next node p = c; c = c->next; } } }