C++?雙向循環(huán)鏈表類模版實(shí)例詳解
在上章C++圖解單向鏈表類模板和iterator迭代器類模版詳解
我們學(xué)習(xí)了單鏈表,所以本章來學(xué)習(xí)雙向循環(huán)鏈表
我們在上個(gè)文章代碼上進(jìn)行修改, 由于雙向循環(huán)鏈表在我們之前學(xué)的單鏈表上相對于較為復(fù)雜,所以需要注意的細(xì)節(jié)如下所示.
1.插入某個(gè)節(jié)點(diǎn)流程
如下圖所示:

對應(yīng)代碼如下所示:
/*插入一個(gè)新的節(jié)點(diǎn)*/
bool insert(int i, const T& value)
{
if (!((i>=0) && (i<=m_length))) {
ThrowException("Invalid parameter i to get value ...");
return false;
}
Node* pre = getNode(i-1);
Node* node = new Node(value); // new一個(gè)新節(jié)點(diǎn)
node->next = pre->next; // 將node新節(jié)點(diǎn)的next鏈接到下個(gè)節(jié)點(diǎn)
node->prev = pre; // 將node新節(jié)點(diǎn)的prev鏈接到pre上個(gè)節(jié)點(diǎn)
pre->next->prev = node; // 將下個(gè)節(jié)點(diǎn)的prev鏈接到node新節(jié)點(diǎn)
pre->next = node; // 將上個(gè)節(jié)點(diǎn)的next鏈接到node新節(jié)點(diǎn)
m_length +=1;
return true;
}2.構(gòu)造函數(shù)修改
在構(gòu)造函數(shù)中,需要將頭節(jié)點(diǎn)的next和prev都指向自己,從而實(shí)現(xiàn)一個(gè)閉環(huán)狀態(tài),代碼如下所示:
LinkedList() { m_header.next = &m_header; m_header.prev = &m_header;??? m_length = 0; }3.重新實(shí)現(xiàn)append和prepend函數(shù)
因?yàn)槭莻€(gè)雙向循環(huán)鏈表,所以我們很輕松的就能獲取到表頭節(jié)點(diǎn)和表尾節(jié)點(diǎn),代碼如下所示:
void append(const T &value)
{
Node* node = new Node(value); // new一個(gè)新節(jié)點(diǎn)
node->next = &m_header; // 新節(jié)點(diǎn)的下個(gè)節(jié)點(diǎn)為頭節(jié)點(diǎn)
node->prev = m_header.prev; // 新節(jié)點(diǎn)的上個(gè)節(jié)點(diǎn)為末尾節(jié)點(diǎn)
node->prev->next = node; // 新節(jié)點(diǎn)的上個(gè)節(jié)點(diǎn)的下個(gè)節(jié)點(diǎn)為新節(jié)點(diǎn)
m_header.prev = node; // 開頭節(jié)點(diǎn)的上個(gè)節(jié)點(diǎn)為i
m_length +=1;
}
void prepend(const T &value)
{
Node* node = new Node(value); // new一個(gè)新節(jié)點(diǎn)
node->next = m_header.next; // 新節(jié)點(diǎn)的下個(gè)節(jié)點(diǎn)為頭節(jié)點(diǎn)的next
node->prev = &m_header; // 新節(jié)點(diǎn)的上個(gè)節(jié)點(diǎn)為頭節(jié)點(diǎn)
m_header.next = node; // 設(shè)置頭結(jié)點(diǎn)下個(gè)節(jié)點(diǎn)為node
node->next->prev = node; // 設(shè)置之前的節(jié)點(diǎn)前驅(qū)節(jié)點(diǎn)
m_length +=1;
}4.修改迭代器類
由于現(xiàn)在是循環(huán)雙鏈表,所以每個(gè)節(jié)點(diǎn)的next都是有值的,所以我們需要判斷m_current當(dāng)前指標(biāo)是否等于頭節(jié)點(diǎn),如果等于則表示已經(jīng)到鏈表末尾了.所以代碼如下所示:
bool hasNext() { return (m_current && m_current != list->constHeader()); }由于現(xiàn)在有prev成員,所以需要增加向前遍歷函數(shù):
void toEnd() { m_current = list->constHeader()->prev; }
bool hasPrev() { return (m_current && m_current != list->constHeader()); }
T& previous() { Node *ret = m_current; m_current = m_current->prev; return ret->value; }5.LinkedList.h代碼如下
#ifndef LinkedLIST_H
#define LinkedLIST_H
#include "throw.h"
// throw.h里面定義了一個(gè)ThrowException拋異常的宏,如下所示:
//#include <iostream>
//using namespace std;
//#define ThrowException(errMsg) {cout<<__FILE__<<" LINE"<<__LINE__<<": "<<errMsg<<endl; (throw errMsg);}
/*鏈表節(jié)點(diǎn)類模板*/
template <typename T>
struct LinkedNode
{
inline LinkedNode(){ }
inline LinkedNode(const T &arg): value(arg) { }
LinkedNode *prev; // 前驅(qū)結(jié)點(diǎn)
LinkedNode *next; // 后驅(qū)節(jié)點(diǎn)
T value; // 節(jié)點(diǎn)值
};
/*鏈表類模板*/
template <class T>
class LinkedList
{
protected:
typedef LinkedNode<T> Node;
mutable Node m_header; // 頭節(jié)點(diǎn)
int m_length;
public:
LinkedList() { m_header.next = &m_header; m_header.prev = &m_header; m_length = 0; }
~LinkedList() { clear(); }
int length() {return m_length;}
Node* begin() {return m_header.next;}
inline Node* constHeader() const { return &m_header; }
static bool rangeValid(int i,int len) {return ((i>=0) && (i<len));}
inline bool isEmpty() const { return m_length == 0; }
void append(const T &value)
{
Node* node = new Node(value); // new一個(gè)新節(jié)點(diǎn)
node->next = &m_header; // 新節(jié)點(diǎn)的下個(gè)節(jié)點(diǎn)為頭節(jié)點(diǎn)
node->prev = m_header.prev; // 新節(jié)點(diǎn)的上個(gè)節(jié)點(diǎn)為末尾節(jié)點(diǎn)
node->prev->next = node; // 新節(jié)點(diǎn)的上個(gè)節(jié)點(diǎn)的下個(gè)節(jié)點(diǎn)為新節(jié)點(diǎn)
m_header.prev = node; // 開頭節(jié)點(diǎn)的上個(gè)節(jié)點(diǎn)為i
m_length +=1;
}
void prepend(const T &value)
{
Node* node = new Node(value); // new一個(gè)新節(jié)點(diǎn)
node->next = m_header.next; // 新節(jié)點(diǎn)的下個(gè)節(jié)點(diǎn)為頭節(jié)點(diǎn)的next
node->prev = &m_header; // 新節(jié)點(diǎn)的上個(gè)節(jié)點(diǎn)為頭節(jié)點(diǎn)
m_header.next = node; // 設(shè)置頭結(jié)點(diǎn)下個(gè)節(jié)點(diǎn)為node
node->next->prev = node; // 設(shè)置之前的節(jié)點(diǎn)前驅(qū)節(jié)點(diǎn)
m_length +=1;
}
/*獲取i位置處的節(jié)點(diǎn)*/
Node* getNode(int i)
{
Node* ret = &m_header;
while((i--)>-1) { // 由于有頭節(jié)點(diǎn)所以,i為0時(shí),其實(shí)ret = m_header->n
ret = ret->next;
}
return ret;
}
/*插入一個(gè)新的節(jié)點(diǎn)*/
bool insert(int i, const T& value)
{
if (!((i>=0) && (i<=m_length))) {
ThrowException("Invalid parameter i to get value ...");
return false;
}
Node* pre = getNode(i-1);
Node* node = new Node(value); // new一個(gè)新節(jié)點(diǎn)
node->next = pre->next; // 將node新節(jié)點(diǎn)的next鏈接到下個(gè)節(jié)點(diǎn)
node->prev = pre; // 將node新節(jié)點(diǎn)的prev鏈接到pre上個(gè)節(jié)點(diǎn)
pre->next->prev = node; // 將下個(gè)節(jié)點(diǎn)的prev鏈接到node新節(jié)點(diǎn)
pre->next = node; // 將上個(gè)節(jié)點(diǎn)的next鏈接到node新節(jié)點(diǎn)
m_length +=1;
return true;
}
/*刪除一個(gè)節(jié)點(diǎn)*/
bool remove(int i)
{
if (!rangeValid(i, m_length)) {
ThrowException("Invalid parameter i to get value ...");
return false;
}
Node* pre = getNode(i-1);
Node* current = pre->next; // 獲取要?jiǎng)h除的節(jié)點(diǎn)
pre->next = current->next; // 將上個(gè)節(jié)點(diǎn)的next鏈接到前一個(gè)的next中
current->next->prev = pre; // 將下個(gè)節(jié)點(diǎn)的prev鏈接到pre節(jié)點(diǎn)
delete current; // delete空閑的節(jié)點(diǎn)
m_length -=1;
return true;
}
/*獲取節(jié)點(diǎn)數(shù)據(jù)*/
T get(int i)
{
T ret;
if (!rangeValid(i, m_length)) {
ThrowException("Invalid parameter i to get value ...");
} else {
ret = getNode(i)->value;
}
return ret;
}
/*設(shè)置節(jié)點(diǎn)*/
bool set(int i, const T& value)
{
if (!rangeValid(i, m_length)) {
ThrowException("Invalid parameter i to get value ...");
return false;
}
getNode(i)->value = value;
return true;
}
void clear()
{
while(m_length > 0) {
remove(0);
}
}
LinkedList<T>& operator << (const T& value)
{
append(value);
return *this;
}
/*在鏈表中向前查找value所在的索引號.默認(rèn)從from索引號0(表頭)開始.如果未找到則返回-1.*/
int indexOf(const T &value, int from =0)
{
int ret = 0;
Node* node = m_header.next;
while(node) {
if (ret >= from && node->value == value) {
return ret;
}
node = node->next;
ret+=1;
}
return -1;
}
};
/*鏈表迭代器類模板*/
template <class T>
class LinkedListIterator
{
typedef LinkedNode<T> Node;
LinkedList<T> *list;
Node *m_current; // 當(dāng)前指標(biāo)
public:
explicit LinkedListIterator(LinkedList<T> &l):list(&l) { m_current = l.begin(); }
void toBegin() { m_current = list->begin(); }
void toEnd() { m_current = list->constHeader()->prev; }
bool hasHeader() { return (m_current && m_current == list->constHeader()); }
bool hasNext() { return (m_current && m_current != list->constHeader()); }
T& next() { Node *ret = m_current; m_current = m_current->next; return ret->value; }
bool hasPrev() { return (m_current && m_current != list->constHeader()); }
T& previous() { Node *ret = m_current; m_current = m_current->prev; return ret->value; }
T& value()
{
if (m_current == nullptr) {
ThrowException(" Current value is empty ...");
}
return m_current->value;
}
T& move(int i) {
if (!list->rangeValid(i, list->length())) {
ThrowException("Invalid parameter i to get value ...");
}
m_current = list->getNode(i);
return value();
}
};
#endif // LinkedLIST_H6.測試運(yùn)行
測試代碼如下所示:
LinkedList<int> list;
for(int i = 0; i< 5; i++)
list.append(i);
LinkedListIterator<int> it(list);
cout<<"list.length:"<<list.length()<<endl;
// 向后遍歷
it.toBegin();
while (it.hasNext())
cout<<"next:"<<it.next()<<endl;
cout<<endl;
// 向前遍歷
it.toEnd(); // 將指標(biāo)移動(dòng)到尾結(jié)點(diǎn)
while (it.hasPrev())
cout<<"previous:"<<it.previous()<<endl;運(yùn)行打印:

