#include #include #include "exp4.h" int compare (void* ptr1, void* ptr2); int main (void) { // Degiskenler int i = 7 ; int j = 8 ; int lrg; lrg = (*(int*) larger (&i, &j, compare)); printf ("Larger value is: %d\n", lrg); return 0; } /* ==================== compare ==================== Pre ptr1 and ptr2 are pointers to integer values Post returns +1 if ptr1 >= ptr2 returns -1 if ptr1 < ptr2 */ int compare (void* ptr1, void* ptr2){ if (*(int*)ptr1 >= *(int*)ptr2) return 1; else return -1; } typedef struct node { void* dataPtr; struct node* link; } NODE; /* =================== createNode ==================== Creates a node in dynamic memory and stores data pointer in it. Pre itemPtr is pointer to data to be stored. Post node created and its address returned. */ NODE* createNode (void* itemPtr) { NODE* nodePtr; nodePtr = (NODE*) malloc (sizeof (NODE)); nodePtr->dataPtr = itemPtr; nodePtr->link = NULL; return nodePtr; } void* larger (void* dataPtr1, void* dataPtr2, int (*ptrToCmpFun)(void*, void*)) { if ((*ptrToCmpFun) (dataPtr1, dataPtr2) > 0) return dataPtr1; else return dataPtr2; } // larger ------------ #include #include #include "exp5.h" int compare (void* ptr1, void* ptr2); int main (void) { // Degiskenler float f1 = 73.4; float f2 = 81.7; float lrg; lrg = (*(float*) larger (&f1, &f2, compare)); printf ("Larger value is: %5.1f\n", lrg); return 0; } /* ==================== compare ==================== Pre ptr1 and ptr2 are pointers to integer values Post returns +1 if ptr1 >= ptr2 returns -1 if ptr1 < ptr2 */ int compare (void* ptr1, void* ptr2) { if (*(float*)ptr1 >= *(float*)ptr2) return 1; else return -1; } typedef struct node { void* dataPtr; struct node* link; } NODE; /* =================== createNode ==================== Creates a node in dynamic memory and stores data pointer in it. Pre itemPtr is pointer to data to be stored. Post node created and its address returned. */ NODE* createNode (void* itemPtr) { NODE* nodePtr; nodePtr = (NODE*) malloc (sizeof (NODE)); nodePtr->dataPtr = itemPtr; nodePtr->link = NULL; return nodePtr; } // Pre data1, data2, comparisonFunction Post void pointer void* larger (void* dataPtr1, void* dataPtr2, int (*ptrToCmpFun)(void*, void*)) { if ((*ptrToCmpFun) (dataPtr1, dataPtr2) > 0) return dataPtr1; else return dataPtr2; }