C++鏈表倒序?qū)崿F(xiàn)方法
本文通過(guò)一個(gè)實(shí)例展示了C++實(shí)現(xiàn)鏈表倒序的方法,對(duì)于C++數(shù)據(jù)結(jié)構(gòu)的學(xué)習(xí)有很好的參考借鑒價(jià)值。具體方法如下:
首先,C++鏈表倒序的難點(diǎn)在于如何一個(gè)個(gè)地修改。雖然不是數(shù)組,但是大概思想是一樣的,所以可以用一個(gè)for循序,一個(gè)游標(biāo)對(duì)應(yīng)for循環(huán)里面的 i,只不過(guò)要記得前一個(gè)節(jié)點(diǎn)和后一個(gè)節(jié)點(diǎn),尤其是后一個(gè),因?yàn)樾薷闹缶驮L問(wèn)不到后面的,所以要記錄。for每一個(gè)循環(huán)只改變所指向的那個(gè)節(jié)點(diǎn)的指針,這樣既不會(huì)亂套了。
用一個(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)對(duì)應(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í)例對(duì)大家學(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)文章
C語(yǔ)言實(shí)現(xiàn)統(tǒng)計(jì)字符串單詞數(shù)
這篇文章主要介紹了C語(yǔ)言實(shí)現(xiàn)統(tǒng)計(jì)字符串單詞數(shù),代碼非常的簡(jiǎn)潔,有需要的小伙伴快來(lái)參考下。2015-03-03
c++重載運(yùn)算符時(shí)返回值為類的對(duì)象或者返回對(duì)象的引用問(wèn)題
這篇文章主要介紹了c++重載運(yùn)算符時(shí)返回值為類的對(duì)象或者返回對(duì)象的引用問(wèn)題,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2022-11-11
C語(yǔ)言實(shí)現(xiàn)讀取CSV文件的方法詳解
這篇文章主要為大家詳細(xì)介紹了C語(yǔ)言如何實(shí)現(xiàn)讀取CSV文件,文中的示例代碼講解詳細(xì),具有一定的借鑒價(jià)值,感興趣的小伙伴可以跟隨小編一起學(xué)習(xí)一下2022-12-12
淺談C語(yǔ)言中strcpy,strcmp,strlen,strcat函數(shù)原型
下面小編就為大家?guī)?lái)一篇淺談C語(yǔ)言中strcpy,strcmp,strlen,strcat函數(shù)原型。小編覺(jué)得挺不錯(cuò)的,現(xiàn)在就分享給大家,也給大家做個(gè)參考。一起跟隨小編過(guò)來(lái)看看吧2017-04-04
C++實(shí)現(xiàn)LeetCode(44.外卡匹配)
這篇文章主要介紹了C++實(shí)現(xiàn)LeetCode(44.外卡匹配),本篇文章通過(guò)簡(jiǎn)要的案例,講解了該項(xiàng)技術(shù)的了解與使用,以下就是詳細(xì)內(nèi)容,需要的朋友可以參考下2021-07-07
C++回文數(shù)及素?cái)?shù)問(wèn)題計(jì)算方法
這篇文章主要介紹了C++回文數(shù)及素?cái)?shù)問(wèn)題計(jì)算方法,可實(shí)現(xiàn)一定范圍內(nèi)的素?cái)?shù)與回文數(shù)運(yùn)算功能,涉及C++字符串遍歷與數(shù)字?jǐn)?shù)學(xué)運(yùn)算的相關(guān)技巧,需要的朋友可以參考下2016-05-05
純c語(yǔ)言優(yōu)雅地實(shí)現(xiàn)矩陣運(yùn)算庫(kù)的方法
本文主要介紹了純c語(yǔ)言優(yōu)雅地實(shí)現(xiàn)矩陣運(yùn)算庫(kù),文中通過(guò)示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2021-08-08

