C++實現通用雙向連結串列
使用C++完成雙向通用連結串列
雙向連結串列不用多說,通用連結串列因為資料結構不確定的,使用一個VOID指標指向資料,
什麼資料都可以掛上去,這樣來封裝連結串列,可以作為基礎類也可以單獨使用,
這裡只是為了練習C++封裝的語法,實現了簡單的增加和刪除連結串列由於實際資料
型別不能確定,列印連結串列資料使用公有函式來完成,完成了正向列印反向列印,
演示了資料型別為簡單的int型別也演示了資料型別為class型別。
程式碼如下:
記憶體洩露檢測:
==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)
雙向連結串列不用多說,通用連結串列因為資料結構不確定的,使用一個VOID指標指向資料,
什麼資料都可以掛上去,這樣來封裝連結串列,可以作為基礎類也可以單獨使用,
這裡只是為了練習C++封裝的語法,實現了簡單的增加和刪除連結串列由於實際資料
型別不能確定,列印連結串列資料使用公有函式來完成,完成了正向列印反向列印,
演示了資料型別為簡單的int型別也演示了資料型別為class型別。
程式碼如下:
點選(此處)摺疊或開啟
-
連結串列實現:
-
#include<iostream>
-
#include<stdlib.h>
-
using namespace std;
-
/* data資料型別進行void封裝,為通用連結串列
-
* node為節點的基本資料結構
-
* addnode使用void資料進行連線到連結串列中,造成連結串列
-
* frist_node為第一個結點位置,開放訪問
-
* last_node為最後一個節點位置,開放訪問
-
* length為節點長度,開放訪問
-
* 只是完成增加節點和釋放節點功能,其他功能也相應簡單,用到再加,列印功能由於
-
* 資料型別不確定無法完成。
-
*/
-
#ifndef _CHAIN_
-
#define _CHAIN_
-
struct node
-
{
-
void* data;
-
node* next;
-
node* priv;
-
unsigned int num;
-
node()
-
{
-
data = NULL;
-
next = NULL;
-
priv = NULL;
-
num = 0;
-
}
-
};
-
-
-
class my_chain
-
{
-
public:
-
my_chain()
-
{
-
-
this->frist_node = NULL;
-
this->length = 0;
-
this->last_node = NULL;
-
}
-
//-1 data is null;
-
// 0 normal
-
// 傳入一個void指標的資料型別,連結串列增加一個節點
-
int addnode(void* data)
-
{
-
ret = 0 ;
-
-
if(data == NULL)
-
{
-
ret = -1;
-
return ret;
-
}
-
-
node* c_node = new node; //分配節點記憶體
-
-
if(this->frist_node == NULL)
-
{
-
-
this->frist_node = c_node;
-
}
-
if(this->last_node == NULL)
-
{
-
c_node->next = NULL;
-
c_node->priv = NULL;
-
c_node->data = data;
-
}
-
else
-
{
-
c_node->next = NULL;
-
c_node->priv = this->last_node;
-
this->last_node->next = c_node;
-
c_node->data = data;
-
}
-
this->last_node = c_node;
-
this->length++;
-
c_node->num = this->length;
-
return ret;
-
}
-
//ret=1 null list;
-
//ret=0 normal list;
-
//釋放整個連結串列記憶體
-
int freechain()
-
{
-
ret = 0;
-
if(this->last_node == NULL)
-
{
-
ret = 1;
-
cout<<"null list"<<endl;
-
return ret;
-
}
-
node* node_my = this->frist_node;
-
while(node_my != NULL)
-
{
-
#ifdef DEBUG
-
cout<<"free node num:"<< node_my->num<<endl;
-
#endif
-
node* temp = node_my;
-
node_my = node_my->next;
-
free(temp->data);//刪除節點資料記憶體?跨函式free
-
delete temp;//刪除節點node記憶體
-
}
-
}
-
//....
-
int delnode() //未實現
-
{
-
ret = 0;
-
return ret;
-
}
-
-
int addmodnode(unsigned int loc)//未實現
-
{
-
ret = 0;
-
return ret;
-
}
-
//.....
-
-
public:
-
node* frist_node;//用於外部訪問
-
unsigned int length;//用於外部訪問
-
node* last_node;//用於外部訪問
-
private:
-
int ret;
-
};
- #endif
點選(此處)摺疊或開啟
-
測試用例:
-
#include<iostream>
-
#define DEBUG
-
#include"chain.h"
-
using namespace std;
-
//測試類
-
class cube
-
{
-
public:
-
cube(int a,int b,int c):a(a),b(b),c(c)
-
{
-
;
-
}
-
int get_size() const
-
{
-
return a*b*c;
-
}
-
private:
-
int a;
-
int b;
-
int c;
-
};
-
//完成列印操作
-
int printchain(my_chain* c_header)
-
{
-
if(c_header->frist_node == NULL)
-
{
-
cout<<"NULL chain" <<endl;
-
return -1;
-
}
-
node* node_my = c_header->frist_node;
-
cout<<"chain total number:"<<c_header->length<<endl;
-
-
//正向訪問
-
cout<<"正向訪問"<<endl;
-
while(node_my != NULL)
-
{
-
cout<<"node num:"<<node_my->num<<" data is:"<<*((int*)(node_my->data))<<endl;
-
node_my = node_my->next;
-
}
-
-
-
node_my = c_header->last_node;
-
//反向訪問
-
cout<<"反向訪問"<<endl;
-
while(node_my != NULL)
-
{
-
cout<<"node num:"<<node_my->num<<" data is:"<<*((int*)(node_my->data))<<endl;
-
node_my = node_my->priv;
-
}
-
return 0;
-
-
}
-
-
int printchain_cube(my_chain* c_header)
-
{
-
if(c_header->frist_node == NULL)
-
{
-
cout<<"NULL chain" <<endl;
-
return -1;
-
}
-
node* node_my = c_header->frist_node;
-
cout<<"chain total number:"<<c_header->length<<endl;
-
//正向訪問
-
cout<<"正向訪問"<<endl;
-
while(node_my != NULL)
-
{
-
cout<<"node num:"<<node_my->num<<" data is:"<<((cube*)(node_my->data))->get_size()<<endl;
-
node_my = node_my->next;
-
}
-
-
node_my = c_header->last_node;
-
//反向訪問
-
cout<<"反向訪問"<<endl;
-
while(node_my != NULL)
-
{
-
cout<<"node num:"<<node_my->num<<" data is:"<<((cube*)(node_my->data))->get_size()<<endl;
-
node_my = node_my->priv;
-
}
-
-
return 0;
-
-
}
-
-
int main()
-
{
-
cout<<"---int data chain:"<<endl;
-
{ //3個測試int資料
-
my_chain* chain_int = new my_chain;//建立my_chain雙向連結串列頭
-
int i = 0;
-
for(i = 0;i<3;i++)
-
{
-
//最好使用malloc族函式使用free來釋放void型別記憶體
-
int* data = (int*)calloc(1,sizeof(int));
-
//int* data = new int(i);
-
(*chain_int).addnode((void*)data);
-
}
-
printchain(chain_int);
-
#ifdef DEBUG
-
cout<<"釋放記憶體"<<endl;
-
#endif
-
(*chain_int).freechain();
-
delete chain_int;
-
}
-
cout<<"---class data chain:"<<endl;
-
{//5個測試類資料
-
my_chain* chain_cube = new my_chain;//建立my_chain雙向的連結串列頭
-
int i = 0;
-
for(i = 0;i<5;i++)
-
{
-
//cube* data = new cube(i,i,i);
-
cube* data = (cube*)calloc(1,sizeof(cube));
-
(*data)=cube(i,i,i);//預設淺複製,這裡無礙
-
(*chain_cube).addnode((void*)data);
-
}
-
printchain_cube(chain_cube);
-
#ifdef DEBUG
-
cout<<"釋放記憶體"<<endl;
-
#endif
-
(*chain_cube).freechain();
-
delete chain_cube;
-
}
-
- }
點選(此處)摺疊或開啟
-
測試結果:
-
---int data chain:
-
chain total number:3
-
正向訪問
-
node num:1 data is:0
-
node num:2 data is:0
-
node num:3 data is:0
-
反向訪問
-
node num:3 data is:0
-
node num:2 data is:0
-
node num:1 data is:0
-
釋放記憶體
-
free node num:1
-
free node num:2
-
free node num:3
-
---class data chain:
-
chain total number:5
-
正向訪問
-
node num:1 data is:0
-
node num:2 data is:1
-
node num:3 data is:8
-
node num:4 data is:27
-
node num:5 data is:64
-
反向訪問
-
node num:5 data is:64
-
node num:4 data is:27
-
node num:3 data is:8
-
node num:2 data is:1
-
node num:1 data is:0
-
釋放記憶體
-
free node num:1
-
free node num:2
-
free node num:3
-
free node num:4
- free node num:5
==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)
來自 “ ITPUB部落格 ” ,連結:http://blog.itpub.net/7728585/viewspace-2134969/,如需轉載,請註明出處,否則將追究法律責任。
相關文章
- 連結串列-雙向通用連結串列
- 連結串列-雙向非通用連結串列
- 實現雙向連結串列
- Go實現雙向連結串列Go
- java實現雙向連結串列Java
- 連結串列-雙向連結串列
- 雙向連結串列的功能實現(初版
- 雙向連結串列
- 資料結構(雙向連結串列的實現)資料結構
- 資料結構-雙向連結串列(Python實現)資料結構Python
- Redis 原始碼解析之通用雙向連結串列(adlist)Redis原始碼
- 通用雙向連結串列的設計(參考Linux系統中的實現)Linux
- 請使用 js 實現一個雙向連結串列JS
- 資料結構實驗之連結串列九:雙向連結串列資料結構
- 資料結構--陣列、單向連結串列、雙向連結串列資料結構陣列
- go 實現單向連結串列Go
- 資料結構——雙向連結串列資料結構
- 結構與演算法(03):單向連結串列和雙向連結串列演算法
- DoublyLinkedList(雙向連結串列)——Javascript版JavaScript
- c/c++ 線性表之雙向迴圈連結串列C++
- 圖解雙連結串列(Java實現)圖解Java
- 資料結構之雙向連結串列資料結構
- 019 透過連結串列學Rust之雙連結串列實現PeekRust
- 019 通過連結串列學Rust之雙連結串列實現PeekRust
- 【c# .net】雙向連結串列( LinkedList )C#
- 雙向連結串列 尾節點插入
- 資料結構與演算法——連結串列 Linked List(單連結串列、雙向連結串列、單向環形連結串列-Josephu 問題)資料結構演算法
- 連結串列 - 單向連結串列
- 單向迴圈連結串列的實現
- Python實現單向連結串列詳解Python
- 資料結構_連結串列_單向迴圈連結串列 & 雙向連結串列的初始化、插入、刪除、修改、查詢列印(基於C語言實現)資料結構C語言
- 連結串列-單連結串列實現
- 013 透過連結串列學習Rust之實現連結串列的通用函式Rust函式
- 013 通過連結串列學習Rust之實現連結串列的通用函式Rust函式
- 畫江湖之資料結構【第一話:連結串列】雙向連結串列資料結構
- 畫江湖之資料結構 [第一話:連結串列] 雙向連結串列資料結構
- LVGL雙向連結串列學習筆記筆記
- 資料結構 - 單連結串列 C++ 實現資料結構C++
- 雙向連結串列————查詢、刪除、插入結點