分享

结构体的两种声明方式:堆上和栈上以及在双链表的应用

 心不留意外尘 2016-05-16

http://blog.csdn.net/unix21/article/details/8685001

2013

在看《算法精解:C语言描述》的双链表chtbl和redis的双链表adlist.c发现代码思路基本是一致的。

但是,对于链表的初始化却不一样


1.《算法精解:C语言描述》风格

  1. /***************************************************************************** 
  2. *                                                                            * 
  3. *  Define a structure for doubly-linked list elements.                       * 
  4. *                                                                            * 
  5. *****************************************************************************/  
  6.   
  7. typedef struct DListElmt_ {  
  8.   
  9. void               *data;  
  10. struct DListElmt_  *prev;  
  11. struct DListElmt_  *next;  
  12.   
  13. } DListElmt;  
  14.   
  15. /***************************************************************************** 
  16. *                                                                            * 
  17. *  Define a structure for doubly-linked lists.                               * 
  18. *                                                                            * 
  19. *****************************************************************************/  
  20.   
  21. typedef struct DList_ {  
  22.   
  23. int                size;  
  24.   
  25. int                (*match)(const void *key1, const void *key2);  
  26. void               (*destroy)(void *data);  
  27.   
  28. DListElmt          *head;  
  29. DListElmt          *tail;  
  30.   
  31. } DList;  


一开始定义结构体在栈上分配内存,后面传递结构体地址

  1. int main(int argc, char **argv) {  
  2.   
  3. DList              list;  
  4. DListElmt          *element;  
  5.   
  6. int                *data,  
  7.                    i;  
  8.   
  9. /***************************************************************************** 
  10. *                                                                            * 
  11. *  Initialize the doubly-linked list.                                        * 
  12. *                                                                            * 
  13. *****************************************************************************/  
  14.   
  15. dlist_init(&list, free);  


init的时候传递结构体指针,直接赋值

  1. void dlist_init(DList *list, void (*destroy)(void *data)) {  
  2.   
  3. /***************************************************************************** 
  4. *                                                                            * 
  5. *  Initialize the list.                                                      * 
  6. *                                                                            * 
  7. *****************************************************************************/  
  8.   
  9. list->size = 0;  
  10. list->destroy = destroy;  
  11. list->head = NULL;  
  12. list->tail = NULL;  
  13.   
  14. return;  
  15.   
  16. }  

2.Redis的风格

init的时候在堆上动态分配内存,返回结构体指针

  1. /* Create a new list. The created list can be freed with 
  2.  * AlFreeList(), but private value of every node need to be freed 
  3.  * by the user before to call AlFreeList(). 
  4.  * 
  5.  * On error, NULL is returned. Otherwise the pointer to the new list. */  
  6. list *listCreate(void)  
  7. {  
  8.     struct list *list;  
  9.   
  10.     if ((list = zmalloc(sizeof(*list))) == NULL)  
  11.         return NULL;  
  12.     list->head = list->tail = NULL;  
  13.     list->len = 0;  
  14.     list->dup = NULL;  
  15.     list->free = NULL;  
  16.     list->match = NULL;  
  17.     return list;  
  18. }  

  1. typedef struct listNode {  
  2.     struct listNode *prev;  
  3.     struct listNode *next;  
  4.     void *value;  
  5. } listNode;  
  6.   
  7. typedef struct list {  
  8.     listNode *head;  
  9.     listNode *tail;  
  10.     void *(*dup)(void *ptr);  
  11.     void (*free)(void *ptr);  
  12.     int (*match)(void *ptr, void *key);  
  13.     unsigned long len;  
  14. } list;  


说透一级指针和二级指以及(void**)&在双链表中的应用  [这里分析了双链表的删除]

    本站是提供个人知识管理的网络存储空间,所有内容均由用户发布,不代表本站观点。请注意甄别内容中的联系方式、诱导购买等信息,谨防诈骗。如发现有害或侵权内容,请点击一键举报。
    转藏 分享 献花(0

    0条评论

    发表

    请遵守用户 评论公约

    类似文章 更多