/* listtester.c * Chris Kern * * The way-too-elaborate interactive test program for linked lists. Hit 'h' for help. */ #include #include #include #include #include "linklist.h" #define MAX_BUFFER 200 /* The global list. */ struct list *list; /* The current number of nodes. */ int number; /* A buffer, and the pointer to the buffer. */ char buf[MAX_BUFFER]; char *buffer = buf; /* A helper function to print the current element of the list, and the number of nodes. */ void printinfo() { printf("Current number of nodes: %i\n", number); if (!list->cursor->item) printf("The current node does not have any contents.\n"); else printf("The value of the current node is \"%s\"\n", list->cursor->item); } /* printinfo() */ /* A helper function to clear the buffer. */ void clearbuf() { /* Set all the buffer elements to 0. */ /* WARNING: bzero is not an ANSI function. */ bzero(buf, MAX_BUFFER); } /* clearbuf() */ /* Main function */ int main() { /* Variable declarations. */ int command; int test; struct node *temp; /* Print introductory material */ printf("-----------------------\n"); printf("Linked List program\n"); printf("v1.0 Chris Kern\n"); printf("Type 'h' for help.\n"); printf("-----------------------\n\n"); /* Create the new list. */ list = CreateList(""); number = 1; /* Print the current item. */ printinfo(); /* This main loop will continue until "q" is pressed. */ while (1) { printf("\nCommand: "); /* Read characters until the newlines are cleared. */ while ((command = getchar()) == '\n'); /* Parse input. */ switch(command) { case 'b': /* Add a node to the front (beginning) */ /* Accept input for the contents of the node. */ clearbuf(); printf("\nType the contents for the node, which will be added at the front.\n"); scanf("%s", &buf); /* Attempt to add the item. */ test = AddToFront(buf, list); /* If it fails, print a message. In either case, go back and get another character. */ if (!test) { printf("Add failed: Memory allocation fault.\n"); } /* if */ /* Increment counter. */ if (test) number++; break; case 'c': /* Set current value */ printinfo(); /* Accept input for the new contents. */ clearbuf(); printf("\nType the new contents for this node.\n"); scanf("%s", &buf); /* Attempt to change the value. */ test = SetValue(buf, list); /* If it fails, print a message. */ if (!test) { printf("Set failed: Memory allocation fault.\n"); } /* if */ break; case 'd': /* Delete current element */ /* You cannot delete the only element of a list. */ if (number == 1) { printf("\nYou cannot delete the only element in a list.\n"); break; } else { DeleteCurrentElement(list); number--; printinfo(); } break; case 'e': /* Add a node to the end. */ /* Accept input for the contents of the node. */ clearbuf(); printf("\nType the contents for the node, which will be added at the end.\n"); scanf("%s",&buf); /* Attempt to add the item. */ test = AddToEnd(buf, list); /* If it fails, print a message. In either case, go back and get another character. */ if (!test) { printf("Add failed: Memory allocation fault.\n"); } /* if */ /* If the add succeeds, increment the counter. */ if (test) number++; break; case 'f': /* Find an element, move the cursor. */ /* Accept input. */ clearbuf(); printf("\nType the string you wish to search for.\n"); scanf("%s",&buf); /* Try to find the element. */ test = FindElement(buf, list); /* If the find failed, print a message. */ if (!test) { printf("Find failed: Element not in list.\n"); } /* if */ break; case 'g': /* Get current value. */ if (!list->cursor->item) printf("\nThe current node does not have any contents.\n"); else printf("The value of the current node is \"%s\"\n", list->cursor->item); break; case 'h': /* Help. */ printf("\nThis program supports the following commands:\n"); printf("(b) Add an item to the beginning of the list.\n"); printf("(c) Change the value of the current item.\n"); printf("(d) Delete the current element of the list.\n"); printf("(e) Add an item to the end of the list.\n"); printf("(f) Search for an element and move the cursor there.\n"); printf("(g) Print the value at the current item.\n"); printf("(h) Help.\n"); printf("(i) Print the current item value and the number of nodes in the list.\n"); printf("(j) Advance the cursor forward.\n"); printf("(k) Move the cursor backwards.\n"); printf("(l) Load a list from a file.\n"); printf("(p) Print the whole list to the screen.\n"); printf("(q) Quit.\n"); printf("(r) Reset the cursor to the first element of the list.\n"); printf("(s) Sort the list in alphabetical order.\n"); printf("(w) Write the list to a file.\n\n"); break; case 'i': /* Info */ printinfo(); break; case 'k': /* Advance the cursor. */ /* Attempt to advance the cursor. */ test = Advance(list); /* If it fails, print an error message. Otherwise, print the new value. */ if (!test) { printf("\nYou are at the end of the list!\n"); } /* if */ if (test) { if (!list->cursor->item) printf("\nThe current node does not have any contents.\n"); else printf("\nThe value of the current node is \"%s\"\n", list->cursor->item); } /* if */ break; case 'j': /* Move the cursor back. */ /* Attempt to move the cursor back. */ test = GoBack(list); /* If it fails, print an error message. Otherwise, print the new value. */ if (!test) { printf("\nYou are at the beginning of the list!\n"); } /* if */ if (test) { if (!list->cursor->item) printf("\nThe current node does not have any contents.\n"); else printf("\nThe value of the current node is \"%s\"\n", list->cursor->item); } /* if */ break; case 'l': /* Load from a file. */ clearbuf(); printf("\nEnter the file to load.\n"); printf("WARNING: If you type in a filename that does not exist, it will crash.\n"); scanf("%s30", buf); /* Make sure that file is a valid linked list file. */ /* Free all the memory. */ for (temp=list->first; temp->next; temp=temp->next) { free(temp->item); } free(list); /* Call the function. */ list = LoadList(buf); /* Set number */ number = 0; for (temp = list->first; temp->next; temp=temp->next) number++; /* It stops before it should, so add 1. */ number++; printinfo(); break; case 'p': /* Print the whole list. */ WriteList(list); break; case 'q': /* Quit. */ printf("\nGoodbye!\n"); exit(0); case 'r': /* Reset the cursor. */ Reset(list); break; case 's': /* Sort the list. */ list = quicksort(list); WriteList(list); break; case 'w': /* Save list. */ clearbuf(); printf("\nEnter a filename."); scanf("%s30",buf); SaveList(buf, list); break; } /* switch(command) */ } /* while */ exit(0); } /* main */