#include #include #include #include "list.h" /* Tester for implementing double-linked lists. * Authors: Dmitry Krivin * Oleksiy Andriychenko * Prashant Paroda * D. Hawando * * Dear Sam, * * In order to get full credit for this assignment, * we have implemented a lot of different and interesting * methods for operating the lists, including all the basic * methods (adding, deleting, displaying, length), * iterating the list, sorting, searching and appending * the lists, and even duplicating lists. In addition, our quicksort * pics random pivot and takes a function for comparison as a * parameter. Also, we have used wrapper structure to protect * users from directly messing up with implementation. * * Have fun reading this. Hope you'll enjoy our program. * * Sincerely, * * - Authors. */ void printMenu(); void printMenu() { /* The menu */ printf ("Please refer to the menu and select an action.\n"); printf ("1. Print out the list\n"); printf ("2. Print out the length of the list\n"); printf ("3. Print out the current entry of the list.\n"); printf ("4. Add an element at the front of the list.\n"); printf ("5. Add an element at the end of the list.\n"); printf ("6. Insert an element after the cursor.\n"); printf ("7. Delete the first element of the list.\n"); printf ("8. Delete the last element of the list.\n"); printf ("9. Delete the current element.\n"); printf ("10. Write the list to a file.\n"); printf ("11. Sort the list.\n"); printf ("12. Find the string in the list.\n"); printf ("13. Move the cursor to the head of the list.\n"); printf ("14. Move the cursor to the end of the list.\n"); printf ("15. Advance the cursor.\n"); printf ("16. Retreat the cursor.\n"); printf ("17. Make a copy of the list.\n"); printf ("18. Read a list from the file. \n"); printf ("19. Change the current node. \n"); printf ("20. Quit the Tester.\n"); printf ("Enter your choice here:\n"); } main () { int n; /* the intitial length of the list*/ int choice; /* exactly what the name suggests. */ int i; char oleksiy[20]; char *temp; Listptr newlist,newlist2; printf ("Hello, my name is PP. \nI will assist you through this testing program.\n"); printf ("First you need to create a list of strings.\nPlease follow the following instructions:\n"); printf ("Enter the number the of the strings that you want to put into the \nlist to start with:\n"); scanf ("%d", &n); printf ("Please enter the strings in the order that you want\nthem to appear in the array, starting with the first element.\n"); newlist=createlist(); /* Reading in the strings and putting them into the list. The first string read will be the first element of the list, and so forth.*/ for (i = 1; i <= n; i++) { printf ("Please enter string number %d : ", i); scanf ("%s", oleksiy); addtoend(newlist, oleksiy); printf("\n"); } printf ("The list has been created.\n"); while (choice != 20) { printMenu(); scanf ("%d", &choice); printf("\n"); switch (choice) { case 1: writetoscreen(newlist); break; case 2: printf ("The lenght of the list is %d \n", length(newlist)); break; case 3: temp = getcurrent(newlist); /* getcurrent returns pointer to a copy of current element content */ if (temp==NULL) printf("List is probably empty \n"); else { printf ("%s\n", temp); free(temp); } break; case 4: printf("Please enter the string to insert at the "); printf("front of the list:\n"); scanf("%s", oleksiy); addtofront(newlist, oleksiy); break; case 5: printf("Please enter the string to insert at end of the list:\n"); scanf("%s", oleksiy); addtoend(newlist, oleksiy); break; case 6: printf ("Please enter the string to insert after the cursor:\n"); scanf ("%s", oleksiy); addaftercursor(newlist, oleksiy); break; case 7: if (deletehead(newlist)== 0) printf ("Delete has failed.\n"); break; case 8: if (deletetail(newlist)== 0) printf ("Delete has failed.\n"); break; case 9: if (deletecurrent(newlist)== 0) printf ("Delete has failed.\n"); break; case 10: printf ("Please enter the file name of the output file:\n"); scanf ("%s", oleksiy); if (writetofile (newlist, oleksiy)==0) printf ("Failed to write to file.\n"); else printf("The list was successfully copied to file %s.\n", oleksiy); break; case 11: quicksort (newlist,(int (*)(char*, char*))strcmp); writetoscreen(newlist); break; case 12: printf ("Please enter the string to look for.\n"); resettohead(newlist); scanf ("%s", oleksiy); if (find(newlist,oleksiy)==1) { printf("The string was found, and the cursor is now "); printf("pointing\nto the element which contains the string.\n"); } else printf ("The string was not found in the list.\n"); break; case 13: resettohead(newlist); printf("The cursor now is at the head of the list.\n"); break; case 14: resettotail(newlist); printf("The cursor now is at the tail of the list.\n"); break; case 15: if (advance(newlist)==0) { printf("The cursor is already at the end (or list is empty)"); printf(",\nunable to advance any further.\n"); } break; case 16: if (retreat(newlist)==0){ printf("The cursor is already at the beginning (or list is empty)"); printf(",\nunable to retreat any further.\n"); } break; case 17: newlist2 = duplicate(newlist); printf ("Here is the copy of the list:\n"); writetoscreen(newlist2); free(newlist2); break; case 18: destroylist(newlist); /* first erase old list */ printf("enter the file name: "); scanf("%s", oleksiy); newlist=readfromfile(oleksiy); /* empty list if error */ writetoscreen(newlist); break; case 19: printf("Enter new content of current node: "); scanf("%s",oleksiy); if (changecurrent(newlist,oleksiy)==0) printf("Failed. List is probaly empty.\n"); else printf("Current node is changed\n"); break; case 20: printf ("Thank you for using the ListTester.\n"); exit(1); default: printf ("Please enter a number between 1 and 20.\n"); } } }/*main*/