/******************************************************************************* * * 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 */ /* * insert n into list and return new list * exits program if unable to get space (FIXME?) */ sorted_int_list_node_t * sorted_int_list_insert( sorted_int_list_node_t * lst, int n); /* * count occurrences of n in list */ int sorted_int_list_count(sorted_int_list_node_t * lst, int n); /* * remove all occurrences of n from list and return new list */ sorted_int_list_node_t * sorted_int_list_remove( sorted_int_list_node_t * lst, int n); /* * remove all nodes from list and return NULL */ sorted_int_list_node_t * sorted_int_list_remove_all( sorted_int_list_node_t * lst); /* * 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); #endif /* end of ifndef SORTED_INT_LIST_H */