/* Goal: Provide an interactive tester for the linked lists and associated functions all ready implemented * * Tester allows lists to be manipulated by selecting choices from a menu. All implemented functionality * of the lists can be used through the tester * * Functions used in the tester are: * int testAdd(list *dataList) * int testMoveCursor(list *dataList) * int testConcatenateList(list *dataList1) * int testReadFile(list *dataList) * int testFileOutput(list *dataList) * Full descriptions of these functions are given below * * Author: Nathan Corvino * * Created 2/9/00 * First fully working version 2/19/99 * Code cleaned up and additional comments added 2/23/99 * */ #include #include "listtester.h" /******************************** * List Tester Functions * ******************************** */ /***************************************************************************************************** * Purpose: to provide an interface with which to test the various add functions of the linked lists * * Parameters: the list on which to test the add functions * * Postconditions: an element was added to dataList as indicated by the user * * Returns: the return of the add function tested * *****************************************************************************************************/ int testAdd(list *dataList) { node *newNode; char buffer[MAXLINE]; int selection; /* get the data for the node to be added */ printf("Please enter a string to be added to the list:"); fgets(buffer, sizeof(buffer), stdin); buffer[strlen(buffer)-1] = '\0'; /* create the new node */ newNode = createNode(buffer, NULL, NULL); /* find out where the new node should be added */ printf("Add New Node to:\n"); printf(" 1 -> Add Node to Beginning of List\n"); printf(" 2 -> Add Node to End of List\n"); printf(" 3 -> Add Node Before Cursor\n"); printf(" 4 -> Add Node After Cursor\n"); printf("Enter Selection: "); while(1) { fgets(buffer, sizeof(buffer), stdin); sscanf(buffer, "%d", &selection); /* add the node to the appropriate place, or ask again for a selection if an invalid option is selected */ switch(selection) { case 1: return(addToStart(newNode, dataList)); case 2: return(addToEnd(newNode, dataList)); case 3: return(addBeforeCursor(newNode, dataList)); case 4: return(addAfterCursor(newNode, dataList)); default: printf("Invalid Option - Reenter Selection\n"); } } } /***************************************************************************************************** * Purpose: to test the cursor movement functions of the linked lists. Distance and direction can be * * specified * * Parameters: list *dataList: the list on which to test the cursor movement functions * * Postconditions: the cusror of dataList was moved as indicated by the user * * Returns: the return of the move function tested * *****************************************************************************************************/ int testMoveCursor(list *dataList) { char buffer[MAXLINE]; int numberToMove; int selection; printf("Please enter the distance the cursor should move:"); fgets(buffer, sizeof(buffer), stdin); sscanf(buffer, "%d", &numberToMove); printf("Choose to advance or regress the cursor:\n"); printf(" 1 -> Advance cursor %d nodes\n", numberToMove); printf(" 2 -> Regress cursor %d nodes\n", numberToMove); while (1) { printf("Enter Selection: "); fgets(buffer, sizeof(buffer), stdin); sscanf(buffer, "%d", &selection); switch(selection) { case 1: return(advanceCursor(numberToMove, dataList)); case 2: return(regressCursor(numberToMove, dataList)); default: printf("Invalid Option - Reenter Selection\n"); } } } /***************************************************************************************************** * Purpose: to test the concatenateLists function of the linked lists. The second list is read from * * a file * * Parameters: list *dataList1: the list to which the list read from a file should be added * * Postconditions: dataList has the elements read from a file added to it * * Returns: 0 following successful operation * *****************************************************************************************************/ int testConcatenateList(list *dataList1) { list *newList; newList = createList(); readList("list.txt", newList); concatenateLists(dataList1, newList); return(0); } /***************************************************************************************************** * Purpose: to test the list reading function. If list given is empty, a list is automatically read * * from file. Otherwise, user can choose to append to list or cancel read * * Parameters: list *dataList: the list on which to test read list function * * Postconditions: the list is read is indicated by the usre (see above) * * Returns: 0 following successful operation * *****************************************************************************************************/ int testReadFile(list *dataList) { char buffer[MAXLINE]; int selection; /* SO FAR INPUTTING THE FILE NAME HAS NOT WORKED!!! Comment it out!!! printf("Please enter the name of the file containing the list: "); fgets(buffer, sizeof(buffer), stdin); */ switch (readList("list.txt", dataList)) { case 0: printf("%d elements were successfully read from the list\n", dataList->length); break; case 1: printf("The list is not empty. Select option:\n"); printf(" 1 -> Concatenate List in file to Existing List\n"); printf(" 2 -> Leave List as it is\n"); printf(" Enter Selection: "); fgets(buffer, sizeof(buffer), stdin); sscanf(buffer, "%d", &selection); switch(selection) { case 1: return(testConcatenateList(dataList)); default: break; } break; case 2: printf("That file could not be opened. Select a valid file name.\n"); break; } return(0); } /***************************************************************************************************** * Purpose: to test the file writing function implemented with linked lists. * * Parameters: list *dataList: the list to be written to file * * Postconditions: "listout.txt" contains the contents of dataList * * Returns: 0 following successful operation * *****************************************************************************************************/ int testFileOutput(list *dataList) { FILE *outputFile; outputFile = fopen("listout.txt", "w"); fdisplayList(outputFile, dataList); close(outputFile); return(0); } /* The main program - provides the user with a list of options to test the functions provided for manipulating linked lists. Options include adding elements, deleting an element, moving the cursor, sorting the list, clearing the list, reading a list from a file, writing the list to a file, and exiting */ main(int argc, char *argv[]) { int selection; list *mainList; char buf[MAXLINE]; mainList = createList(); while(1){ /* display the current list */ displayList(mainList); /* display the menu of options */ printf("Select an Operation to Perform on this List:\n"); printf(" 1 -> Add an Element to the List\n"); printf(" 2 -> Delete Cursor Node\n"); printf(" 3 -> Move Cursor\n"); printf(" 4 -> Sort List Using Quicksort\n"); printf(" 5 -> Clear List\n"); printf(" 6 -> Read List From File 'List.txt'\n"); printf(" 7 -> Write List to File 'Listout.txt'\n"); printf(" 8 -> Exit List Tester\n"); printf("Enter Selection: "); fgets(buf, sizeof(buf), stdin); sscanf(buf, "%d", &selection); /* execute the appropriate option */ switch(selection) { case 1: testAdd(mainList); break; case 2: deleteCursorNode(mainList); break; case 3: testMoveCursor(mainList); break; case 4: quicksort(mainList); break; case 5: clearList(mainList); break; case 6: testReadFile(mainList); break; case 7: testFileOutput(mainList); break; case 8: deleteList(mainList); exit(0); default: printf("Invalid Option\n"); break; } } }