// piecetest.cxx - does three tests of list_piece implementation // cmc, 10/23/2015 #include "listpiece.h" #include #include using namespace std; using namespace main_savitch_5; void print_list(const node *start_ptr); // prints list values from start_ptr to end of list void print_array(double array[], size_t start, size_t end); // prints array elements from start to just before end void do_test(double data[], const node *datalist, size_t mid); // tests and shows results of using list_piece to split at mid const size_t SIZE = 20; // global constant int main() { double data[SIZE]; size_t i; for (i = 0; i < SIZE; i++) data[i] = i+1; // make a list of SIZE nodes in order of data and print it node *datalist = NULL, *ptr; list_head_insert(datalist, data[0]); for (i = 1, ptr = datalist; i < SIZE; i++, ptr=ptr->link()) list_insert(ptr, data[i]); cout << "Here is the whole original list:" << endl; print_list(datalist); // test two (about) equal pieces cout << "\nTEST 1 - two equal parts." << endl; size_t mid = SIZE / 2; do_test(data, datalist, mid); // test for first piece just head cout << "\nTEST 2 - first part just first node." << endl; do_test(data, datalist, 1); // test for second piece just tail cout << "\nTEST 3 - second part just last node." << endl; do_test(data, datalist, SIZE-1); return 0; } void print_list(const node *start_ptr) { const node *n = start_ptr; while (n) { cout << n->data() << " "; n = n->link(); } cout << endl; } void print_array(double array[], size_t start, size_t end) { for (size_t i = start; i < end; i++) cout << array[i] << " "; cout << endl; } void do_test(double data[], const node *datalist, size_t mid) { cout << "Correct split for this test: " << endl; print_array(data, 0, mid); print_array(data, mid, SIZE); const node *datamid = list_locate(datalist, mid+1); node *copyhead1, *copyhead2, *copytail1, *copytail2; // Here is where your function gets called twice. list_piece(datalist, datamid, copyhead1, copytail1); list_piece(datamid, NULL, copyhead2, copytail2); cout << "Now your two parts:" << endl; print_list(copyhead1); print_list(copyhead2); cout << "First piece: head at " << copyhead1->data() << "; tail at " << copytail1->data() << endl; cout << "Second piece: head at " << copyhead2->data() << "; tail at " << copytail2->data() << endl; }