溫馨提示×

您好,登錄后才能下訂單哦!

密碼登錄×
登錄注冊(cè)×
其他方式登錄
點(diǎn)擊 登錄注冊(cè) 即表示同意《億速云用戶服務(wù)條款》

C++實(shí)現(xiàn)通用雙向鏈表

發(fā)布時(shí)間:2020-08-04 16:50:27 來(lái)源:ITPUB博客 閱讀:503 作者:gaopengtttt 欄目:互聯(lián)網(wǎng)科技
使用C++完成雙向通用鏈表
雙向鏈表不用多說(shuō),通用鏈表因?yàn)閿?shù)據(jù)結(jié)構(gòu)不確定的,使用一個(gè)VOID指針指向數(shù)據(jù),
什么數(shù)據(jù)都可以掛上去,這樣來(lái)封裝鏈表,可以作為基礎(chǔ)類也可以單獨(dú)使用,
這里只是為了練習(xí)C++封裝的語(yǔ)法,實(shí)現(xiàn)了簡(jiǎn)單的增加和刪除鏈表由于實(shí)際數(shù)據(jù)
類型不能確定,打印鏈表數(shù)據(jù)使用公有函數(shù)來(lái)完成,完成了正向打印反向打印,
演示了數(shù)據(jù)類型為簡(jiǎn)單的int類型也演示了數(shù)據(jù)類型為class類型。
代碼如下:


點(diǎn)擊(此處)折疊或打開(kāi)

  1. 鏈表實(shí)現(xiàn):
  2. #include<iostream>
  3. #include<stdlib.h>
  4. using namespace std;
  5. /* data數(shù)據(jù)類型進(jìn)行void封裝,為通用鏈表
  6.  * node為節(jié)點(diǎn)的基本數(shù)據(jù)結(jié)構(gòu)
  7.  * addnode使用void數(shù)據(jù)進(jìn)行連接到鏈表中,造成鏈表
  8.  * frist_node為第一個(gè)結(jié)點(diǎn)位置,開(kāi)放訪問(wèn)
  9.  * last_node為最后一個(gè)節(jié)點(diǎn)位置,開(kāi)放訪問(wèn)
  10.  * length為節(jié)點(diǎn)長(zhǎng)度,開(kāi)放訪問(wèn)
  11.  * 只是完成增加節(jié)點(diǎn)和釋放節(jié)點(diǎn)功能,其他功能也相應(yīng)簡(jiǎn)單,用到再加,打印功能由于
  12.  * 數(shù)據(jù)類型不確定無(wú)法完成。
  13.  */
  14. #ifndef _CHAIN_
  15. #define _CHAIN_
  16. struct node
  17. {
  18.         void* data;
  19.         node* next;
  20.         node* priv;
  21.         unsigned int num;
  22.         node()
  23.         {
  24.                 data = NULL;
  25.                 next = NULL;
  26.                 priv = NULL;
  27.                 num = 0;
  28.         }
  29. };


  30. class my_chain
  31. {
  32.         public:
  33.                 my_chain()
  34.                 {

  35.                         this->frist_node = NULL;
  36.                         this->length = 0;
  37.                         this->last_node = NULL;
  38.                 }
  39. //-1 data is null;
  40. // 0 normal
  41. // 傳入一個(gè)void指針的數(shù)據(jù)類型,鏈表增加一個(gè)節(jié)點(diǎn)
  42.                 int addnode(void* data)
  43.                 {
  44.                         ret = 0 ;

  45.                         if(data == NULL)
  46.                         {
  47.                                 ret = -1;
  48.                                 return ret;
  49.                         }

  50.                         node* c_node = new node; //分配節(jié)點(diǎn)內(nèi)存

  51.                         if(this->frist_node == NULL)
  52.                         {

  53.                                 this->frist_node = c_node;
  54.                         }
  55.                         if(this->last_node == NULL)
  56.                         {
  57.                                 c_node->next = NULL;
  58.                                 c_node->priv = NULL;
  59.                                 c_node->data = data;
  60.                         }
  61.                         else
  62.                         {
  63.                                 c_node->next = NULL;
  64.                                 c_node->priv = this->last_node;
  65.                                 this->last_node->next = c_node;
  66.                                 c_node->data = data;
  67.                         }
  68.                         this->last_node = c_node;
  69.                         this->length++;
  70.                         c_node->num = this->length;
  71.                         return ret;
  72.                 }
  73. //ret=1 null list;
  74. //ret=0 normal list;
  75. //釋放整個(gè)鏈表內(nèi)存
  76.                 int freechain()
  77.                 {
  78.                         ret = 0;
  79.                         if(this->last_node == NULL)
  80.                         {
  81.                                 ret = 1;
  82.                                 cout<<"null list"<<endl;
  83.                                 return ret;
  84.                         }
  85.                         node* node_my = this->frist_node;
  86.                         while(node_my != NULL)
  87.                         {
  88. #ifdef DEBUG
  89.                                  cout<<"free node num:"<< node_my->num<<endl;
  90. #endif
  91.                                  node* temp = node_my;
  92.                                  node_my = node_my->next;
  93.                                  free(temp->data);//刪除節(jié)點(diǎn)數(shù)據(jù)內(nèi)存?跨函數(shù)free
  94.                                  delete temp;//刪除節(jié)點(diǎn)node內(nèi)存
  95.                         }
  96.                 }
  97. //....
  98.                 int delnode() //未實(shí)現(xiàn)
  99.                 {
  100.                         ret = 0;
  101.                         return ret;
  102.                 }

  103.                 int addmodnode(unsigned int loc)//未實(shí)現(xiàn)
  104.                 {
  105.                         ret = 0;
  106.                         return ret;
  107.                 }
  108. //.....

  109.         public:
  110.                  node* frist_node;//用于外部訪問(wèn)
  111.                  unsigned int length;//用于外部訪問(wèn)
  112.              node* last_node;//用于外部訪問(wèn)
  113.         private:
  114.                  int ret;
  115. };
  116. #endif


