ACPbE, Chpt3.3, ex3.7-3.12,Linked Lists

Run Settings
LanguageC
Language Version
Run Command
// Includes. #include "llist.h" int main () { char string[20] ; int found, duplicate ; NODE *list, *nodeptr ; int insert( NODE *pList, char *string) ; int delete( NODE *pList, char *string) ; NODE *init_list( void) ; NODE *find( NODE *pList, char *string) ; void traverse ( NODE *pList) ; // init LList, dummy nodes list = init_list(); while( printf ("Enter string (or quit): "), gets (string), printf ("%s\n", string), strcmp ( string, "quit")) { duplicate = insert (list, string) ; if (duplicate) { printf ("String in LList already\n") ; } } // // now print all strings in LList // traverse (list) ; // query user for keys/strings to delete or quit // lookout for missing keys, warn user. while( printf("Enter string (or quit): "), gets (string), printf ("%s\n", string), strcmp( string, "quit")) { found = delete (list, string) ; if (!found) { printf ("Can't find string in LList node\n") ; } else { printf ("Deleted node with string %s\n", string) ; } } // // now print all strings in LList (that survived deletion) // traverse (list) ; // query user for keys/strings to find or quit // lookout for missing keys, warn user. while( printf ("Enter string (or quit): "), gets (string), printf ("%s\n", string), strcmp (string, "quit")) { nodeptr = find (list, string) ; if (!nodeptr) { printf ("Can't find node w string in LList\n") ; } else { printf ("Found node with string %s\n", nodeptr->string) ; } } #ifdef TEST2 // populate the LList w strings/keys or quit // lookout for duplicates, warn user. // populate the LList w strings/keys or quit // lookout for duplicates, warn user. while( printf("Enter string (or quit): "), gets (string), strcmp( string, "quit")) { printf ("string is %s\n", string) ; duplicate = insert (list, string) ; if (duplicate) { printf ("String in LList already\n") ; } } // // now print all strings in LList // traverse (list) ; // query user for keys/strings to find or quit // lookout for missing keys, warn user. while( printf("Enter string (or quit): "), gets (string), strcmp( string, "quit")) { nodeptr = find (list, string) ; if (!nodeptr) { printf ("Can't find node w string in LList node\n") ; } else { printf ("Found node with string %s\n", nodeptr->string) ; } } #endif // printf ("main()\n") ; }
// llist.h // Include. #include <stdio.h> #include <string.h> #include <stdlib.h> // malloc() and exit() // Defines. #define DUMMY_TRAILER '\177' #define DUPLICATE 1 #define NEW_NODE 0 #define FOUND 1 #define NOT_FOUND 0 // Types. typedef struct node NODE; struct node { char string[20]; NODE *next; };
// Includes. #include "llist.h" // init_list() nodes // parms (void) // return ptr to LList (NODE *) // NODE *init_list(void) { NODE *plist = NULL; //printf ("init_list(),s\n") ; if ( (plist = (NODE *) malloc(sizeof(NODE))) == NULL) { printf ("Fatal malloc(), init_list()\n"); exit (1); } plist->string[0] = '\0' ; if ( (plist->next = (NODE *) malloc(sizeof(NODE))) == NULL) { printf ("Fatal malloc(), init_list()\n"); exit (2); } //printf ("plist adrs %x\n", plist) ; //printf ("plist->next adrs %x\n", plist->next) ; plist->next->string[0] = DUMMY_TRAILER ; plist->next->string[1] = '\0' ; plist->next->next = NULL ; //printf ("init_list(),e\n") ; return plist ; }
// Includes. #include "llist.h" // insert() nodes // parms (ptr to Llist (NODE), ptr to key, (string) // return value successCode // int insert (NODE *plist, char *string) { NODE *cur = plist->next ; NODE *prev= plist ; NODE *new ; while ( strcmp(string, cur->string) > 0) { prev = cur ; cur = cur->next ; } if ( strcmp(string, cur->string) == 0 ) { return DUPLICATE ; } else { if ( ( new = (NODE *)malloc(sizeof(NODE))) == NULL ) { printf ("Fatal malloc(), insert()\n") ; exit (3) ; } strcpy (new->string, string) ; new->next = cur ; prev->next = new ; return NEW_NODE ; } }
// Includes. #include "llist.h" // delete() a node // parms (ptr to Llist (NODE), ptr to key, (string) // return value successCode // int delete( NODE *plist, char *string) { NODE *cur = plist->next ; //set chasing ptrs, prev, cur NODE *prev= plist ; // march thru list, til you've passed "key, string" while( strcmp( string, cur->string) > 0) { prev = cur ; cur = cur->next ; } if( strcmp( string, cur->string) != 0 ) { return NOT_FOUND ; } else { prev->next = cur->next ; free (cur) ; return FOUND ; } }
// Includes. #include "llist.h" // // NODE *find (NODE *pList, char *string) // params, ptr to LList, NODE *, ptr to key, char * // return, NODE ptr // march across LList, printf content of each 'node' (ignore dummies) NODE *find (NODE *pList, char *string) { pList = pList->next ; while ( strcmp (string, pList->string) >= 0 ) { if ( strcmp (string, pList->string) == 0 ) { return pList ; } pList = pList->next ; } return NULL ; } // void traverse (NODE *pList) // params, ptr to LList, NODE * // return, none // march across LList, printf content of each 'node' (ignore dummies) // void traverse (NODE *pList) { pList = pList->next ; while (pList->string[0] != DUMMY_TRAILER) { printf("%s\n", pList->string) ; pList = pList->next ; } }
Editor Settings
Theme
Key bindings
Full width
Lines