/* fun_with_lists.c * Authors: Pete Broadwell * Josh Vickery * This program serves as the front-end interactive interface to the * list manipulation library. It allows the user to read a list from * a file and then perform a wide variety of operations on that list. * Attempting to run these operations before a list has been loaded * results in error messages */ #include "ll_defs.h" int main(int argc, char *argv[]) { llist *clist=NULL; /* pointer to the current list */ char c; int quit=0; char filename[256]; FILE *fd; int count; printf("\nWelcome to Josh and Pete's list manipulator!\n"); while (quit==0) { /* print the main menu */ printf("\nOperations:\n"); printf("r Read a list from a file\n"); printf("w Write the current list to a file\n"); printf("p Print the current list to the screen\n"); printf("s Sort the current list using quicksort\n"); printf("a Advance the cursor in the list\n"); printf("b Back up the cursor in the list\n"); printf("d Delete the current element in the list\n"); printf("f Move the cursor to the front of the list\n"); printf("e Move the cursor to the end of the list\n"); printf("c Count the number of nodes on the list\n"); printf("q Quit Josh and Pete's list manipulator\n\n"); printf("What would you like to do? "); scanf("%c", &c); /* I'm not sure why this is necessary, but otherwise it reads the * newline as a new character. */ while(c == '\n') scanf("%c", &c); switch(c) { case 'r': /* read a list from the given file and return a pointer to the list */ printf("Name of file: "); scanf("%s", filename); /* if a list is currently in use, deallocate that list */ if (clist != NULL){ printf("\nDELETING CURRENT LIST.\n"); delete_l(clist); } /* read the new list from the file */ clist=(llist *)readfile(filename); printf("\nLIST CREATED.\n"); break; case 'w': /* write the list to the given file */ printf("Name of file: "); scanf("%s", filename); fd=fopen(filename, "w"); if (fd == NULL) { printf("Unacceptable filename.\n"); break; } writefile(fd, clist); printf("\nFILE CREATED.\n"); fclose(fd); break; case 'p': /* print the current list to the screen (stdout) */ printf("\n"); printf("LIST CONTENTS:\n"); writefile(stdout, clist); break; case 's': /* sort the list */ clist=(llist *)quicksort(clist); printf("\nLIST SORTED.\n"); break; case 'a': /* advance the cursor one element, if possible */ if (clist==NULL){ printf("List is currently empty. Nothing done.\n"); break; } printf("\nADVANCING CURSOR\n"); clist->current=(node *)advance_cursor(clist); break; case 'b': /* move the cursor backwards one element, if possible */ if (clist==NULL){ printf("List is currently empty. Nothing done.\n"); break; } printf("\nBACKING UP CURSOR\n"); clist->current=(node *)backup_cursor(clist); break; case 'd': /* delete the current element from the list and reset the cursor */ if (clist==NULL){ printf("List is currently empty. Nothing changed.\n"); break; } delete_element(clist); printf("\nELEMENT DELETED.\n"); clist->current=(node *)tofront(clist); printf("Cursor reset to front of list.\n"); break; case 'f': /* move the cursor to the front of the list */ if (clist==NULL){ printf("List is currently empty. Nothing done.\n"); break; } clist->current=(node *)tofront(clist); printf("\nCURSOR IS AT FRONT OF LIST.\n"); break; case 'e': /* move the cursor to the end of the list */ if (clist==NULL){ printf("List is currently empty. Nothing done.\n"); break; } clist->current=(node *)toend(clist); printf("\nCURSOR IS AT END OF LIST.\n"); break; case 'c': count=count_nodes(clist); printf("\nTHE LIST HAS %d ELEMENTS.\n", count); printf("Cursor reset to front of list.\n"); break; case 'q': /* prepare to exit */ quit=1; break; default: break; } } printf("\nThank you for using Josh and Pete's list manipulator!\n"); }