while循環(huán)打印30次,代碼如下所示:
it.toBegin();
int i = 30;
while(i--) {
if (it.hasHeader()) it.next(); // 如果到頭結(jié)點(diǎn),需要舍棄掉
cout<<"i:"<<i<<" value:"<<it.next()<<endl;
}總結(jié)
本篇文章就到這里了,希望能夠給你帶來幫助,也希望您能夠多多關(guān)注腳本之家的更多內(nèi)容!
相關(guān)文章
C++11中std::declval的實(shí)現(xiàn)機(jī)制淺析
這篇文章主要給大家介紹了關(guān)于C++11中std::declval實(shí)現(xiàn)機(jī)制的相關(guān)資料,文中通過示例代碼介紹的非常詳細(xì),對大家具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面來一起看看吧。2017-07-07
基于Windows C++ 應(yīng)用程序通用日志組件的使用詳解
眾所周知,在調(diào)試、跟蹤和執(zhí)行應(yīng)用程序的過程中,程序的日志能為這些工作提供大量有價(jià)值的運(yùn)行信息。因此,程序的日志對應(yīng)用程序的運(yùn)行、維護(hù)至關(guān)重要2013-05-05
C++一個(gè)數(shù)組賦值給另一個(gè)數(shù)組方式
文章介紹了三種在C++中將一個(gè)數(shù)組賦值給另一個(gè)數(shù)組的方法:使用循環(huán)逐個(gè)元素賦值、使用標(biāo)準(zhǔn)庫函數(shù)std::copy或std::memcpy以及使用標(biāo)準(zhǔn)庫容器,每種方法都有其適用的場景和注意事項(xiàng)2025-02-02
C++實(shí)現(xiàn)高并發(fā)異步定時(shí)器
這篇文章主要為大家詳細(xì)介紹了如何利用C++實(shí)現(xiàn)高并發(fā)異步定時(shí)器,文中的示例代碼講解詳細(xì),具有一定的學(xué)習(xí)價(jià)值,感興趣的小伙伴可以跟隨小編一起學(xué)習(xí)一下2023-11-11

