码迷,mamicode.com
首页 > 其他好文 > 详细

Linus Torvalds 指针

时间:2014-05-22 04:40:17      阅读:245      评论:0      收藏:0      [点我收藏+]

标签:des   style   blog   class   c   code   

《对话Linus Torvalds:大多黑客甚至连指针都未理解》
http://www.csdn.net/article/2013-01-10/2813559-two-star-programming

 

“不懂指针”的开发者代码示例:

bubuko.com,布布扣
 1 typedef struct node  
 2 {  
 3     struct node * next;  
 4     ....  
 5 } node;  
 6  
 7 typedef bool (* remove_fn)(node const * v);  
 8  
 9 // Remove all nodes from the supplied list for which the   
10 // supplied remove function returns true.  
11 // Returns the new head of the list.  
12 node * remove_if(node * head, remove_fn rm)  
13 {  
14     for (node * prev = NULL, * curr = head; curr != NULL; )  
15     {  
16         node * next = curr->next;  
17         if (rm(curr))  
18         {  
19             if (prev)  
20                 prev->next = curr->next;  
21             else  
22                 head = curr->next;  
23             free(curr);  
24         }  
25         else  
26             prev = curr;  
27         curr = next;  
28     }  
29     return head;  
30 } 
bubuko.com,布布扣

 

 

Linus Torvalds提供的解决方案:

bubuko.com,布布扣
 1 void remove_if(node ** head, remove_fn rm)  
 2 {  
 3     for (node** curr = head; *curr; )  
 4     {  
 5         node * entry = *curr;  
 6         if (rm(entry))  
 7         {  
 8             *curr = entry->next;  
 9             free(entry);  
10         }  
11         else  
12             curr = &entry->next;
13     }  
14 } 
bubuko.com,布布扣

 

Linus Torvalds 指针,布布扣,bubuko.com

Linus Torvalds 指针

标签:des   style   blog   class   c   code   

原文地址:http://www.cnblogs.com/qingsiburan/p/3738249.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!