



Study with the several resources on Docsity
Earn points by helping other students or get them with a premium plan
This page cannot be seen from the preview
Don't miss anything!




Problem : Suppose I want to call a function in my program, but I won't know which function the user wants to call until run‐time. Solution : Store the name of the function in a variable and use the variable (Function names are actually pointers!!) int (funcPtr) (int i); // funcPtr will hold the name // of a function that has a // prototype like // int func(int i) … funcPtr = doIt; // doIt is an actual function answer = funcPtr(num); // really a call to doIt // could also be written: answer = (funcPtr)(num);
new_node->value = new_value; new_node->next = NULL; // head should not be null, but in case it is .... if (head == NULL) { head = list_create(); } // we never insert before the dummy header, so only the // 'insert middle and end' cases are needed! for(tmp = head; tmp->next != NULL; tmp = tmp->next) { if (new_value < tmp->next->value) { new_node->next = tmp->next; tmp->next = new_node; return head; } } tmp->next = new_node; return head; } // a cheese-y test driver ... what cases are tested? which // are missed (for new version of list) int main() { List *l = list_create(); list_print("empty list ", l); int a[] = {5, 9, 7}; l = list_insert(l, a, compare); list_print("[5] ", l, output); l = list_insert(l, a+1, compare); list_print("[5 9] ", l, output); l = list_insert(l, a+2, compare); list_print("[5 7 9] ", l, output); l = list_delete(l, a+2, compare); list_print("[5 7 ] ", l, output); }