Hello, I recently asked a few questions on linked lists in C.
The link was found here.
First, I want to thank everyone for helping me with this. But I have one problem that I cannot understand. I even asked the professor, but he e-mailed me some information. Basically, I write a linked list in C (see Link above). One of the things the professor gives us in the header file is this:
void list_map( INTLIST *list, void (*f)(void *) );
So I emailed him about this and said:
Another question: in the header file you did not define a sort function, we need to write a sort function with a prototype, and finally, what is list_map
And he answered:
You will be asked to implement the sort function f, which is called via list_map (list, f). Hope it clears your doubts.
My only doubt is that this has not been fully taken into account. I can understand how to actually sort the linked list, here is some kind of pseudo code:
tmp=head; while(tmp!=NULL) { tmp2=tmp->next; //pointer to next node while(tmp2!=NULL) { if (tmp2->data < tmp->data) { int x = tmp2->data; tmp2->data = tmp->data; tmp2->data = x; } tmp2=tmp2->next; } tmp=tmp->next; }
I know that experts can say that this is not the most effective, and I understand that now I am just studying and trying to make everything work. I can clear the afterword ... so on to my question.
My question is asked. I have a sort function (in the case of a professor, he calls it f). How can I call this sort function when the signature is:
void list_map(INTLIST* list, void (*f) (void*));
I would say:
list_map(myList, f());
Or do I really need to define list_map somewhere? I'm not a typical student, just looking for someone to do my job. I'm really trying to figure it out as best as possible.
Thank you everybody.
[EDIT PORTION]
I wanted to add that one of the posters Caleb P. said
"Therefore, your task is to create a sort of the function with which you list_map. Note that the correct syntax for passing it will be:"
So should my code just be as follows:
in the .h file, I am a function prototype:
void myCustomSort(void*);
And then in .cpp it will be:
void myCustomSort(void*f) { tmp=f->head; while(tmp!=NULL) { tmp2=tmp->next; //pointer to next node while(tmp2!=NULL) { if (tmp2->data < tmp->data) { int x = tmp2->data; tmp2->data = tmp->data; tmp2->data = x; } tmp2=tmp2->next; } tmp=tmp->next; } }
And to call it basically, I would just do:
list_map(myListPointer, &myCustomSort);
But don't I need to define list_map anywhere? Since it is in the .h file, do I need to define it?