點(diǎn)擊(此處)折疊或打開(kāi)

  1. 測(cè)試用例:
  2. #include<iostream>
  3. #define DEBUG
  4. #include"chain.h"
  5. using namespace std;
  6. //測(cè)試類
  7. class cube
  8. {
  9.         public:
  10.                 cube(int a,int b,int c):a(a),b(b),c(c)
  11.         {
  12.                 ;
  13.         }
  14.                 int get_size() const
  15.                 {
  16.                         return a*b*c;
  17.                 }
  18.         private:
  19.                 int a;
  20.                 int b;
  21.                 int c;
  22. };
  23. //完成打印操作
  24. int printchain(my_chain* c_header)
  25. {
  26.         if(c_header->frist_node == NULL)
  27.         {
  28.                 cout<<"NULL chain" <<endl;
  29.                 return -1;
  30.         }
  31.         node* node_my = c_header->frist_node;
  32.    cout<<"chain total number:"<<c_header->length<<endl;

  33. //正向訪問(wèn)
  34. cout<<"正向訪問(wèn)"<<endl;
  35.         while(node_my != NULL)
  36.         {
  37.                 cout<<"node num:"<<node_my->num<<" data is:"<<*((int*)(node_my->data))<<endl;
  38.                 node_my = node_my->next;
  39.         }


  40.         node_my = c_header->last_node;
  41. //反向訪問(wèn)
  42. cout<<"反向訪問(wèn)"<<endl;
  43.         while(node_my != NULL)
  44.         {
  45.                 cout<<"node num:"<<node_my->num<<" data is:"<<*((int*)(node_my->data))<<endl;
  46.                 node_my = node_my->priv;
  47.         }
  48.         return 0;

  49. }

  50. int printchain_cube(my_chain* c_header)
  51. {
  52.         if(c_header->frist_node == NULL)
  53.         {
  54.                 cout<<"NULL chain" <<endl;
  55.                 return -1;
  56.         }
  57.         node* node_my = c_header->frist_node;
  58.         cout<<"chain total number:"<<c_header->length<<endl;
  59. //正向訪問(wèn)
  60. cout<<"正向訪問(wèn)"<<endl;
  61.         while(node_my != NULL)
  62.         {
  63.                 cout<<"node num:"<<node_my->num<<" data is:"<<((cube*)(node_my->data))->get_size()<<endl;
  64.                 node_my = node_my->next;
  65.         }

  66.         node_my = c_header->last_node;
  67. //反向訪問(wèn)
  68. cout<<"反向訪問(wèn)"<<endl;
  69.         while(node_my != NULL)
  70.         {
  71.                 cout<<"node num:"<<node_my->num<<" data is:"<<((cube*)(node_my->data))->get_size()<<endl;
  72.                 node_my = node_my->priv;
  73.         }

  74.         return 0;

  75. }

  76. int main()
  77. {
  78.         cout<<"---int data chain:"<<endl;
  79.         { //3個(gè)測(cè)試int數(shù)據(jù)
  80.                 my_chain* chain_int = new my_chain;//建立my_chain雙向鏈表頭
  81.                 int i = 0;
  82.                 for(i = 0;i<3;i++)
  83.                 {
  84.                         //最好使用malloc族函數(shù)使用free來(lái)釋放void類型內(nèi)存
  85.                         int* data = (int*)calloc(1,sizeof(int));
  86.                         //int* data = new int(i);
  87.                         (*chain_int).addnode((void*)data);
  88.                 }
  89.                 printchain(chain_int);
  90. #ifdef DEBUG
  91.                 cout<<"釋放內(nèi)存"<<endl;
  92. #endif
  93.                 (*chain_int).freechain();
  94.                 delete chain_int;
  95.         }
  96.    cout<<"---class data chain:"<<endl;
  97.         {//5個(gè)測(cè)試類數(shù)據(jù)
  98.                 my_chain* chain_cube = new my_chain;//建立my_chain雙向的鏈表頭
  99.                 int i = 0;
  100.                 for(i = 0;i<5;i++)
  101.                 {
  102.                         //cube* data = new cube(i,i,i);
  103.                         cube* data = (cube*)calloc(1,sizeof(cube));
  104.                         (*data)=cube(i,i,i);//默認(rèn)淺拷貝,這里無(wú)礙
  105.                         (*chain_cube).addnode((void*)data);
  106.                 }
  107.                 printchain_cube(chain_cube);
  108. #ifdef DEBUG
  109.                 cout<<"釋放內(nèi)存"<<endl;
  110. #endif
  111.                 (*chain_cube).freechain();
  112.                 delete chain_cube;
  113.         }

  114. }


點(diǎn)擊(此處)折疊或打開(kāi)

  1. 測(cè)試結(jié)果:
  2. ---int data chain:
  3. chain total number:3
  4. 正向訪問(wèn)
  5. node num:1 data is:0
  6. node num:2 data is:0
  7. node num:3 data is:0
  8. 反向訪問(wèn)
  9. node num:3 data is:0
  10. node num:2 data is:0
  11. node num:1 data is:0
  12. 釋放內(nèi)存
  13. free node num:1
  14. free node num:2
  15. free node num:3
  16. ---class data chain:
  17. chain total number:5
  18. 正向訪問(wèn)
  19. node num:1 data is:0
  20. node num:2 data is:1
  21. node num:3 data is:8
  22. node num:4 data is:27
  23. node num:5 data is:64
  24. 反向訪問(wèn)
  25. node num:5 data is:64
  26. node num:4 data is:27
  27. node num:3 data is:8
  28. node num:2 data is:1
  29. node num:1 data is:0
  30. 釋放內(nèi)存
  31. free node num:1
  32. free node num:2
  33. free node num:3
  34. free node num:4
  35. free node num:5
內(nèi)存泄露檢測(cè):
==4624== 
==4624== HEAP SUMMARY:
==4624==     in use at exit: 0 bytes in 0 blocks
==4624==   total heap usage: 18 allocs, 18 frees, 392 bytes allocated
==4624== 
==4624== All heap blocks were freed -- no leaks are possible
==4624== 
==4624== ERROR SUMMARY: 0 errors from 0 contexts (suppressed: 0 from 0)
==4624== ERROR SUMMARY: 0 errors from 0 contexts (suppressed: 0 from 0)


向AI問(wèn)一下細(xì)節(jié)

免責(zé)聲明:本站發(fā)布的內(nèi)容(圖片、視頻和文字)以原創(chuàng)、轉(zhuǎn)載和分享為主,文章觀點(diǎn)不代表本網(wǎng)站立場(chǎng),如果涉及侵權(quán)請(qǐng)聯(lián)系站長(zhǎng)郵箱:is@yisu.com進(jìn)行舉報(bào),并提供相關(guān)證據(jù),一經(jīng)查實(shí),將立刻刪除涉嫌侵權(quán)內(nèi)容。

AI