// This is the test program and question description for Fall 2008 SYSC 2002 lab test #4 version #1 (for D1) for TA testing. // You are to write the three missing methods in the List class, plus any other helper functions or helper // methods that you like to solve the problems. Your solutions must be recursive. // Note that your code must work for all lists, not just for the examples given here. // The following code and the sample executable should make it clear as to what your methods must do. // IF YOUR SOLUTIONS ARE NOT RECURSIVE YOU WILL GET A VERY LOW MARK!! // Submit updated files List.h and List.cpp using the submit program provided. #include "stdstuff.h" #include "List.h" void realMain () { List list1, list2, list3, list4, list5; for (int i=10; i>0; i--) { list1.insert(i); list2.insert(i+1); list1.insert(i-1); list3.insert(10-2*i); } list5.insert(2); // Method sumGreaterThan(n) returns the sum of the values in the given list that are greater than n. // Note that "greater than" refers to the contents (value) of the node *not* its position in the list. // If there are no values greater than n, this method returns 0. // This method does *not* change the list. cout << "\n\nThe sum of the values in list1 greater than 6 is (should be 58): " << list1.sumGreaterThan(6) << ".\n\n"; cout << "The sum of the values in list2 greater than 0 is (should be 65): " << list2.sumGreaterThan(0) << ".\n\n"; cout << "The sum of the values in list3 greater than -10 is (should be 0): " << list3.sumGreaterThan(-10) << ".\n\n"; cout << "The sum of the values in list4 greater than 100000 is (should be 0): " << list4.sumGreaterThan(10000) << ".\n\n"; cout << "The sum of the values in list5 greater than -10 is (should be 2): " << list5.sumGreaterThan(-10) << ".\n\n\n"; // Method hasValueLessThan(m) returns true if the list contains a value less than m, false otherwise. // Note that "less than" refers to the contents (value) of the node *not* its position in the list. // If there are no values in the list, this method returns false. // This method does *not* change the list. if (list1.hasValueLessThan(100)) cout << "Correct: List1 contains a value less than 100.\n\n"; else cout << "Incorrect: List1 does not contain a value less than 100.\n\n"; if (list2.hasValueLessThan(-100)) cout << "Incorrect: List2 contains a value less than -100.\n\n"; else cout << "Correct: List2 does not contain a value less than -100.\n\n"; if (list3.hasValueLessThan(-4)) cout << "Correct: List3 contains a value less than -4.\n\n"; else cout << "Incorrect: List3 does not contain a value less than -4.\n\n"; if (list4.hasValueLessThan(100)) cout << "Incorrect: List4 contains a value less than 100.\n\n"; else cout << "Correct: List4 does not contain a value less than 100.\n\n"; if (list5.hasValueLessThan(-4)) cout << "Incorrect: List5 contains a value less than 2.\n\n\n"; else cout << "Correct: List5 does not contain a value less than 2.\n\n\n"; // Method hasDoubleValueAfter() returns true if the list contains a node directly followed by a node containing // double the value of the first node, and false otherwise. // If there are no values in the list, this method returns false. // This method does *not* change the list. if (list1.hasDoubleValueAfter()) cout << "Correct: List1 contains a node directly followed by one containing double its value.\n\n"; else cout << "Incorrect: List1 does not contain a node directly followed by one containing its double.\n\n"; if (list2.hasDoubleValueAfter()) cout << "Incorrect: List2 contains a node directly followed by one containing double its value.\n\n"; else cout << "Correct: List2 does not contain a node directly followed by one containing its double.\n\n"; if (list3.hasDoubleValueAfter()) cout << "Correct: List3 contains a node directly followed by one containing double its value.\n\n"; else cout << "Incorrect: List3 does not contain a node directly followed by one containing its double.\n\n"; if (list4.hasDoubleValueAfter()) cout << "Incorrect: List4 contains a node directly followed by one containing double its value.\n\n"; else cout << "Correct: List4 does not contain a node directly followed by one containing its double.\n\n"; if (list5.hasDoubleValueAfter()) cout << "Incorrect: List5 contains a node directly followed by one containing double its value.\n\n\n"; else cout << "Correct: List5 does not contain a node directly followed by one containing its double.\n\n\n"; } int main () { try { realMain (); } catch (exception &e) { // catches all uncaught exceptions cout << "\nException <" << e.what() << "> occurred.\n"; } pause (); return 0; }