c - How to write a function within a function (list_map) -
Hello, I have asked some questions on linked lists.
First of all I want to thank everyone for helping me with this. But I have a problem that I can not understand. I also asked the professor, but he emailed me back with a bit of information, in fact I am writing a link in C (see link above). One of the professors gives us the header file:
zero list_map (INTLIST * list, zero (* f) (zero *)); / * Implements a function for each element of the list. * /
So I emailed him about this and said:
Another question In the header file, you have not defined a sorting function, do we have to type a sorting function with prototype and what is the list_map
and responded with it: / P>
Tell you to implement the sorting function It is what is called through Suchi_map (List, F). Hope it clears your doubts.
I only doubt that it has not been completely taught. Here's some pseudo code: How can I actually sort the link list?
tmp = head; While (TMP! = Null) {tmp2 = tmp- & gt; next; // indicator for the next node while (tmp2! = Null) {if (tmp2-> data & lt; tmp- & gt; data) {int x = tmp2- & gt; Data; Tmp2- & gt; Data = tmp-> Data; Tmp2- & gt; Data = x; } Tmp2 = tmp2-> next; } Tmp = tmp- & gt; next; }
I know that experts say that this is not the most efficient, and I understand that I am learning now and am trying to work. I can clear the following questions ... then on my question.
My question has been given that I have a sort function (in the case of a professor he calls it f). When I sign, how do I call this sorting function:
zero list_map (INTLIST * list, zero (* f) (zero *));
Will I just say:
list_map (myList, f ()); // Apply the function F in the current linked list
Do I need to define the list_map elsewhere? I am not a typical student, just looking for someone to do my job
[edit]
Thank you all.
I wanted to add one blackbuck from that poster.
"Thus, your job is to create a sorting function that you will send in the list_map. Note that it can be in the right syntax to pass:"
So my code should just be:
In the .h file I have the function prototype in this form:
zero myCustomSort (zero *);
And then. This happens in CPP:
myCustomSort zero (zero * f) {tmp = f-> gt; Head; While (TMP! = Null) {tmp2 = tmp- & gt; next; // indicator for the next node while (tmp2! = Null) {if (tmp2-> data & lt; tmp- & gt; data) {int x = tmp2- & gt; Data; Tmp2- & gt; Data = tmp-> Data; Tmp2- & gt; Data = x; } Tmp2 = tmp2-> next; } Tmp = tmp- & gt; next; }}
And I just do this to call the main:
list_map (myListPointer, and myCustomSort);
But do I need to define a catalog map anywhere? Because it is in .h file, do not I have to define it?
Assume that You can apply a Wherever Because it is a homework, I am trying to not remove the whole solution. list_map
is implemented like this,
zero list_map (INTLIST * list, zero (* f) (zero *)) {INTLIST * node; (Node = list; node; node = node-> next) f (node); }
zero list_sort (INTLIST * list) to {list_map (list, swap_head_with_smallest); }
void swap_head_with_smallest (zero *)
list then swaps data in the node provided with the smallest data of any nodes.
zero swap_head_v_most (zero * list) {INTLIST * head = list; INTLIST * smallest; / * Head, head-> Tail, head-> Tail-> Tail, etc. * / / * Swap head-gt; Data and smallest- & gt; Data * /}
Comments
Post a Comment