Singly-Linked Linked List of Integers

/** NOTE: I used nothing but * notation in this program. I expected that you would be using pointers (->) and since the book no longer has the * notation, I decided to give you an example. I also added a couple of simple functions to this program such as is_empty() which make the code a bit more readable. I also added several defines to enhance readability. **/ /***** * Brenda Sonderegger CS210 Fall, 1994 * **** Linked list example ***/ #include <stdio.h> #include "myio_c.h" /** syntatic sugar **/ #define TRUE 1 #define FALSE 0 #define INSERT 1 #define DELETE 2 #define PRINT 3 #define QUIT 4 /** the structure definition and the prototypes for the functions using the structure all belong in a linked.h file **/ typedef struct node_record { int info; struct node_record *link; } NODE; /** I left variable names in to help explain what was being sent **/ int is_empty(NODE *first); void get_memory(NODE **new_node); void fill_node(NODE *new_node); void insert_node(NODE **first, NODE *new_node); /** void create_list(NODE **first, NODE *new_node); void add_node(NODE **first, NODE *new_node); **/ void delete_node(NODE **first, int value); void print_list(NODE *first); /** print_menu() and main() should all be in a driver.c file **/ void print_menu(void); void main(void) { NODE *first, *new_node; int flag, option, value; first = new_node = NULL; do { print_menu(); printf("\nEnter your choice now "); get_int_bound(&option, 1, 4); switch(option) { case INSERT: get_memory(&new_node); fill_node(new_node); insert_node(&first, new_node); /**** if (is_empty(first) == TRUE) create_list(&first, new_node); else add_node(&first, new_node); ****/ break; case DELETE: if (is_empty(first) == TRUE) printf("\nList is empty, cannot delete\n"); else { printf("\nEnter a value to delete "); get_int(&value); delete_node(&first, value); } break; case PRINT: print_list(first); break; case QUIT: printf("\nQuitting program.\n\n"); break; default: printf("\nInvalid entry for option - try again\n"); } } while (option != QUIT); } void print_menu(void) { printf("\nEnter your choice \n"); printf("\n1 - Insert a new node"); printf("\n2 - Delete a node"); printf("\n3 - Print the list"); printf("\n4 - Quit the program\n"); } /*****all functions from here down belong in linked.c file*****/ void insert_node(NODE **first, NODE *new_node) { NODE *current, *previous; if ((*first == NULL) || ((*new_node).info <= (**first).info)) { /*insert at front */ (*new_node).link = *first; *first = new_node; } else { current = *first; while (((*new_node).info > (*current).info) && ((*current).link != NULL)) { previous = current; current = (*current).link; } if ((*new_node).info <= (*current).info) { /* insert middle */ (*new_node).link = current; (*previous).link = new_node; } else { /* insert end */ (*current).link = new_node; } } } void delete_node(NODE **start, int value) { NODE *current, *previous; int found = FALSE; current = *start; if ((*current).info == value) { found = TRUE; if ((*current).link == NULL) { /** only 1 element in list **/ *start = NULL; } else { /** delete first element **/ *start = (**start).link; } } else { /** delete from middle and end **/ while (((*current).info < value) && ((*current).link != NULL)) { previous = current; current = (*current).link; } if ((*current).info == value) { /** delete current **/ (*previous).link = (*current).link; found = TRUE; } } if (found == TRUE) free((void *)current); else printf("\n%d is not in the list.\n", value); } void get_memory(NODE **new_node) { *new_node = (NODE *)malloc(sizeof(NODE)); if (*new_node == NULL) { printf("\n\nOut of memory - terminating.\n\n"); exit(1); } } void fill_node(NODE *new_node) { printf("\nEnter an integer "); get_int(&(*new_node).info); (*new_node).link = NULL; } int is_empty(NODE *first) { if (first == NULL) return(TRUE); else return(FALSE); } void print_list(NODE *start) { if (is_empty(start) == TRUE) { printf("\nList is empty.\n"); } else { printf("\n"); while(start != NULL) { printf("%d ", (*start).info); start = (*start).link; } } }