實現雙向鏈表

來源:互聯網
上載者:User

    進入公司的第一個培訓項目,就是用C語言實現雙向鏈表.這是一個比較基礎的,又是很常用的一個結構.在GTK+中,它會給你提供一個現成的.

    以前在大學的時候,鏈表的理論知識經過資料結構的修鍊之後,可謂是比較熟悉的,可是沒有自己實現過,當老大給我布置這麼一個任務的時候,我還是比較茫然的.因為自己太是菜鳥了.今天,我把這個鏈表寫一下,回味和複習.

    首先,你要明確鏈表要實現什麼功能,一般就是插入、追加、遍曆、刪除、銷毀等。然後就可以根據這麼功能去實現。而且一個鏈表當中,不但可以插入數位結點,還可以插入字串的結點。下面通過代碼來實現這麼功能。

    

  1. dlist.h
  2. #ifndef _DLIST_H
  3. #define _DLIST_H
  4. typedef void* value_type
  5. typedef void (*ForeachFunc)(void* data, void* ctx);
  6. typedef void (*NodeDestroyFunc)(void* data, void* destroy_ctx);
  7. typedef struct _DList DList;
  8. typdef enum _Ret
  9. {
  10.  RET_FALSE,
  11.  RET_TRUE
  12. }Ret;
  13. DList*   dlist_create();
  14. Ret        dlist_append(DList* thiz, value_type data);
  15. Ret        dlist_insert(DList* thiz, size_t index, value_type data);
  16. Ret        dlist_delete(DList* thiz, size_t index, NodeDestroy node_destroy, void* ctx);
  17. Ret        dlist_destroy(DList* thiz, NodeDestroy node_destroy, void* ctx);
  18. void       dlist_foreach(DList* thiz, ForeachFunc print, void* ctx);
  19. endif /*_DList_H*/

下面是dlist.c的具體實現。

 

  1. dlist.c 
  2. #include "dlist.h" 
  3. #incluee <stdlib.h> 
  4. #include <stdio.h> 
  5. #include <assert.h> 
  6. typedef struct _Node
  7. {
  8.   value_type data;
  9.   struct _Node* next;
  10.   struct _Node* prev;
  11. }Node;
  12. struct _DList
  13. {
  14.   Node* first;
  15.   NodeDestroy node_destroy;
  16. };
  17. DList* dlist_create(NodeDestroy node_destroy)
  18. {
  19.   DList* thiz = (DList*)malloc(sizeof(DList));
  20.   thiz->node_destroy = node_destroy;
  21.   thiz->first = NULL;
  22.   return thiz;
  23. }
  24. static Node* dlist_find(DList* thiz, size_t index)
  25. {
  26.   assert(thiz != NULL);
  27.   Node* iter = thiz->first;
  28.   
  29.   while(index > 0)
  30.   {
  31.     iter = iter->next;
  32.     index--;
  33.   }
  34.   
  35.   return iter;
  36. Ret dlist_append(DList* thiz, value_type)
  37. {
  38.   assert(thiz != NULL);
  39.   Node* node = (Node* )malloc(sizeof(Node));
  40.   node->next = NULL;
  41.   node->prev = NULL;
  42.   if(thiz->first == NULL)
  43.   {
  44.      thiz->first = node;
  45.   }
  46.   else
  47.   {
  48.      Node* iter = thiz->first;
  49.      while(iter->next != NULL)
  50.      {
  51.         iter = iter->next;
  52.      }
  53.      iter->next = node;
  54.      node->prev = iter;
  55.   }
  56.   
  57.   return RET_TRUE;
  58. }
  59. Ret       dlist_insert(DList* thiz, size_t index, value_type data);
  60. Ret       dlist_delete(DList* thiz, size_t index, NodeDestroy node_destroy, void* ctx);
  61. Ret       dlist_destroy(DList* thiz, NodeDestroy node_destroy, void* ctx);
  62. void      dlist_foreach(DList* thiz, ForeachFunc print, void* ctx);

由於要寫的代碼比較多,所以沒有一一寫出來,只是實現一部分。不過思路就是這樣咯。

 

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.