#include #include "ListT.h" using namespace std; void TellAboutList(ListT list); void PrintList(ListT list); void SimpleTest(); void RandomTest(); const size_t MAX_ITEMS = 10; int main() { cout << "RUNNING SIMPLE TEST " << endl; cout << "++++++++++++++++++++++++++++++++++++++++" << endl; SimpleTest(); cout << "++++++++++++++++++++++++++++++++++++++++" << endl; cout << endl << endl; cout << "RUNNING Random TEST " << endl; cout << "++++++++++++++++++++++++++++++++++++++++" << endl; RandomTest(); cout << "++++++++++++++++++++++++++++++++++++++++" << endl; cout << endl << endl; return 0; } int RandInList(ListT & list) { int pos =0; int key = 0; if (list.Length() > 0) { pos = rand() % static_cast (list.Length()); list.Reset(); while(pos > 0) { list.Next(); pos--; } key = list.GetItem(); } return key; } void RandomTest() { int trials = 1000; int trial; int action; int key; ListT list; for(trial =0; trial < trials; trial ++) { action = rand() % 6; key = rand() % 50 - 25; switch(action) { case 0: // double the chances of an insert. [[fallthrough]]; case 1: cout << "Insert " << key; cout << " into "; PrintList(list); cout << endl; list.Insert(key); break; case 4: // delete with a key from the list key = RandInList(list); [[fallthrough]]; case 2: cout << "Delete " << key; cout << " from "; PrintList(list); cout << endl; list.Delete(key); break; case 5: // search with a key from the list key = RandInList(list); [[fallthrough]]; case 3: cout << "Search "; PrintList(list); cout << " for " << key << "." << endl; if (list.IsPresent(key)) { cout << "\t " << key << " is in the list " << endl; } else { cout << "\t " << key << " is not in the list " << endl; } break; } cout << "The list is now "; PrintList(list); cout << endl; cout << endl; } } void SimpleTest() { int i; ListT list; TellAboutList(list); for(i =0; i < static_cast(MAX_ITEMS+2); i++) { cout << "Inserting " << i << endl; list.Insert(i); TellAboutList(list); } for(i=-3;i< static_cast (MAX_ITEMS+2) ;i++) { cout << "Searching "; PrintList(list); cout << " for " << i << endl; if (list.IsPresent(i)) { cout << "\t" << i << " is in the list." << endl; } else { cout << "\t" << i << " is not in the list." << endl; } } cout << endl; cout << "Deleting 9" << endl; list.Delete(9); TellAboutList(list); if (list.IsPresent(9)) { cout << "Error, 9 is still there after deleting it" << endl; } for(i=-3;i< static_cast (MAX_ITEMS+4) ;i++) { cout << "Deleting " << i << endl; list.Delete(i); TellAboutList(list); } } void TellAboutList(ListT list){ cout << endl; cout << "The list contains " << list.Length() << " elements." << endl; cout << "The list is"; if (not list.IsEmpty()) { cout << " not"; } cout << " empty and"; if (not list.IsFull()) { cout << " not"; } cout << " full." << endl; if( not list.IsEmpty()) { cout << "The list contiains: " << endl; PrintList(list); } cout << endl << endl; return; } void PrintList(ListT list){ list.Reset(); bool first = true; ListT b; b = list; cout << "{"; while (list.HasNext() ) { if (first) { first = false; } else { cout << ", " ; } cout << list.GetItem(); list.Next(); } cout << "}"; return; }