Note that there are some explanatory texts on larger screens.

plurals
  1. POHow to write a function within a function (list_map)
    primarykey
    data
    text
    <p>Hello I recently asked some questions on linked lists in C.<br> <a href="https://stackoverflow.com/questions/2106691/c-issue-cant-figure-how-to-assign-pointer-to-beginning-of-list">The link was found here</a></p> <p>First I want to thank everyone for helping me with this. But I have one issue I cannot understand. I even asked the professor but he emailed me back with little information. Basically I am writing a linked list in C (see above link). One of the things the professor gives us in the header file is this:</p> <pre><code>void list_map( INTLIST *list, void (*f)(void *) ); /*Applies a function to each element of the list */ </code></pre> <p>So I emailed him about this and said:</p> <blockquote> <p>Another question, in the header file you did not define a sorting function, do we need to write a sorting function with the prototype and finally what is list_map</p> </blockquote> <p>And he replied with:</p> <blockquote> <p>You are asked to implement a sorting function f, which is called through list_map(list, f). Hope it clears your doubts.</p> </blockquote> <p>My only doubts are this was not fully taught. I can understand how to sort the linked list in fact here is some pseudo code:</p> <pre><code>tmp=head; while(tmp!=NULL) { tmp2=tmp-&gt;next; //pointer to next node while(tmp2!=NULL) { if (tmp2-&gt;data &lt; tmp-&gt;data) { int x = tmp2-&gt;data; tmp2-&gt;data = tmp-&gt;data; tmp2-&gt;data = x; } tmp2=tmp2-&gt;next; } tmp=tmp-&gt;next; } </code></pre> <p>I know the experts might say this is not the most efficient, and I understand that right now I am just learning and trying to get things working. I can clean up afterwords...so on to my question.</p> <p>My question is given I have the sort function (in the professor's case he calls it f). How would I call this sorting function when the signature is:</p> <pre><code>void list_map(INTLIST* list, void (*f) (void*)); </code></pre> <p>Would I just say:</p> <pre><code>list_map(myList, f()); //apply function f to the current linked list </code></pre> <p>Or do I really need to define list_map somewhere? I am not the typical student just looking for someone to do my work. I am really trying to understand this as best I can.</p> <p>Thanks to all of you.</p> <p><strong>[EDIT PORTION]</strong></p> <p>I wanted to add that one of the posters Kaleb P. said </p> <blockquote> <p>"Thus, your job is to create a sorting function that you will pass in to list_map. Note that the correct syntax for passing it in will be:"</p> </blockquote> <p>So should my code simply be this:</p> <p>in the .h file I prototype the function as:</p> <pre><code>void myCustomSort(void*); </code></pre> <p>And then in the .cpp it becomes:</p> <pre><code>void myCustomSort(void*f) { tmp=f-&gt;head; while(tmp!=NULL) { tmp2=tmp-&gt;next; //pointer to next node while(tmp2!=NULL) { if (tmp2-&gt;data &lt; tmp-&gt;data) { int x = tmp2-&gt;data; tmp2-&gt;data = tmp-&gt;data; tmp2-&gt;data = x; } tmp2=tmp2-&gt;next; } tmp=tmp-&gt;next; } } </code></pre> <p>And to call it in main I would just do:</p> <pre><code>list_map(myListPointer, &amp;myCustomSort); </code></pre> <p>But don't I need to define list_map anywhere? Because it is in the .h file do I not have to define it?</p>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload