C++ 模版雙向鏈表的實(shí)現(xiàn)詳解
更新時(shí)間:2013年05月31日 10:27:30 作者:
本篇文章是對(duì)C++中的模版雙向鏈表進(jìn)行了詳細(xì)的分析介紹,需要的朋友參考下
代碼如下所示:
#include <iostream>
template <typename T>
class double_linked
{
struct node
{
T data;
node* prev;
node* next;
node(T t, node* p, node* n) : data(t), prev(p), next(n) {}
};
node* head;
node* tail;
public:
double_linked() : head( NULL ), tail ( NULL ) {}
template<int N>
double_linked( T (&arr) [N]) : head( NULL ), tail ( NULL )
{
for( int i(0); i != N; ++i)
push_back(arr[i]);
}
bool empty() const { return ( !head || !tail ); }
operator bool() const { return !empty(); }
void push_back(T);
void push_front(T);
T pop_back();
T pop_front();
~double_linked()
{
while(head)
{
node* temp(head);
head=head->next;
delete temp;
}
}
};
template <typename T>
void double_linked<T>::push_back(T data)
{
tail = new node(data, tail, NULL);
if( tail->prev )
tail->prev->next = tail;
if( empty() )
head = tail;
}
template <typename T>
void double_linked<T>::push_front(T data)
{
head = new node(data, NULL, head);
if( head->next )
head->next->prev = head;
if( empty() )
tail = head;
}
template<typename T>
T double_linked<T>::pop_back()
{
if( empty() )
throw("double_linked : list empty");
node* temp(tail);
T data( tail->data );
tail = tail->prev ;
if( tail )
tail->next = NULL;
else
head = NULL ;
delete temp;
return data;
}
template<typename T>
T double_linked<T>::pop_front()
{
if( empty() )
throw("double_linked : list empty");
node* temp(head);
T data( head->data );
head = head->next ;
if( head )
head->prev = NULL;
else
tail = NULL;
delete temp;
return data;
}
int main()
{
int arr[] = { 4, 6, 8, 32, 19 } ;
double_linked<int> dlist ( arr );
dlist.push_back( 11 );
dlist.push_front( 100 );
while( dlist )
std::cout << dlist.pop_back() << " ";
}
復(fù)制代碼 代碼如下:
#include <iostream>
template <typename T>
class double_linked
{
struct node
{
T data;
node* prev;
node* next;
node(T t, node* p, node* n) : data(t), prev(p), next(n) {}
};
node* head;
node* tail;
public:
double_linked() : head( NULL ), tail ( NULL ) {}
template<int N>
double_linked( T (&arr) [N]) : head( NULL ), tail ( NULL )
{
for( int i(0); i != N; ++i)
push_back(arr[i]);
}
bool empty() const { return ( !head || !tail ); }
operator bool() const { return !empty(); }
void push_back(T);
void push_front(T);
T pop_back();
T pop_front();
~double_linked()
{
while(head)
{
node* temp(head);
head=head->next;
delete temp;
}
}
};
template <typename T>
void double_linked<T>::push_back(T data)
{
tail = new node(data, tail, NULL);
if( tail->prev )
tail->prev->next = tail;
if( empty() )
head = tail;
}
template <typename T>
void double_linked<T>::push_front(T data)
{
head = new node(data, NULL, head);
if( head->next )
head->next->prev = head;
if( empty() )
tail = head;
}
template<typename T>
T double_linked<T>::pop_back()
{
if( empty() )
throw("double_linked : list empty");
node* temp(tail);
T data( tail->data );
tail = tail->prev ;
if( tail )
tail->next = NULL;
else
head = NULL ;
delete temp;
return data;
}
template<typename T>
T double_linked<T>::pop_front()
{
if( empty() )
throw("double_linked : list empty");
node* temp(head);
T data( head->data );
head = head->next ;
if( head )
head->prev = NULL;
else
tail = NULL;
delete temp;
return data;
}
int main()
{
int arr[] = { 4, 6, 8, 32, 19 } ;
double_linked<int> dlist ( arr );
dlist.push_back( 11 );
dlist.push_front( 100 );
while( dlist )
std::cout << dlist.pop_back() << " ";
}
您可能感興趣的文章:
- c++雙向鏈表操作示例(創(chuàng)建雙向鏈、雙向鏈表中查找數(shù)據(jù)、插入數(shù)據(jù)等)
- C++ 構(gòu)造雙向鏈表的實(shí)現(xiàn)代碼
- 關(guān)于雙向鏈表的增刪改查和排序的C++實(shí)現(xiàn)
- C++ STL入門教程(2) list雙向鏈表使用方法(附程序代碼)
- C++ 實(shí)現(xiàn)雙向鏈表的實(shí)例
- C++將二叉樹轉(zhuǎn)為雙向鏈表及判斷兩個(gè)鏈表是否相交
- 深入解析C++的循環(huán)鏈表與雙向鏈表設(shè)計(jì)的API實(shí)現(xiàn)
- C++雙向鏈表實(shí)現(xiàn)簡單通訊錄
- C++實(shí)現(xiàn)雙向鏈表
- C++數(shù)據(jù)結(jié)構(gòu)之雙向鏈表
相關(guān)文章
C++實(shí)現(xiàn)LeetCode(24.成對(duì)交換節(jié)點(diǎn))
這篇文章主要介紹了C++實(shí)現(xiàn)LeetCode(24.成對(duì)交換節(jié)點(diǎn)),本篇文章通過簡要的案例,講解了該項(xiàng)技術(shù)的了解與使用,以下就是詳細(xì)內(nèi)容,需要的朋友可以參考下2021-07-07C++無鎖數(shù)據(jù)結(jié)構(gòu)實(shí)現(xiàn)示例詳解
這篇文章主要為大家介紹了C++無鎖數(shù)據(jù)結(jié)構(gòu)實(shí)現(xiàn)示例詳解,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步,早日升職加薪2023-12-12C/C++ Crypto密碼庫調(diào)用的實(shí)現(xiàn)方法
Crypto 庫是C/C++的加密算法庫,這個(gè)加密庫很流行,基本上涵蓋了市面上的各類加密解密算法,感興趣的可以參考一下2021-06-06VC實(shí)現(xiàn)A進(jìn)程窗口嵌入到B進(jìn)程窗口中顯示的方法
這篇文章主要介紹了VC實(shí)現(xiàn)A進(jìn)程窗口嵌入到B進(jìn)程窗口中顯示的方法,對(duì)于理解windows程序運(yùn)行原理的進(jìn)程問題有一定的幫助,需要的朋友可以參考下2014-07-07C++中的四個(gè)默認(rèn)成員函數(shù)與運(yùn)算符重載詳解
這篇文章主要給大家介紹了關(guān)于C++中四個(gè)默認(rèn)成員函數(shù)與運(yùn)算符重載的相關(guān)資料,文中通過示例代碼介紹的非常詳細(xì),對(duì)大家具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面來跟著小編一起學(xué)習(xí)學(xué)習(xí)吧。2017-08-08數(shù)據(jù)結(jié)構(gòu)串的操作實(shí)例詳解
這篇文章主要介紹了數(shù)據(jù)結(jié)構(gòu)串的操作實(shí)例詳解的相關(guān)資料,需要的朋友可以參考下2017-07-07C++函數(shù)的嵌套調(diào)用和遞歸調(diào)用學(xué)習(xí)教程
這篇文章主要介紹了C++函數(shù)的嵌套調(diào)用和遞歸調(diào)用學(xué)習(xí)教程,是C++入門學(xué)習(xí)中的基礎(chǔ)知識(shí),需要的朋友可以參考下2015-09-09