/*---- signature: main: void -> int purpose: testing program for my collection of linked list functions compiled using: (written on 1 line!) g++ linked-list-tests.cpp linked-list-functs.cpp Node.cpp -o linked-list-tests run using: ./linked-list-tests | more # to better look over the results 8-) by: Sharon Tuttle last modified: 2022-11-11 - adding some comments, cleaning up a bit 2022-11-01 - Week 11 Lecture 1 version 2022-10-28 - original version ----*/ #include <cstdlib> #include <iostream> #include <string> #include <cmath> #include "Node.h" #include "linked-list-functs.h" using namespace std; int main() { cout << boolalpha; // testing insert_at_front cout << endl << "*** Testing insert_at_front ***" << endl; Node *begin = NULL; insert_at_front(begin, 47); /*--- if the above worked, list now contains 47 ---*/ cout << (begin->get_data() == 47) << endl; cout << (begin->get_next() == NULL) << endl; insert_at_front(begin, 36); /*--- after this: * begin should point to a node containing 36, * ...whose next data field should point to a node containing 47 * ...and the node with 47 should have a NULL next data field. ---*/ cout << (begin->get_data() == 36) << endl; cout << ( begin->get_next()->get_data() == 47 ) << endl; cout << ( begin->get_next()->get_next() == NULL ) << endl; // testing print_list cout << endl << "*** Testing print_list ***" << endl; Node* my_first; my_first = NULL; cout << endl << "should see List header, and no contents:" << endl; print_list(my_first); insert_at_front(my_first, 12); insert_at_front(my_first, 700); insert_at_front(my_first, 15); cout << endl << "should see List contents of 15, then 700, then 12" << endl; print_list(my_first); // testing delete_list cout << endl << "*** Testing delete_list ***" << endl; cout << (delete_list(my_first) == 3) << endl; Node *empty; empty = NULL; cout << (delete_list(empty) == 0) << endl; // and want to clean up list Node pointer begin // points to, also -- so why not an additional // test while I am at it? 8-) cout << (delete_list(begin) == 2) << endl; // testing search_for cout << endl; cout << "*** Testing search_for ***" << endl; Node *stuff = NULL; insert_at_front(stuff, 300); insert_at_front(stuff, 400); insert_at_front(stuff, 250); insert_at_front(stuff, 400); insert_at_front(stuff, 25); cout << (search_for(stuff, 10) == NULL) << endl; cout << (search_for(stuff, 25) == stuff) << endl; cout << (search_for(stuff, 400) == stuff->get_next()) << endl; Node *found_node = search_for(stuff, 300); cout << "I hope found_node's node has value 300: " << found_node->get_data() << endl; // testing insert_after cout << endl; cout << "*** Testing insert_after ***" << endl; Node *node_before = search_for(stuff, 250); insert_after(node_before, 8); cout << "Should see elements 25, 400, 250, 8, 400, 300:" << endl; print_list(stuff); node_before = search_for(stuff, 300); insert_after(node_before, 16); cout << "Should see elements 25, 400, 250, 8, 400, 300, 16:" << endl; print_list(stuff); // clean-up cout << endl; cout << "cleaning up after later tests: " << endl; cout << "how many nodes freed?" << endl; cout << delete_list(stuff) << endl; cout << "stuff == NULL? " << (stuff == NULL) << endl; cout << endl; return EXIT_SUCCESS; }