C++鏈表倒序?qū)崿F(xiàn)方法
本文通過一個(gè)實(shí)例展示了C++實(shí)現(xiàn)鏈表倒序的方法,對于C++數(shù)據(jù)結(jié)構(gòu)的學(xué)習(xí)有很好的參考借鑒價(jià)值。具體方法如下:
首先,C++鏈表倒序的難點(diǎn)在于如何一個(gè)個(gè)地修改。雖然不是數(shù)組,但是大概思想是一樣的,所以可以用一個(gè)for循序,一個(gè)游標(biāo)對應(yīng)for循環(huán)里面的 i,只不過要記得前一個(gè)節(jié)點(diǎn)和后一個(gè)節(jié)點(diǎn),尤其是后一個(gè),因?yàn)樾薷闹缶驮L問不到后面的,所以要記錄。for每一個(gè)循環(huán)只改變所指向的那個(gè)節(jié)點(diǎn)的指針,這樣既不會亂套了。
用一個(gè)for循環(huán)就非常好理解了,實(shí)例代碼如下所示:
#include <iostream> #include <cstdlib> #include <ctime> using namespace std; //鏈表節(jié)點(diǎn)類 class Node { private: int m_data; Node* m_next; Node(Node&) {} //copy constructor is not allowed public: explicit Node(int val = 0) : m_data(val), m_next(NULL) {} int getData() const { return m_data; } void setData(int val) { m_data = val; } Node* getNext(void) const { return m_next; } void setNext(Node* p) { m_next = p; } }; //鏈表 class MyList { private: Node* m_head; //pionter to the first node of the list Node* m_tail; //poinoer to the last node of the list MyList(MyList&) {} public: explicit MyList() : m_head(NULL), m_tail(NULL) {} void addNode(Node* pNode); void show(void) const; void reverse(void); void clean(void); }; void MyList::addNode(Node* pNode) { if (m_head) { m_tail->setNext(pNode); m_tail = pNode; } else //blank list { m_head = pNode; m_tail = pNode; } } void MyList::show(void) const { Node* pNode = m_head; while (pNode) { std::cout << pNode->getData() << " "; pNode = pNode->getNext(); } } void MyList::reverse(void) { Node* preNode = NULL; //下面游標(biāo)的前一個(gè) Node* pNode ; //指向每一個(gè)節(jié)點(diǎn),相當(dāng)于游標(biāo) Node* afterNode; //上面游標(biāo)的上一個(gè) for (pNode = m_head; pNode != NULL; pNode = afterNode) //這里的每次循環(huán)對應(yīng)一個(gè)節(jié)點(diǎn),本質(zhì)上和數(shù)組原理差不多 { afterNode = pNode->getNext(); pNode->setNext(preNode); preNode = pNode; } pNode = m_head; //交換頭尾指針 m_head = m_tail; m_tail = pNode; } void MyList::clean(void) { if (m_head) { Node* pNode = m_head; Node* pTemp; while (pNode) { pTemp = pNode->getNext(); delete pNode; pNode = pTemp; } m_head = m_tail = NULL; } } int main(void) { MyList listHead; srand((unsigned)time(NULL)); for (int i = 0; i < 9; i++) { int temp = rand() % 50; Node* pNode = new Node(temp); listHead.addNode(pNode); } listHead.show(); listHead.reverse(); cout << endl; listHead.show(); listHead.clean(); listHead.show(); system("pause"); }
相信本文實(shí)例對大家學(xué)習(xí)C++數(shù)據(jù)結(jié)構(gòu)與算法能起到一定的參考借鑒作用。
- C++實(shí)現(xiàn)單鏈表刪除倒數(shù)第k個(gè)節(jié)點(diǎn)的方法
- C++刪除鏈表中間節(jié)點(diǎn)的方法
- C++實(shí)現(xiàn)單鏈表按k值重新排序的方法
- C++數(shù)據(jù)結(jié)構(gòu)與算法之判斷一個(gè)鏈表是否為回文結(jié)構(gòu)的方法
- 用C++類實(shí)現(xiàn)單向鏈表的增刪查和反轉(zhuǎn)操作方法
- 淺析C++中單鏈表的增、刪、改、減
- C++中單鏈表的建立與基本操作
- C++循環(huán)鏈表之約瑟夫環(huán)的實(shí)現(xiàn)方法
- c++雙向鏈表操作示例(創(chuàng)建雙向鏈、雙向鏈表中查找數(shù)據(jù)、插入數(shù)據(jù)等)
- C++實(shí)現(xiàn)的鏈表類實(shí)例
- C++ 構(gòu)造雙向鏈表的實(shí)現(xiàn)代碼
- 用C++實(shí)現(xiàn)單向循環(huán)鏈表的解決方法
- C++實(shí)現(xiàn)打印兩個(gè)有序鏈表公共部分的方法
相關(guān)文章
Linux UDP服務(wù)端和客戶端程序的實(shí)現(xiàn)
這篇文章主要介紹了Linux UDP服務(wù)端和客戶端程序的實(shí)現(xiàn),文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧2020-05-05C語言之棧和堆(Stack && Heap)的優(yōu)缺點(diǎn)及其使用區(qū)別
本篇文章主要介紹了什么是棧(Stack) 、什么是堆( Heap),以及棧和堆的優(yōu)缺點(diǎn),同時(shí)介紹了應(yīng)該什么時(shí)候使用堆和棧,有需要的朋友可以參考下2015-07-07Qt中CQGUI框架之陰影圓角窗口實(shí)現(xiàn)
這篇文章主要介紹了Qt中CQGUI框架之陰影圓角窗口實(shí)現(xiàn),文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧2021-03-03C++實(shí)現(xiàn)LeetCode(174.地牢游戲)
這篇文章主要介紹了C++實(shí)現(xiàn)LeetCode(174.地牢游戲),本篇文章通過簡要的案例,講解了該項(xiàng)技術(shù)的了解與使用,以下就是詳細(xì)內(nèi)容,需要的朋友可以參考下2021-07-07C語言kmp算法簡單示例和實(shí)現(xiàn)原理探究
這篇文章主要介紹了C語言kmp算法簡單示例和實(shí)現(xiàn)原理探究,本文用簡潔的語言說明KMP算法的原理,并給出了示例,需要的朋友可以參考下2014-09-09