如何将一个函数中写一个函数(list_map) [英] How to write a function within a function (list_map)

查看:200
本文介绍了如何将一个函数中写一个函数(list_map)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

您好我最近问C.结果上链表一些问题
<一href=\"http://stackoverflow.com/questions/2106691/c-issue-cant-figure-how-to-assign-pointer-to-beginning-of-list\">The链接在这里被发现

Hello I recently asked some questions on linked lists in C.
The link was found here

首先,我要感谢大家对我的帮助与此有关。但我有一个问题,我无法理解。我甚至问教授,但他给我发电子邮件回来的信息很少。基本上,我写在C(见上面的链接)的链接列表。其中一件事教授给了我们在头文件是这样的:

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:

void list_map( INTLIST *list, void (*f)(void *) );
/*Applies a function to each element of the list */

所以我给了他这件事,说:

So I emailed him about this and said:

另一个问题,在头文件中没有定义排序功能,我们需要编写的原型分类功能,最后是什么list_map

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

和他的回答是:

您被要求实施分类函数f,它是通过list_map称为(列表,F)。希望它会清除你的疑惑。

You are asked to implement a sorting function f, which is called through list_map(list, f). Hope it clears your doubts.

我唯一的疑虑是这个并未完全授课。我可以理解如何在这里其实链表排序是一些伪code:

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:

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 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.

我的问题是给我已经排序功能(在教授的情况下,他称之为F)。我怎么会调用这个排序功能时,签名是:

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:

void list_map(INTLIST* list, void (*f) (void*));

请问我只想说:

list_map(myList, f()); //apply function f to the current linked list

或者我真的需要的地方定义list_map?我不是典型的学生只是想找人做我的工作。我真的想明白这一点,尽我所能。

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.说。

I wanted to add that one of the posters Kaleb P. said

这样,你的任务是创建一个排序
  函数,你会传递到
  list_map。需要注意的是正确的语法
  在通过这将是:

"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:"

于是我的code仅仅是这样的:

So should my code simply be this:

在.h文件我的原型功能:

in the .h file I prototype the function as:

void myCustomSort(void*);

然后在的.cpp就变成:

And then in the .cpp it becomes:

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 in main I would just do:

list_map(myListPointer, &myCustomSort); 

但是,不要我需要的任何地方定义list_map?因为是在.h文件我没有界定呢?

But don't I need to define list_map anywhere? Because it is in the .h file do I not have to define it?

推荐答案

假设 list_map 是这样实现的,给予˚F按顺序每个节点,

Assuming list_map is implemented like this, giving f each node in sequential order,

void list_map(INTLIST *list, void (*f)(void *)) {
    INTLIST *node;
    for (node = list; node; node = node->next)
        f(node);
}

您可以实现一个选择排序

void list_sort(INTLIST *list) {
    list_map(list, swap_head_with_smallest);
}

其中,无效swap_head_with_smallest(无效*)交换用最小的任一节点的数据,给定节点的数据列表中的下吧。

where void swap_head_with_smallest(void *) swaps the datum of a given node with the smallest of the data of any nodes following it in the list.

由于这是功课,我试图不给整个解决方案了。

As this is homework, I'm trying not to give the whole solution away.

void swap_head_with_smallest(void *list) {
    INTLIST *head = list;
    INTLIST *smallest;

    /* set smallest the smallest node of
         head, head->tail, head->tail->tail, etc. */

    /* swap head->datum and smallest->datum */
}

这篇关于如何将一个函数中写一个函数(list_map)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