回调函数

2018-01-17 18:41:18 浏览数 (1)

  有点类似模板的功能,可以使用函数指针作为参数,当调用函数时,使用void *进行传递参数,细致比较时,再用int *之类的进行强制转换。回调函数,其实就是在参数中定义函数,调用时,回到主函数去调用这个函数。仔细用法如下:

首先定义查找函数

代码语言:javascript复制
Node * search_list(Node *node, void const *value, int (*compare)(void const *,void const *)){
    while(node!=NULL){
        if(compare(&node->data,value) == 0 )
            break;
        node = node->next;
    }
    return node;
}

比较函数

代码语言:javascript复制
int compare_ints(void const *a,void const *b){
    if( *(int *)a == *(int *)b)
        return 0;
    else
        return 1;
}

函数中的调用

代码语言:javascript复制
    int *desire = (int *)malloc(sizeof(int));
    *desire = 3;
    Node *n1 = (Node *)malloc(sizeof(Node)); 
    n1 = search_list(L->next,desire,compare_ints);
    if(n1!=NULL)
        printf("找到了%d",n1->data);

全部代码

代码语言:javascript复制
  1 #include <stdio.h>
  2 #include <stdlib.h>
  3 
  4 typedef struct Node{
  5     int data;
  6     struct Node * next;
  7 }Node;
  8 
  9 void createList(Node * L,int len);
 10 void showList(Node *L);
 11 void clearList(Node *L);
 12 void getNode(Node *L,int n,Node *tar);
 13 int insertNode(Node *L,int n,int num);
 14 int deleteNode(Node *L,int n);
 15 Node * search_list(Node *node, void const *value, int (*compare)(void const *,void const *));
 16 int compare_ints(void const *a,void const *b);
 17 
 18 int main()
 19 {
 20     Node *L= (Node *)malloc(sizeof(Node));
 21 
 22     createList(L,5);
 23     showList(L);
 24 
 25     Node *tar= (Node *)malloc(sizeof(Node));
 26     getNode(L,3,tar);
 27     printf("the third is:%dn",tar->data);
 28     
 29     if(insertNode(L,3,0))
 30         showList(L);
 31     
 32     printf("回调函数:n");
 33     int *desire = (int *)malloc(sizeof(int));
 34     *desire = 3;
 35     Node *n1 = (Node *)malloc(sizeof(Node)); 
 36     n1 = search_list(L->next,desire,compare_ints);
 37     if(n1!=NULL)
 38         printf("找到了%d",n1->data);
 39 
 40     /*
 41     if(deleteNode(L,3))
 42         showList(L);
 43 
 44     clearList(L);
 45     showList(L);
 46     */
 47 
 48     /**desire = 9;
 49     Node *n2 = search_list(L,desire,compare_ints);
 50     if(n2!=NULL)
 51         printf("找到了%d",n2->data);
 52     *desire = 7;
 53     Node *n3 = search_list(L,desire,compare_ints);
 54     if(n3!=NULL)
 55         printf("找到了%d",n3->data);*/
 56     getchar();
 57     return 0;
 58 }
 59 Node * search_list(Node *node, void const *value, int (*compare)(void const *,void const *)){
 60     while(node!=NULL){
 61         if(compare(&node->data,value) == 0 )
 62             break;
 63         node = node->next;
 64     }
 65     return node;
 66 }
 67 int compare_ints(void const *a,void const *b){
 68     if( *(int *)a == *(int *)b)
 69         return 0;
 70     else
 71         return 1;
 72 }
 73 
 74 void createList(Node * L,int len){
 75     int i;
 76     Node * p;
 77     L->next = NULL;
 78     for(i=0;i<len;i  ){
 79         p = (Node *)malloc(sizeof(Node));
 80         p->data = 2*i 1;
 81         p->next = L->next;
 82         L->next = p;
 83     }
 84 }
 85 
 86 void showList(Node *L){
 87     Node *p = (Node *)malloc(sizeof(Node));
 88     p=L->next;
 89     while(p){
 90         printf("%d->",p->data);
 91         p=p->next;
 92     }
 93     printf("nulln");
 94     free(p);
 95 }
 96 
 97 void clearList(Node *L){
 98     Node *p,*q;
 99     p=L->next;
100     while(p){
101         q=p->next;
102         free(p);
103         p=q;
104     }
105     L->next=NULL;
106 }
107 
108 void getNode(Node *L,int n,Node *tar){
109     int i=1;
110     Node *p;
111     p=L->next;
112     while(p && i<n){
113         p=p->next;
114         i  ;
115     }
116     if(!p || i>n)
117         printf("error!");
118     tar->data=p->data;
119 }
120 
121 int insertNode(Node *L,int n,int num){
122     int i=1;
123     Node *p = L->next;
124     while( p && i<n-1){
125         p=p->next;
126         i  ;
127     }
128     if(!p || i>n-1)
129         return 0;
130     Node *q = (Node *)malloc(sizeof(Node));
131     q->data = num;
132     q->next = p->next;
133     p->next = q;
134     return 1;
135 }
136 
137 int deleteNode(Node *L,int n){
138     int i=1;
139     Node *p = L->next;
140     Node *q;
141     while( p->next && i<n-1){
142         p=p->next;
143         i  ;
144     }
145     if( !(p->next) || i>n-1)
146         return 0;
147     q=p->next;
148     p->next = q->next;
149     free(q);
150     return 1;
151 }

运行示例

0 人点赞