I am working on a bubble sort function for a linked list. Here is the header for the function:
void sort(struct lnode** head,
void (*swapPtr)(struct lnode** head, struct lnode* n1, struct lnode* n2),
int (*comparePtr)(void* v1, void* v2))
I don't really understand the function pointers being used. swapPtr
is a function pointer to a function that is used to swap two nodes in the list. comparePtr
is used as a pointer to one of three functions, all of which compare either the values in a certain member of the structure which is used to store the number of counts and line number of a given word. The possibilities are:
countComp
— takes two nodes and compares the number of times the word shows up, returns 0 if equal, 1 if node1 > node 2 and -1 for the reverse.wordComp
— compares the words for the given nodes same return values as above.lineComp
— compares the line number the word occurs on same return values as above.
I understand how the bubble sort works and the general steps to achieve a sorted list. The area I am confused on is on how to call comparePtr
and what do I need to pass to it? Also I have a test.c
file used for testing my sorting methods. How would I go about calling the sort function? I'm not sure what to pass for the second and third arguments.
If someone could help explain this to me, it'd be great!