/* Definition of data structure and operations for working with linked lists */ /* librarys for standard I/O, strings, and memory allocation */ #include #include #include /* maximum size of an array within a list node */ #define strMax 50 /* declaration of node-related types */ typedef struct Node * listType; typedef struct Node { char data [strMax]; listType next; } listNode; /* pre-condition: list is an initialized list post-condition: a new node is created with str as data and the new node is inserted at the front of previoius list the nodes and order of the initial list are unchanged */ void insertFront (char * str, listType *front) { listType newFront = (listType) malloc (sizeof (listNode)); strncpy (newFront->data, str, strMax-1); // allow space for null at end newFront->next = *front; *front = newFront; } /* pre-condition: list is an initialized list post-condition: data in each node is printed */ void listSimplePrint (listType list){ listType listPtr = list; while (listPtr != NULL) { printf (" \"%s\" ", listPtr->data); listPtr = listPtr->next; } printf ("\n"); } /* pre-condition: list is an initialized list post-condition: data in each node is printed, Scheme-style, within () */ void listPrint (listType list){ listType listPtr = list; char * separator = ""; printf ("("); while (listPtr != NULL) { printf ("%s\"%s\"", separator, listPtr->data); separator = ", "; listPtr = listPtr->next; } printf (")\n"); } /* pre-condition: list is an initialized list post-condition: returns "found" or "not found", depending on whether the given string is the data field for some node on the list */ char * search (char * str, listType list) { listType listPtr = list; while (listPtr != NULL) { if (strcmp (listPtr->data, str) == 0) { return "found"; } listPtr = listPtr->next; } return ("not found"); } void listDelete (listType * list) { /* pre-condition: list is an initialized list post-condition: list is changed to a NULL list with any previously-defined nodes deallocated */ if (*list != NULL) { /* recursively remove the rest of the list nodes */ listDelete (&((*list)->next)); /* deallocate the space for the node itself */ free (*list); /* set list pointer to null list */ *list = NULL; } } /* pre-condition: list is an initialized list post-condition: data in each node are printed in reverse order */ void printReverseBruteForce (listType list) { listType ptr, lastPrinted; lastPrinted = NULL; // print nodes in reverse until first item printed while (lastPrinted != list) { ptr = list; while (ptr->next != lastPrinted) { ptr = ptr->next; } printf (" \"%s\" ", ptr-> data); lastPrinted = ptr; } printf ("\n"); } /* pre-condition: list is an initialized list post-condition: data in each node are printed in reverse order */ void printReverseRecursive (listType list) { if (list != NULL) { printReverseRecursive (list->next); printf ("\"%s\" ", list-> data); } } /* pre-condition: list is an initialized list post-condition: data in each node are printed in reverse order with a list-oriented format, with commas and parentheses */ void printReverseRecursiveFormatted (listType list) { printf ("not yet implemented\n"); } /* simple driver program to test list insertion and versions of printing. */ int main ( ) { printf ("program illustrating list creating and printing procedures\n"); /* declaration of variables */ listType first = NULL; /* Construction of list with 5 nodes */ insertFront ("Node E", &first); insertFront ("Node D", &first); insertFront ("Node C", &first); insertFront ("Node B", &first); insertFront ("Node A", &first); /* printing options */ printf ("Original list -- Simple Print: "); listSimplePrint (first); printf ("Original list -- Formatted Print: "); listPrint (first); /* searching options */ printf ("searching for Node A: %s\n", search ("Node A", first)); printf ("searching for Node C: %s\n", search ("Node C", first)); printf ("searching for Node E: %s\n", search ("Node E", first)); printf ("searching for Node G: %s\n", search ("Node G", first)); /* print list in reverse order */ printf ("List in Reverse Order -- Brute Force: "); printReverseBruteForce (first); printf ("List in Reverse Order -- Recursively: "); printReverseRecursive (first); printf ("Formatted Reverse List -- Recursively: "); printReverseRecursiveFormatted (first); /* clean up */ printf ("\ncleaning up\n"); listDelete (&first); printf ("pragram completed\n"); return 0; }