#include /* definitions and prototypes for calloc() below */ #include /* definitions and prototypes for printf() below */ /* definition of list item structure */ struct Item_t { double data; struct Item_t * next; }; /* type "Item" is defined in the header file */ #include "mylist.h" Item * makeItem( double data ) { Item * item = (Item *)calloc( 1, sizeof( Item ) ); item->data = data; return item; } void freeItems( Item * first ) { Item *item = first; /* the for loop doesn't work for the free function, * because by the time you look at the item->next member, * item has been freed. So be a little more careful: */ while( item != NULL ) { Item *next = item->next; free( item ); item = next; } } void linkItem( Item * item, Item * next ) { item->next = next; } void doActionToItems( const Item * first, ItemAction action ) { const Item * item; for( item = first; item->next != NULL; item = item->next ) action( item ); } static void printItem( const Item * item ) { printf( "item data: %f\n", item->data ); } void printItems( const Item * first ) { doActionToItems( first, printItem ); }