您好,登錄后才能下訂單哦!
本文小編為大家詳細(xì)介紹“C++雙向循環(huán)鏈表類模版實(shí)例代碼分析”,內(nèi)容詳細(xì),步驟清晰,細(xì)節(jié)處理妥當(dāng),希望這篇“C++雙向循環(huán)鏈表類模版實(shí)例代碼分析”文章能幫助大家解決疑惑,下面跟著小編的思路慢慢深入,一起來(lái)學(xué)習(xí)新知識(shí)吧。
如下圖所示:
對(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; }
在構(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; }
因?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; // 開(kāi)頭節(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; }
由于現(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; }
#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; // 開(kāi)頭節(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(表頭)開(kāi)始.如果未找到則返回-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_H
測(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; }
讀到這里,這篇“C++雙向循環(huán)鏈表類模版實(shí)例代碼分析”文章已經(jīng)介紹完畢,想要掌握這篇文章的知識(shí)點(diǎn)還需要大家自己動(dòng)手實(shí)踐使用過(guò)才能領(lǐng)會(huì),如果想了解更多相關(guān)內(nèi)容的文章,歡迎關(guān)注億速云行業(yè)資訊頻道。
免責(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)容。