#include #include #include #include "llistheader.h" #define MAX_LINE 245 /* printCommands is a simple function which prints out a list of commands * using printf to show the testers which commands are avaliable to them. */ void printCommands() { printf("\n"); printf("Please enter one of the following commands...\n"); printf("1 : Create new list\n"); printf("2 : Add text to the list\n"); printf("3 : Delete text from the list\n"); printf("4 : Sort the list\n"); printf("5 : Create a list from lines of text in a file\n"); printf("6 : Excise a node\n"); printf("7 : Print out your list!\n"); printf("8 : Help (displays these commands)\n"); printf("9 : Write the list to a file\n"); printf("10 : Find an item\n"); printf("11 : Quit this application\n"); printf("------------------\n"); }/*printCommands()*/ /* The purpose of the main is to allow the tester to run through a series of tests, * which are defined from 1 to 11. As a precondition, one should never enter * characters, just numbers. Main will either print out the requested data or * create new files and linked lists. */ int main() { node *initialnode; node *tempnode; char buf[MAX_LINE]; char *newbuf; char *filename; char *newfile; char tempstring[200]; int choice = 0; /* Clear the buffer. */ bzero(buf, sizeof(buf)); /* printing the commands the first time through provides the tester with a set * of commands. */ printCommands(); /* The primary while loop continues stays in scanf unless something other than an * integer is entered. If an integer is entered then that number is compared with * each of the switch cases. */ while (scanf("%d", &choice)) { /* Determing which, if any, of the cases to execute.*/ switch (choice) { /**********************************************************************/ /* Just a simple test to see if the user entered zero, in which case an * error message is printed. */ case 0: fputs("Incorrect input: please pick one of the numbers given.\n", stdout); printCommands(); fputs("Type 8 for help.\n", stdout); break; /**********************************************************************/ /* Case 1 or option 1 is responsible for creating a new linked list. The tester * just has to enter text after he is prompted. If a list already exists * the tester will get an error message. Each line of text entered after the * prompt makes a new node.*/ case 1: /*Checks to see if there already exists a list.*/ if(initialnode != NULL) { fputs("A list has already been created.\n", stdout); fputs("Type 8 for help.\n", stdout); break; } /*Creates the initial node and the temporary node.*/ initialnode = createNode(tempstring); tempnode = initialnode; bzero(tempstring, sizeof tempstring); /*Instructions for the user.*/ fputs("Creating a new list..\n", stdout); fputs("Each word you enter will be added to the list.\n", stdout); fputs("To finish creating your list type: \"!end\"\n", stdout); fputs("Please enter some text:\n", stdout); /* Get the whole line after a return is entered and creates a new node * containing the text entered. */ while (fgets(buf, sizeof buf, stdin)) { /* Gets rid of the "\n" character at the end of the string.*/ strncpy(tempstring, buf, strlen(buf)-1); /* Allows the user to exit create list. This is done with a !end.*/ if(strcmp(tempstring, "!end") == 0) { fputs("Finished creating list.\n", stdout); fputs("Type 8 for help.\n", stdout); bzero(tempstring, sizeof tempstring); break; } /* If the user doesn't do any of the above then nodes with the text * writtern are created. */ else { tempnode = addNode(tempnode, tempstring); bzero(tempstring, sizeof tempstring); } }/*while(scanf())*/ /* Just making sure everything is cleaned up.*/ bzero(tempstring, sizeof tempstring); tempnode = initialnode; break; /**********************************************************************/ /* Case 2 or option 2 allows the user to add text to a list that already exists. * If the list does not exist then it will return an error message. Again, * each line of text entered after the prompt is a new node.*/ case 2: /* Check to see if no lists exists. If one doesn't then an error message * is printed. */ if(initialnode == NULL) { fputs("No list to add to.\n", stdout); fputs("Type 8 for help.\n\n", stdout); break; } /* Instructions for the user. */ fputs("Each word you enter will be added to the list.\n", stdout); fputs("To finish adding type: \"!end\"\n", stdout); fputs("Please enter some text to add to the list.\n\n", stdout); /* A loop which reads in a line of text from stdin and writes the text to a * node added to the initial node. */ while (fgets(buf, sizeof buf, stdin)) { /* Chops off the "\n" character at the end of the string. */ strncpy(tempstring, buf, strlen(buf)-1); /* A way for the user to finish adding nodes.*/ if(strcmp(tempstring, "!end") == 0) { fputs("Finished adding text.\n", stdout); fputs("Type 8 for help.\n\n", stdout); bzero(tempstring, sizeof tempstring); break; } /* If none of the above conditions are met, then the text, in the form of a * node is added onto the linked list. */ else { tempnode = addNode(getLast(tempnode), tempstring); bzero(tempstring, sizeof tempstring); } }/*while(scanf())*/ /* Just making sure everything is cleaned up.*/ bzero(tempstring, sizeof tempstring); tempnode = initialnode; break; /**********************************************************************/ /* Case 3 or option 3 will delete text from the list. The text must match the * contents of a node. Delete will not delete text from part of a node. */ case 3: /* If a list does not exist, then an error message is printed.*/ if(initialnode == NULL) { fputs("No elments to delete\n", stdout); fputs("Type 8 for help.\n\n", stdout); break; } else{ fputs("Please enter text to delete.\n", stdout); scanf("%s", buf); if(tempnode = (node *)findNode(buf, initialnode)){ deleteNode(tempnode, initialnode); fputs("Deleted the node.\n", stdout); fputs("Type 8 for help.\n\n", stdout); tempnode = initialnode; break; } else{ fputs("Didn't find the text, couldn't delete.\n", stdout); fputs("Type 8 for help.\n\n", stdout); tempnode = initialnode; break; } } /**********************************************************************/ /* Case 4 or option 4 will use quicksort to sort the elements of the * linked list. If the linked list does not exist, then an error message is * returned. */ case 4: if(initialnode == NULL) { fputs("There is not a list to sort\n", stdout); fputs("Type 8 for help.\n\n", stdout); break; } else{ initialnode = quicksort(initialnode); fputs("Your list has been sorted.\n",stdout); fputs("Type 8 for help.\n\n", stdout); tempnode = initialnode; break; } /**********************************************************************/ /* Case 5 or option allows the user to use a file which exists and make each * of it's line an element in a linked list. */ case 5: fputs("Please enter a filename to read the text from.\n", stdout); scanf("%s", buf); filename = buf; initialnode = readListFromFile(filename); tempnode = initialnode; fputs("The file has been read.\n", stdout); fputs("Type 8 for help.\n\n",stdout); break; /**********************************************************************/ /* Case 6 or option 6 will excise a node from a given linked list. First it * excises the node then it returns the node excised and the modified list. */ case 6: fputs("Please enter a node to excise: \n", stdout); scanf("%s", buf); tempnode = findNode(buf, initialnode); fputs("Excising the node:\n", stdout); printLinkedList(exciseNode(tempnode)); fputs("The remaining list:\n", stdout); printLinkedList(initialnode); tempnode = initialnode; fputs("Type 8 for help.\n\n", stdout); break; /**********************************************************************/ /* Case 7 or option 7 just prints a linked list. If there has been no list * created then an error message is printed. */ case 7: if(initialnode == NULL) { fputs("You have not created a list.\n", stdout); fputs("Type 8 for help.\n\n", stdout); break; } else{ printLinkedList(initialnode); fputs("Type 8 for help.\n\n", stdout); tempnode = initialnode; break; } /**********************************************************************/ /* Case 8 or option 8 calls printCommands, allowing the user to see a list * of commands. */ case 8: printCommands(); break; /**********************************************************************/ /* Case 9 or option 9 will write the list that the initial node points to, to a * a file that the user specifies. */ case 9: if(initialnode == NULL) { fputs("No data to write to file.\n", stdout); fputs("Type 8 for help.\n\n", stdout); break; } /* Waits for the filename. If the file already exists then the file * is just replaced. */ fputs("Please enter a filename:\n", stdout); scanf("%s", buf); newfile = buf; writeListToFile(newfile, initialnode); tempnode = initialnode; fputs("Wrote to file.\n", stdout); fputs("Type 8 for help.\n\n", stdout); break; /**********************************************************************/ /* Case 10 or option 10 does a search through the linked list. If there does * not exist a linked list then an error message is returned. */ case 10: if(initialnode == NULL) { fputs("No data to search from.\n", stdout); fputs("Type 8 for help.\n\n", stdout); break; } /* Asks the user for text to search for. Only text which matches the contents * of a node will make a succesful match. Otherwise, a message saying the * element can not be found will be returned. */ else{ fputs("Please enter text to search for.\n", stdout); scanf("%s", buf); if(tempnode = (node *)findNode(buf, initialnode)){ fputs("Found the node.\n", stdout); printLinkedList(tempnode); fputs("Type 8 for help.\n\n", stdout); tempnode = initialnode; break; } else{ fputs("Didn't find the text.\n", stdout); fputs("Type 8 for help.\n\n", stdout); tempnode = initialnode; break; } } /**********************************************************************/ /* Case 11 or option 11 allows the user to exit the program.*/ case 11: printf("Thanks for playing.\n"); exit(0); /**********************************************************************/ /* If the user enters a numbe which is not valid then the default is * executed. */ default: printf("Please enter an appropriate command.\n"); printCommands(); break; }/*switch*/ }/*while*/ }/*main*/