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

對(duì)應(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所在的索引號(hào).默認(rèn)從from索引號(hào)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.測(cè)試運(yùn)行
測(cè)試代碼如下所示:
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é)
本篇文章就到這里了,希望能夠給你帶來(lái)幫助,也希望您能夠多多關(guān)注腳本之家的更多內(nèi)容!
相關(guān)文章
C++連接mysql數(shù)據(jù)庫(kù)的兩種方法小結(jié)
這篇文章主要介紹了C++連接mysql數(shù)據(jù)庫(kù)的兩種方法小結(jié),文中通過示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來(lái)一起學(xué)習(xí)學(xué)習(xí)吧2020-04-04
c++調(diào)用實(shí)現(xiàn)yolov5轉(zhuǎn)onnx介紹
大家好,本篇文章主要講的是c++調(diào)用實(shí)現(xiàn)yolov5轉(zhuǎn)onnx介紹,感興趣的同學(xué)趕快來(lái)看一看吧,對(duì)你有幫助的話記得收藏一下,方便下次瀏覽2021-12-12
淺談使用Rapidxml 庫(kù)遇到的問題和分析過程(分享)
下面小編就為大家?guī)?lái)一篇淺談使用Rapidxml 庫(kù)遇到的問題和分析過程(分享)。小編覺得挺不錯(cuò)的,現(xiàn)在就分享給大家,也給大家做個(gè)參考。一起跟隨小編過來(lái)看看吧2017-05-05
Qt 使用QDialog實(shí)現(xiàn)界面遮罩的示例(蒙版)
界面遮罩在很多時(shí)候都可以用到,例如彈窗,本文主要介紹了Qt 使用QDialog實(shí)現(xiàn)界面遮罩的示例,文中通過示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來(lái)一起學(xué)習(xí)學(xué)習(xí)吧2022-04-04
Qt數(shù)據(jù)庫(kù)相關(guān)應(yīng)用開發(fā)總結(jié)
這篇文章主要為大家介紹了在Qt數(shù)據(jù)庫(kù)應(yīng)用開發(fā)中的一些經(jīng)驗(yàn)總結(jié),以及一些組件的使用介紹。文中的示例代碼講解詳細(xì),需要的可以參考一下2022-02-02
C語(yǔ)言中隱藏結(jié)構(gòu)體的細(xì)節(jié)
以筆者粗淺的認(rèn)識(shí),有兩種最常用的方法,可以實(shí)現(xiàn)庫(kù)內(nèi)結(jié)構(gòu)體定義的隱藏:接口函數(shù)形參使用結(jié)構(gòu)體指針,接口函數(shù)形參使用句柄。2017-05-05
C++中String的語(yǔ)法及常用接口的底層實(shí)現(xiàn)詳解
在C語(yǔ)言中,string是一個(gè)標(biāo)準(zhǔn)庫(kù)類(class),用于處理字符串,它提供了一種更高級(jí)、更便捷的字符串操作方式,string 類提供了一系列成員函數(shù)和重載運(yùn)算符,以便于對(duì)字符串進(jìn)行操作和處理,本編文章會(huì)對(duì)C++中的 string 進(jìn)行詳解,希望本篇文章會(huì)對(duì)你有所幫助2023-06-06
C語(yǔ)言中函數(shù)與指針的應(yīng)用總結(jié)
本篇文章是對(duì)C語(yǔ)言中函數(shù)與指針的應(yīng)用進(jìn)行了詳細(xì)的分析介紹,需要的朋友參考下2013-05-05

