新聞中心

        EEPW首頁 > 嵌入式系統(tǒng) > 牛人業(yè)話 > C語言的那些小秘密之鏈表(二)

        C語言的那些小秘密之鏈表(二)

        作者: 時間:2015-04-11 來源:網(wǎng)絡(luò) 收藏

          雖然兩個指針變量存放在不同的單元,但是它們都指向同一個存儲單元,所以在釋放的時候只能釋放一次,如果釋放兩次就會出錯。注意切不可同時使用free(pointer_1);和 free(pointer_2);,否則將會出現(xiàn)內(nèi)存錯誤。對出錯原因不懂的可以參考我前面的文章《的那些小秘密之指針》。這個代碼和我們雙的最大區(qū)別是它只能使用free()函數(shù)進(jìn)行一次釋放,而我們雙中看似使用了兩次,但實則一次而已。原因就在于我們在對頭結(jié)點分配空間的時候分配的是存放data指針變量的存儲空間(注意:所有的指針變量在分配的時候均分配4字節(jié)大小的存儲空間,如果有什么疑惑可以參考我之前寫的《的那些小秘密之指針》)。所以釋放的時候也是釋放的存放指針變量data的存儲空間,并沒有釋放掉指針變量data所指向的存儲空間。所以在釋放data指向的存儲空間時,我們只需要在main()函數(shù)中對stu[i]所指向的存儲空間使用free()函數(shù)即可,因為data和它指向的是同一個存儲空間。

        本文引用地址:http://www.104case.com/article/272382.htm

          接下來我們來添加一個在頭結(jié)點添加結(jié)點的模塊。

          #include

          #include

          typedef enum _DListReturn

          {

          DLIST_RETURN_OK,

          DLIST_RETURN_FAIL

          }DListReturn;

          typedef struct _DStu

          {

          int score;

          }DStu;

          typedef struct _DListNode

          {

          struct _DListNode* prev;

          struct _DListNode* next;

          DStu* data;

          }DListNode;

          typedef struct _DList

          {

          DListNode* head;

          }DList;

          typedef DListReturn (*DListPrintFunction)(void* data);

          DListNode* dlist_node_create(void* data)

          {

          DListNode* node;

          if((node = (DListNode*) malloc(sizeof(DListNode)))==NULL)

          {

          printf("分配空間失敗!");

          exit(0);

          }

          if(node != NULL)

          {

          node->prev = NULL;

          node->next = NULL;

          node->data =(DStu*)data;

          }

          return node;

          }

          DList* dlist_head_create(void)

          {

          DList* thiz;

          if((thiz = (DList*)malloc(sizeof(DList)))==NULL)

          {

          printf("分配空間失敗!");

          exit(0);

          }

          if(thiz != NULL)

          {

          thiz->head = NULL;

          }

          return thiz;

          }

          DListReturn dlist_append(DList* thiz, void* data)

          {

          DListNode* node = NULL;

          DListNode* cursor = NULL;

          if((node = dlist_node_create(data)) == NULL)

          {

          return DLIST_RETURN_FAIL;

          }

          if(thiz->head == NULL)

          {

          thiz->head = node;

          return DLIST_RETURN_OK;

          }

          cursor = thiz->head;

          while(cursor != NULL && cursor->next != NULL)

          {

          cursor = cursor->next;

          }

          cursor->next = node;

          node->prev = cursor;

          return DLIST_RETURN_OK;

          }

          DListReturn dlist_prepend(DList* thiz, void* data)

          {

          DListNode* node = NULL;

          DListNode* cursor = NULL;

          if((node = dlist_node_create(data)) == NULL)

          {

          return DLIST_RETURN_FAIL;

          }

          if(thiz->head == NULL)

          {

          thiz->head = node;

          return DLIST_RETURN_OK;

          }

          cursor = thiz->head;

          if(thiz->head == cursor)

          thiz->head = node;

          node->next = cursor;

          cursor->prev = node;

          return DLIST_RETURN_OK;

          }

          DListReturn dlist_print(DList* thiz, DListPrintFunction print)

          {

          DListNode* iter = thiz->head;

          while(iter != NULL)

          {

          print(iter->data);

          iter = iter->next;

          }

          printf("n");

          return DLIST_RETURN_OK;

          }

          DListReturn print_int(void* data)

          {

          DStu* ss=(DStu*)data;

          printf("%dt ", ss->score);

          return DLIST_RETURN_OK;

          }

          DListReturn dlist_node_destroy(DListNode* node)

          {

          if(node != NULL)

          {

          node->next = NULL;

          node->prev = NULL;

          free(node);

          }

          return DLIST_RETURN_OK;

          }

          DListReturn dlist_destroy(DList* thiz)

          {

          DListNode* iter = thiz->head;

          DListNode* next = NULL;

          while(iter != NULL)

          {

          next = iter->next;

          dlist_node_destroy(iter);

          iter = next;

          }

          thiz->head = NULL;

          free(thiz);

          return DLIST_RETURN_OK;

          }

          int main(int argc, char* argv[])

          {

          int i = 0;

          DList* dlist = dlist_head_create();

          for(i = 0; i < 7; i++)

          {

          DStu* stu =(DStu*) malloc(sizeof(DStu));

          stu->score = i;

          dlist_append(dlist, (void*)stu);

          }

          for(i = 0; i < 7; i++)

          {

          DStu* stu =(DStu*) malloc(sizeof(DStu));

          stu->score = i;

          dlist_prepend(dlist, (void*)stu);

          }

          dlist_print(dlist, print_int);

          dlist_destroy(dlist);

          return 0;

          }

          我們在上一段代碼添加了紅色部分代碼后,使得其在已經(jīng)生成的雙的頭結(jié)點處添加結(jié)點,運行結(jié)果如下:

          

         

          6 5 4 3 2 1 0 0 1 2

          3 4 5 6

          Press any key to continue

          可以看出結(jié)果與我們的要求完全符合。如果你認(rèn)真分析了代碼的話你就會發(fā)現(xiàn),我們的兩種添加方式中有公共代碼出現(xiàn),那就說明我們的代碼可以繼續(xù)改進(jìn),把公共代碼放到一個函數(shù)中,讀者可以另外編寫一個函數(shù)來實現(xiàn),使得我們編寫的代碼得到充分的利用。

          篇幅似乎有些過長了,接下來我就不再一一講解了,我在這里只是想起一個引路的作用,讀者完全可以在此基礎(chǔ)之上繼續(xù)編寫雙鏈表其余部分的功能,其他的功能模塊讀者可以在此基礎(chǔ)上一一添加上去,到下一篇的時候我們將走進(jìn)linux內(nèi)核鏈表,繼續(xù)鏈表之旅的最后一站。由于本人水平有限,博客中的不妥或錯誤之處在所難免,殷切希望讀者批評指正。同時也歡迎讀者共同探討相關(guān)的內(nèi)容,如果樂意交流的話請留下你寶貴的意見。


        上一頁 1 2 3 下一頁

        關(guān)鍵詞: C語言 鏈表

        評論


        相關(guān)推薦

        技術(shù)專區(qū)

        關(guān)閉
        主站蜘蛛池模板: 崇左市| 肇州县| 东山县| 四川省| 扎囊县| 上虞市| 迁西县| 新蔡县| 靖州| 锡林浩特市| 眉山市| 称多县| 鄂温| 舒城县| 郸城县| 莱芜市| 柳州市| 聂拉木县| 长春市| 洮南市| 鸡东县| 海淀区| 台中市| 武平县| 永吉县| 阿荣旗| 广宗县| 金秀| 黄陵县| 渝北区| 都匀市| 铜陵市| 昔阳县| 正安县| 巴林左旗| 资中县| 肇州县| 长沙县| 水城县| 石嘴山市| 平和县|