For use with CMSC-341, Spring 99, Project 2 Here is the definition of the main function Copy it to your Proj2.C file. Remember: -- complete the documentation -- write the function void getCommandLine(int argc, char * argv[], int * nelem, int * startval, List ** lst) ------------------------------------------------ // Reads command line arguments and inserts values // into nelem, startval, and lst appropriately. // // The command line must be of the form: // Proj2 number-of-entries start-val list-type // where: // number-of-entries is the number of integers to // insert into the list, // start-val is the first integer to be inserted, and // list-type is either LinkedList or ArrayList // // This function constructs the appropriate type // of list (LinkedList or ArrayList) based on the last // command line argument. // // Param: argc - the number of command line arguments // Param: argv - array of the command line arguments // Param: nelem - upon return will contain the number of // elements to be inserted in list // Param: startval - upon return will contain the // starting value for the elements // Param: lst -- upon return will contain a pointer to the // List on which to perform the operations. The List itself // will be constructed in this function. // // This function must exit (after issuing a "Usage" message to // cerr) if any command line arguments are invalid: // 1) the number of arguments must be correct // 2) number-of-entries must be greater than zero // 3) start-value must be a number // 4) list-type must be either "LinkedList" or "ArrayList" template void getCommandLine(int argc, char * argv[], int * nelem, int * startval, List ** lst); int main(int argc, char *argv[]) { int i; int nelem; // number of elements int startval; // starting value for elements Iterator * iter1; Iterator * iter2; List * list; getCommandLine(argc, argv, &nelem, &startval, &list); cout << "------- Using " << list->ToString() << " -------" << endl << endl; // Insert elements at index 1 into initially empty list. cout << "-- Insert (at index 1) " << nelem << " elements, "; cout << "starting with " << startval << " --" << endl; for (i = 1; i <= nelem; i++) { int val = startval + i - 1; list->Insert(val, 1); } cout << *list << endl; cout << "length = " << list->Length() << endl; // Clear the list cout << "-- Clear the list --" << endl; // for (i = 0; i < nelem; i++) // list->Delete(1); list->MakeEmpty(); cout << *list << endl; cout << "length = " << list->Length() << endl; // Insert elements in-order into initially empty list. cout << "-- Insert (in-order) " << nelem << " elements, "; cout << "starting with " << startval << " --" << endl; for (i = 1; i <= nelem; i++) { int val = startval + i - 1; list->Insert(val, i); } cout << *list << endl; cout << "length = " << list->Length() << endl; // Clear the list cout << "-- Clear the list --" << endl; for (i = 0; i < nelem; i++) list->Delete(1); cout << *list << endl; cout << "length = " << list->Length() << endl; // Append elements into initially empty list. cout << "-- Append " << nelem << " elements, "; cout << "starting with " << startval << " --" << endl; for (i = startval; i < startval + nelem; i++) list->Append(i); cout << *list << endl; cout << "length = " << list->Length() << endl; // show that more than one iterator can be used // at one time cout << "-- Use multiple iterators simultaneously --" << endl; iter1 = list->GetIterator(); iter2 = list->GetReverseIterator(); while (iter1->HasNext()) cout << (iter1->Next() + iter2->Next()) << " "; cout << endl; // de-allocate iter2, don't need it anymore delete iter2; iter2 = NULL; // show that iterator can be output directly cout << "-- Output directly from reverse iterator --" << endl; delete iter1; // de-allocate old iter1 iter1 = list->GetReverseIterator(); cout << iter1 << endl; // de-allocate iter1, don't need it anymore delete iter1; iter1 = NULL; // repeatedly delete first element, even on an empty list cout << "Repeatedly delete 1st element, even on empty list" << endl; for (i = 0; i < nelem + 1; i++) { cout << *list << endl; list->Delete(1); } cout << *list << endl; }