/******************************************************************************* * * simple implementation of sorted list of ints * * "list" is represented by pointer to first node */ /* * the next two directives (and the last line) make the .h file "idempotent" -- * if you inadvertently #include it twice, no harm done, * since the second #include finds SORTED_INT_LIST_H defined * and ignores the rest of the file. * this is (usually?) considered to be good style. */ #ifndef SORTED_INT_LIST_H #define SORTED_INT_LIST_H #include #include #include /* * data structure for list node * * type to use in declaring nodes, and pointers to nodes, * is sorted_int_list_node_t */ typedef struct sorted_int_list_node { int data; struct sorted_int_list_node * next; } sorted_int_list_node_t; /* * functions * * notice that functions that might change the head of a list take as a * parameter a sorted_int_list_node_t ** (pointer to pointer to node). * so that if called with the address of a variable they can change it. */ /* * insert n into *lst_p * returns true if insertion succeeded, false otherwise (e.g., malloc error) */ bool sorted_int_list_insert(sorted_int_list_node_t ** lst_p, int n); /* * count occurrences of n in lst */ int sorted_int_list_count(sorted_int_list_node_t * lst, int n); /* * remove all occurrences of n from *lst_p */ void sorted_int_list_remove(sorted_int_list_node_t ** lst_p, int n); /* * remove all nodes from *lst_p and set to NULL */ void sorted_int_list_remove_all(sorted_int_list_node_t ** lst_p); /* * print all elements of lst to output stream f using format fmt * ("fmt" is the kind of string expected by fprintf, e.g., "%d " or "%d\n") * * the FILE * parameter lets the caller decide whether to print * to stdout or a file (already open) or what * the fmt parameter lets the caller decide whether to print all on * one line or each on a separate line or what */ void sorted_int_list_print(sorted_int_list_node_t * lst, FILE * f, char * fmt); /* * filter: * produces new list * sets *error true if unable to make new list, else false */ sorted_int_list_node_t * sorted_int_list_filter(sorted_int_list_node_t * lst, bool (*filter_fcn)(int), bool * error); /* * map: * produces new list * sets *error true if unable to make new list, else false */ sorted_int_list_node_t * sorted_int_list_map(sorted_int_list_node_t * lst, int (*map_fcn)(int), bool * error); #endif /* end of ifndef SORTED_INT_LIST_H */