回調函數

  有點相似模板的功能,可使用函數指針做爲參數,當調用函數時,使用void *進行傳遞參數,細緻比較時,再用int *之類的進行強制轉換。回調函數,其實就是在參數中定義函數,調用時,回到主函數去調用這個函數。仔細用法以下:node

首先定義查找函數

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; }

比較函數

int compare_ints(void const *a,void const *b){ if( *(int *)a == *(int *)b) return 0; else
        return 1; }

函數中的調用

 

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);

 

所有代碼

 

 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:%d\n",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("null\n");  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 }
View Code

 

運行示例

相關文章
相關標籤/搜索
本站公眾號
   歡迎關注本站公眾號,獲取更多信息