C语言的那些小秘密之链表(二)

发表于:2011-12-14 09:38  作者:bigloomy(CSDNblog)   来源:51Testing软件测试网采编

字体: | 上一篇 | 下一篇 |我要投稿 | 推荐标签:

  接下来我们来添加一个在头结点添加结点的模块。

  1. #include <stdio.h>   
  2. #include <stdlib.h>   
  3.   
  4. typedef enum _DListReturn  
  5. {  
  6.     DLIST_RETURN_OK,  
  7.     DLIST_RETURN_FAIL  
  8. }DListReturn;  
  9.   
  10. typedef struct _DStu  
  11. {  
  12.     int score;  
  13. }DStu;  
  14.   
  15. typedef struct _DListNode  
  16. {  
  17.     struct _DListNode* prev;  
  18.     struct _DListNode* next;  
  19.   
  20.     DStu* data;  
  21.   
  22. }DListNode;  
  23.   
  24. typedef struct _DList  
  25. {  
  26.     DListNode* head;  
  27. }DList;  
  28.   
  29. typedef DListReturn (*DListPrintFunction)(void* data);  
  30.   
  31. DListNode* dlist_node_create(void* data)  
  32. {  
  33.     DListNode* node;  
  34.     if((node = (DListNode*) malloc(sizeof(DListNode)))==NULL)  
  35.     {  
  36.         printf("分配空间失败!");  
  37.         exit(0);  
  38.     }  
  39.   
  40.     if(node != NULL)  
  41.     {  
  42.         node->prev = NULL;  
  43.         node->next = NULL;  
  44.         node->data =(DStu*)data;  
  45.     }  
  46.   
  47.     return node;  
  48. }  
  49.   
  50. DList* dlist_head_create(void)  
  51. {  
  52.     DList* thiz;  
  53.     if((thiz = (DList*)malloc(sizeof(DList)))==NULL)  
  54.     {  
  55.         printf("分配空间失败!");  
  56.         exit(0);  
  57.     }  
  58.   
  59.     if(thiz != NULL)  
  60.     {  
  61.         thiz->head = NULL;  
  62.     }  
  63.   
  64.     return thiz;  
  65. }  
  66.   
  67. DListReturn dlist_append(DList* thiz, void* data)  
  68. {  
  69.     DListNode* node = NULL;  
  70.     DListNode* cursor = NULL;  
  71.   
  72.     if((node = dlist_node_create(data)) == NULL)  
  73.     {  
  74.         return DLIST_RETURN_FAIL;   
  75.     }  
  76.   
  77.     if(thiz->head == NULL)  
  78.     {  
  79.         thiz->head = node;  
  80.   
  81.         return DLIST_RETURN_OK;  
  82.     }  
  83.   
  84.     cursor = thiz->head;  
  85.     while(cursor != NULL && cursor->next != NULL)  
  86.     {  
  87.         cursor = cursor->next;  
  88.     }  
  89.   
  90.     cursor->next = node;  
  91.     node->prev = cursor;  
  92.   
  93.     return DLIST_RETURN_OK;  
  94. }  
  95.   
  96. <span style="color:#ff0000;">DListReturn dlist_prepend(DList* thiz, void* data)  
  97. {  
  98.     DListNode* node = NULL;  
  99.     DListNode* cursor = NULL;  
  100.   
  101.     if((node = dlist_node_create(data)) == NULL)  
  102.     {  
  103.         return DLIST_RETURN_FAIL;   
  104.     }  
  105.   
  106.     if(thiz->head == NULL)  
  107.     {  
  108.         thiz->head = node;  
  109.   
  110.         return DLIST_RETURN_OK;  
  111.     }  
  112.   
  113.     cursor = thiz->head;  
  114.   
  115.     if(thiz->head == cursor)  
  116.         thiz->head = node;  
  117.   
  118.     node->next = cursor;  
  119.     cursor->prev = node;  
  120.   
  121.     return DLIST_RETURN_OK;  
  122. }  
  123.   
  124. </span>DListReturn dlist_print(DList* thiz, DListPrintFunction print)  
  125. {  
  126.     DListNode* iter = thiz->head;  
  127.   
  128.     while(iter != NULL)  
  129.     {  
  130.         print(iter->data);  
  131.         iter = iter->next;  
  132.     }  
  133.     printf("\n");  
  134.     return DLIST_RETURN_OK;  
  135. }  
  136.   
  137. DListReturn print_int(void* data)  
  138. {  
  139.     DStu* ss=(DStu*)data;  
  140.     printf("%d\t ", ss->score);  
  141.   
  142.     return DLIST_RETURN_OK;  
  143. }  
  144.   
  145. DListReturn dlist_node_destroy(DListNode* node)  
  146. {  
  147.     if(node != NULL)  
  148.     {  
  149.         node->next = NULL;  
  150.         node->prev = NULL;  
  151.         free(node);  
  152.     }  
  153.   
  154.     return DLIST_RETURN_OK;  
  155. }  
  156.   
  157. DListReturn dlist_destroy(DList* thiz)  
  158. {  
  159.     DListNode* iter = thiz->head;  
  160.     DListNode* next = NULL;  
  161.   
  162.     while(iter != NULL)  
  163.     {  
  164.         next = iter->next;  
  165.         dlist_node_destroy(iter);  
  166.         iter = next;  
  167.     }  
  168.   
  169.     thiz->head = NULL;  
  170.     free(thiz);  
  171.   
  172.     return DLIST_RETURN_OK;  
  173. }  
  174.   
  175. int main(int argc, char* argv[])  
  176. {  
  177.     int i = 0;  
  178.       
  179.     DList* dlist = dlist_head_create();  
  180.   
  181.     for(i = 0; i < 7; i++)  
  182.     {  
  183.         DStu* stu =(DStu*) malloc(sizeof(DStu));  
  184.         stu->score = i;  
  185.         dlist_append(dlist, (void*)stu);  
  186.     }  
  187. <span style="color:#ff0000;"for(i = 0; i < 7; i++)  
  188.     {  
  189.         DStu* stu =(DStu*) malloc(sizeof(DStu));  
  190.         stu->score = i;  
  191.         dlist_prepend(dlist, (void*)stu);  
  192.     }</span>  
  193.     dlist_print(dlist, print_int);  
  194.     dlist_destroy(dlist);  
  195.     return 0;  
  196. }


【福利】填问卷 送2019精选测试大礼包+接口测试实战课程!
54/5<12345>

评 论

论坛新帖

顶部 底部


建议使用IE 6.0以上浏览器,800×600以上分辨率,法律顾问:上海瀛东律师事务所 张楠律师
版权所有 上海博为峰软件技术股份有限公司 Copyright©51testing.com 2003-2019, 沪ICP备05003035号
投诉及意见反馈:webmaster@51testing.com; 业务联系:service@51testing.com 021-64471599-8017

沪公网安备 31010102002173号

51Testing官方微信

51Testing官方微博

扫一扫 测试知识全知道