/** * An Interactive tester for using the doubly linked list library * written by Brian K. Smith and Erik Hanson. * @author Brian K. Smith and Erik Hanson * @version 1.0 of Feb. 18, 2000 */ #include "doublylinkedlist.h" #include #include //Here all of teh functions in this file are declared. int print_menu(); int print_create_a_new_list(); int print_view_all_lists(); int print_view_a_list(); int print_delete_a_list(); int print_add_element_to_list(); int print_remove_element_from_list(); int print_quick_sort_list(); int print_concatenate_lists(); int concatenate1list(); int concatenate2lists(); int concatenate3lists(); int concatenate4lists(); int concatenate5lists(); int concatenate6lists(); int concatenate7lists(); int concatenate8lists(); int concatenate9lists(); int print_read_a_list(); int print_write_a_list(); //Global Variables char user_input[256]; int num; struct dllnode *lists[9]; int number_of_lists; /** * This function runs our tester. * @return This function will return 0 upon completion. */ int main() { number_of_lists = 0; printf("Welcome to Erik and Brian's interactive doubly linked list tester.\n"); //Here the menu for the user to use is displaied. print_menu(); while(scanf("%s", user_input)) { num = (int) *user_input; num = tolower(num); //This switch statement calls different functions depending on the user's input. switch(num) { case 'c': { print_create_a_new_list(); print_menu(); break; } case 'v': { print_view_all_lists(); print_menu(); break; } case 'l': { print_view_a_list(); print_menu(); break; } case 'd': { print_delete_a_list(); print_menu(); break; } case 'a': { print_add_element_to_list(); print_menu(); break; } case 'r': { print_remove_element_from_list(); print_menu(); break; } case 's': { print_quick_sort_list(); print_menu(); break; } case 'j': { print_concatenate_lists(); print_menu(); break; } case 'm': { print_menu(); break; } case 'q': { exit(0); } case 'w': { print_write_a_list(); print_menu(); break; } case 'f': { print_read_a_list(); print_menu(); break; } default: { printf("%d is not a supported option. Please type the number for the option you wish to select or m to see the menu again.\n", num); break; } } } return 0; } /** * This function prints the menu for the user to interact with. * @return This function will return 0 upon completion. */ int print_menu() { printf("Type the letter of the option you wish to use, then press enter.\nYour options today are:\nC [C]reate a new list.\nV [V]iew all current lists.\nL View a particular [L]ist.\nD [D]elete a list.\nA [A]dd an element to a list.\nR [R]emove an element from a list.\nS [S]ort a list, using our handy quicksort function!\nJ Concatenate ([J]oin) some lists.\nW [W]rite a list to a file.\nF Read a list from a previously created [F]ile\nQ [Q]uit.\n"); return 0; } /** * This function prompts the user for input and then takes that input and uses it to create * a new doubly linked list by repeatidly callin the add_element_to_list function. * @return This function returns 0 upon completion. */ int print_create_a_new_list() { int i; char *temp; //If the user has created the maximum number of lists, they will be notified. if(number_of_lists > 8) { printf("Maximum number of lists supported by this program reached.\nPlease remove a list before you try to create another list.\n"); return 0; } //Prompt the user for input printf("How many elements in your list?\n"); scanf("%d", &num); for(i = 0; i < num; i++) { printf("Please enter the data for element number %d:\n", i); scanf("%s", user_input); //If this is the first element in the list, create a new list. if(i == 0) { lists[number_of_lists] = (struct dllnode *) malloc(sizeof(struct dllnode)); lists[number_of_lists] = make_new_node(); lists[number_of_lists]->data = (char *) malloc(MAX_DATA_LENGTH); strcpy(lists[number_of_lists]->data, user_input); number_of_lists++; }//if //If the list has been created, add to it. else { add_element_to_list(lists[number_of_lists-1], user_input); }//else } printf("Thanks for making a list!\n"); //Reset the user_input string. memset(user_input, '\0', strlen(user_input)); return 0; } /** * This function displays all of the current lists. * @return This function returns 0 upon completion. */ int print_view_all_lists() { int i; //For each list, print out its data. for(i = 0; i < number_of_lists; i++) { printf("List Number: %d\n", i); write_list_to_file(stdout, lists[i]); } printf("\n"); return 0; } /** * This function displays a particulat list, chosen by the user. * @return This function returns 0 upon completion. */ int print_view_a_list() { int i; i = 999999; while(i > number_of_lists) { //Prompt the user to choose the list they want to vies. printf("There are %d lists. Which one would you like to view?\n", number_of_lists); scanf("%d", &i); i--; } //Display that list to the screen. write_list_to_file(stdout, lists[i]); return 0; } /** * This function deletes an entire list which is chosen by the user. * @return This function returns 0 upon completion. */ int print_delete_a_list() { int i; int n; i = 999999; while(i > number_of_lists) { //Prompt the user to choose a list to delete. printf("There are %d lists. Which one would you like to delete?\n", number_of_lists); scanf("%d", &i); i--; } //Delete the given list. delete_list(lists[i]); //If the deleted list is the last list, subtract 1 from number_of_lists. if(i == number_of_lists - 1) { number_of_lists--; } //If the deleted list is the first list adjust the list to compensate //and subtract 1 from number_of_lists. else if(i == 0) { n = 0; while(n < number_of_lists) { lists[n] = lists[++n]; } lists[n] = NULL; number_of_lists--; } //If the deleted list is in the middle, adjust the list to compensate //and subtract 1 from number_of_lists. else { n = i; while(n < number_of_lists) { lists[n] = lists[++n]; } lists[n] = NULL; number_of_lists--; } return 0; } /** * This function adds an element with data given by the user to a list that the user chooses. * @return This functipon returns 0 upon completion. */ int print_add_element_to_list() { int i; i = 999999; while(i > number_of_lists) { //Prompt the user to choose a list to add an element to. printf("There are %d lists. Which one would you like to add an element to?\n", number_of_lists); scanf("%d", &i); i--; } //Prompt the user to enter a value for the new element and add it. printf("What value would you like your new element to have?\n"); scanf("%s", user_input); add_element_to_list(lists[i], user_input); //Reset the user input string. memset(user_input, '\0', strlen(user_input)); return 0; } /** * This function removes an element chosen by the user from a list chosen by a user. * @return This function returns 0 upon completion. */ int print_remove_element_from_list() { int i,c; struct dllnode *n; i = 999999; while(i > number_of_lists) { //Prompt the user to choose a list to remove an element from. printf("There are %d lists. Which one would you like to remove an element from?\n", number_of_lists); scanf("%d", &i); i--; } n = lists[i]; c = 1; //Print out the current contents of the chosen list for the user to pick an element from. while (has_next_element(n)) { printf("%d: %s\n", c, n->data); n = get_next_element(n); c++; } printf("%d: %s\n", c, n->data); //Prompt the user to pick an element. printf("Enter the number of the element you want to remove.\n"); scanf("%d", &c); //Remove the chosen element from the chosen list. lists[i] = remove_element_from_list(lists[i], --c); return 0; } /** * This function quicksorts a list chosen by the user. * @return This function returns 0 upon completion. */ int print_quick_sort_list() { int i; i = 999999; while(i > number_of_lists) { //Prompt the user to choose a list to quicksort. printf("There are %d lists. Which one would you like to Quicksort?\n", number_of_lists); scanf("%d", &i); i--; } //Set the seed for random number generation for quicksort. srand(time(NULL)); //Quicksort the chosen list. lists[i] = quicksort_list(lists[i]); return 0; } /** * This function concatenates a given number of lists, up to the max supported by the * program. This function is not completely bug free. * @return This function returns 0 upon completion. */ int print_concatenate_lists() { int i; i = 999999; while(i > number_of_lists) { //Prompt the user to enter the number of lists they want to concatonate. printf("There are %d lists. How many do you wish to concatenate?\n", number_of_lists); scanf("%d", &i); } //Choose a concatenation function depending on how many lists are to be joined. switch(i) { case 1: { concatenate1list(); break; } case 2: { concatenate2lists(); break; } case 3: { concatenate3lists(); break; } case 4: { concatenate4lists(); break; } case 5: { concatenate5lists(); break; } case 6: { concatenate6lists(); break; } case 7: { concatenate7lists(); break; } case 8: { concatenate8lists(); break; } case 9: { concatenate9lists(); break; } default: { printf("There are no more than 10 lists and no fewer than 1 lists.\n"); break; } } return 0; } /** * The following functions all concatenate a given number of lists. For body comments see the * concatenate2lists function. * @return All of these functions return 0 upon compeletion. */ int concatenate1list() { int i; i = 999999; while(i > number_of_lists) { printf("There are %d lists.\nWhat list do you want to concatenate?\n(Concatenating one list to itself is sort of silly.)\n", number_of_lists); scanf("%d", &i); i--; printf("The concatenation will be placed in the spot occupied by the first list.\n"); } lists[i] = concatenate_lists(1, lists[i]); return 0; } int concatenate2lists(){ int i, count, n, num2cat; int con[2]; num2cat = 2; i = 999999; printf("The concatenation will be placed in the spot occupied by the first list.\n"); for(count = 0; count < num2cat; count++) { while(i > number_of_lists) { //Prompt the user to choose lists to concatenate. printf("There are %d lists.\nWhich list do you want to concatenate?\n(You will get this prompt %d times.)\n", number_of_lists, num2cat); scanf("%d", &i); i--; con[count] = i; } i = 99999999; } lists[con[0]] = concatenate_lists(num2cat, lists[con[0]], lists[con[1]]); //Here the array of lists is repacked to eliminate holes. for(i = 1; i < num2cat; i++) { lists[con[i]] = NULL; if(con[i] == number_of_lists - 1) { number_of_lists--; } else if(con[i] == 0) { n = 0; while(n < number_of_lists) { lists[n] = lists[++n]; } lists[n] = NULL; number_of_lists--; } else { n = con[i]; while(n < number_of_lists) { lists[n] = lists[++n]; } lists[n] = NULL; number_of_lists--; } } return 0; } int concatenate3lists(){ int i, count, n, num2cat; int con[3]; num2cat = 3; i = 999999; printf("The concatenation will be placed in the spot occupied by the first list.\n"); for(count = 0; count < num2cat; count++) { while(i > number_of_lists) { printf("There are %d lists.\nWhich list do you want to concatenate?\n(You will get this prompt %d times.)\n", number_of_lists, num2cat); scanf("%d", &i); i--; con[count] = i; } i = 99999999; } lists[con[0]] = concatenate_lists(num2cat, lists[con[0]], lists[con[1]], lists[con[2]]); for(i = 1; i < num2cat; i++) { lists[con[i]] = NULL; if(con[i] == number_of_lists - 1) { number_of_lists--; } else if(con[i] == 0) { n = 0; while(n < number_of_lists) { lists[n] = lists[++n]; } lists[n] = NULL; number_of_lists--; } else { n = con[i]; while(n < number_of_lists) { lists[n] = lists[++n]; } lists[n] = NULL; number_of_lists--; } } return 0; } int concatenate4lists(){ int i, count, n, num2cat; int con[4]; num2cat = 4; i = 999999; printf("The concatenation will be placed in the spot occupied by the first list.\n"); for(count = 0; count < num2cat; count++) { while(i > number_of_lists) { printf("There are %d lists.\nWhich list do you want to concatenate?\n(You will get this prompt %d times.)\n", number_of_lists, num2cat); scanf("%d", &i); i--; con[count] = i; } i = 99999999; } lists[con[0]] = concatenate_lists(num2cat, lists[con[0]], lists[con[1]], lists[con[2]], lists[con[3]]); for(i = 1; i < num2cat; i++) { lists[con[i]] = NULL; if(con[i] == number_of_lists - 1) { number_of_lists--; } else if(con[i] == 0) { n = 0; while(n < number_of_lists) { lists[n] = lists[++n]; } lists[n] = NULL; number_of_lists--; } else { n = con[i]; while(n < number_of_lists) { lists[n] = lists[++n]; } lists[n] = NULL; number_of_lists--; } } return 0; } int concatenate5lists(){ int i, count, n, num2cat; int con[5]; num2cat = 5; i = 999999; printf("The concatenation will be placed in the spot occupied by the first list.\n"); for(count = 0; count < num2cat; count++) { while(i > number_of_lists) { printf("There are %d lists.\nWhich list do you want to concatenate?\n(You will get this prompt %d times.)\n", number_of_lists, num2cat); scanf("%d", &i); i--; con[count] = i; } i = 99999999; } lists[con[0]] = concatenate_lists(num2cat, lists[con[0]], lists[con[1]], lists[con[2]], lists[con[3]], lists[con[4]]); for(i = 1; i < num2cat; i++) { lists[con[i]] = NULL; if(con[i] == number_of_lists - 1) { number_of_lists--; } else if(con[i] == 0) { n = 0; while(n < number_of_lists) { lists[n] = lists[++n]; } lists[n] = NULL; number_of_lists--; } else { n = con[i]; while(n < number_of_lists) { lists[n] = lists[++n]; } lists[n] = NULL; number_of_lists--; } } return 0; } int concatenate6lists(){ int i, count, n, num2cat; int con[6]; num2cat = 6; i = 999999; printf("The concatenation will be placed in the spot occupied by the first list.\n"); for(count = 0; count < num2cat; count++) { while(i > number_of_lists) { printf("There are %d lists.\nWhich list do you want to concatenate?\n(You will get this prompt %d times.)\n", number_of_lists, num2cat); scanf("%d", &i); i--; con[count] = i; } i = 99999999; } lists[con[0]] = concatenate_lists(num2cat, lists[con[0]], lists[con[1]], lists[con[2]], lists[con[3]], lists[con[4]], lists[con[5]]); for(i = 1; i < num2cat; i++) { lists[con[i]] = NULL; if(con[i] == number_of_lists - 1) { number_of_lists--; } else if(con[i] == 0) { n = 0; while(n < number_of_lists) { lists[n] = lists[++n]; } lists[n] = NULL; number_of_lists--; } else { n = con[i]; while(n < number_of_lists) { lists[n] = lists[++n]; } lists[n] = NULL; number_of_lists--; } } return 0; } int concatenate7lists(){ int i, count, n, num2cat; int con[7]; num2cat = 7; i = 999999; printf("The concatenation will be placed in the spot occupied by the first list.\n"); for(count = 0; count < num2cat; count++) { while(i > number_of_lists) { printf("There are %d lists.\nWhich list do you want to concatenate?\n(You will get this prompt %d times.)\n", number_of_lists, num2cat); scanf("%d", &i); i--; con[count] = i; } i = 99999999; } lists[con[0]] = concatenate_lists(num2cat, lists[con[0]], lists[con[1]], lists[con[2]], lists[con[3]], lists[con[4]], lists[con[5]], lists[con[6]]); for(i = 1; i < num2cat; i++) { lists[con[i]] = NULL; if(con[i] == number_of_lists - 1) { number_of_lists--; } else if(con[i] == 0) { n = 0; while(n < number_of_lists) { lists[n] = lists[++n]; } lists[n] = NULL; number_of_lists--; } else { n = con[i]; while(n < number_of_lists) { lists[n] = lists[++n]; } lists[n] = NULL; number_of_lists--; } } return 0; } int concatenate8lists(){ int i, count, n, num2cat; int con[8]; num2cat = 8; i = 999999; printf("The concatenation will be placed in the spot occupied by the first list.\n"); for(count = 0; count < num2cat; count++) { while(i > number_of_lists) { printf("There are %d lists.\nWhich list do you want to concatenate?\n(You will get this prompt %d times.)\n", number_of_lists, num2cat); scanf("%d", &i); i--; con[count] = i; } i = 99999999; } lists[con[0]] = concatenate_lists(num2cat, lists[con[0]], lists[con[1]], lists[con[2]], lists[con[3]], lists[con[4]], lists[con[5]], lists[con[6]], lists[con[7]]); for(i = 1; i < num2cat; i++) { lists[con[i]] = NULL; if(con[i] == number_of_lists - 1) { number_of_lists--; } else if(con[i] == 0) { n = 0; while(n < number_of_lists) { lists[n] = lists[++n]; } lists[n] = NULL; number_of_lists--; } else { n = con[i]; while(n < number_of_lists) { lists[n] = lists[++n]; } lists[n] = NULL; number_of_lists--; } } return 0; } int concatenate9lists(){ int i, count, n, num2cat; int con[9]; num2cat = 9; i = 999999; printf("The concatenation will be placed in the spot occupied by the first list.\n"); for(count = 0; count < num2cat; count++) { while(i > number_of_lists) { printf("There are %d lists.\nWhich list do you want to concatenate?\n(You will get this prompt %d times.)\n", number_of_lists, num2cat); scanf("%d", &i); i--; con[count] = i; } i = 99999999; } lists[con[0]] = concatenate_lists(num2cat, lists[con[0]], lists[con[1]], lists[con[2]], lists[con[3]], lists[con[4]], lists[con[5]], lists[con[6]], lists[con[7]], lists[con[8]]); for(i = 1; i < num2cat; i++) { lists[con[i]] = NULL; if(con[i] == number_of_lists - 1) { number_of_lists--; } else if(con[i] == 0) { n = 0; while(n < number_of_lists) { lists[n] = lists[++n]; } lists[n] = NULL; number_of_lists--; } else { n = con[i]; while(n < number_of_lists) { lists[n] = lists[++n]; } lists[n] = NULL; number_of_lists--; } } return 0; } /** * This function writes a list chosen by the user to a file with a name specified by the user. * @return This function returns 0 upon completion. */ int print_write_a_list() { int i; char *filename; FILE *fp; i = 999999; //Alocate memory. filename = (char *) malloc(256); if(!filename) { fprintf(stderr, "Failed to allocate space for the filename when trying to write a list."); exit(1); } while(i > number_of_lists) { //Prompt the user to choose a file to be written to the file. printf("There are %d lists. Which one would you like to print to file?\n", number_of_lists); scanf("%d", &i); i--; } //Prompt the user for a filename. printf("What filename would you like to write this list to?\n"); scanf("%s", filename); //Open the stream with the specified file name. fp = fopen(filename, "w"); //Write the list write_list_to_file(fp, lists[i]); //Close the file. fclose(fp); return 0; } /** * This function reads a list from a file with the name specified by the user. * @return This function returns 0 upon completion. */ int print_read_a_list() { int i; char *filename; FILE *fp; //Allocate memory for the filename filename = (char *) malloc(256); if(!filename) { fprintf(stderr, "Failed to allocate space for the filename when trying to write a list."); exit(1); } //Prompt the user for a filename. printf("What filename would you like to read from disk?\n(The list will be placed in the last list slot.)\n"); scanf("%s", filename); //Open the stream with the file name. fp = fopen(filename, "r"); //Read the file and place it at the end of the array of lists. lists[number_of_lists] = read_list_from_file(fp); //Close the file. fclose(fp); //Inform the program that there is another list in the array of lists. number_of_lists++; return 0; }