/*------------------------------------------------------------------*/ /* teststack.c (Client of a Generic Stack ADT that Uses Function */ /* Pointers) */ /*------------------------------------------------------------------*/ #include #include #include "stack.h" /*------------------------------------------------------------------*/ static void printString(void *pvItem, void *pvExtra) /* Print string *pvItem using format pvFormat. */ { char *pcItem = (char*)pvItem; char *pcFormat = (char*)pvExtra; printf(pcFormat, pcItem); } /*------------------------------------------------------------------*/ static void sumDouble(void *pvItem, void *pvExtra) /* Accumulate double *pvItem into *pvExtra. */ { double *pdItem = (double*)pvItem; double* pdSum = (double*)pvExtra; *pdSum += *pdItem; } /*------------------------------------------------------------------*/ int main(void) /* Test the Stack ADT. */ { Stack_T oStack1; Stack_T oStack2; double d1 = 1.1; double d2 = 2.2; double d3 = 3.3; double dSum = 0.0; /* Create and use a Stack of strings. */ oStack1 = Stack_new(); Stack_push(oStack1, "Ruth"); Stack_push(oStack1, "Gehrig"); Stack_push(oStack1, "Mantle"); Stack_push(oStack1, "Jeter"); Stack_map(oStack1, printString, "%s\n"); Stack_free(oStack1); /* Create and use a Stack of doubles. */ oStack2 = Stack_new(); Stack_push(oStack2, &d1); Stack_push(oStack2, &d2); Stack_push(oStack2, &d3); Stack_map(oStack2, sumDouble, &dSum); printf("The sum is %g.\n", dSum); Stack_free(oStack2); return 0; }