C++雙向鏈表

為什么要使用雙向鏈表

單向鏈表只能從頭節(jié)點(diǎn)往后遍歷豁辉,雙向鏈表每個(gè)節(jié)點(diǎn)都有指向上一個(gè)節(jié)點(diǎn)的指針庶橱,支持反向遍歷夷恍,如果數(shù)據(jù)量比較大的時(shí)候拱雏,從后往前遍歷遍歷效率更高棉安,查找的效率會(huì)間接影響增刪的效率。

具體邏輯實(shí)現(xiàn)

//
// Created by ygq on 2024/11/1.
//
#include "log.h"

#ifndef YGQ_TAG
#define YGQ_TAG

template<class E>
struct Node {
    E value;
    Node<E> *pre;
    Node<E> *next;

    Node(E value, Node<E> *pre, Node<E> *next) {
        this->value = value;
        this->pre = pre;
        this->next = next;
    }
};

template<class E>
class LinkedList {
private:
    int len = 0;
    Node<E> *head = nullptr;
    Node<E> *end = nullptr;
public:
    void push(E e);

    void insert(int index, E e);

    E remove(int index);

    Node<E> *findNode(int index);

    E get(int index);

    int size();

    void linkLast(E e);

    void linkBefore(Node<E> *cur, E e);
};

/*
template<class E>
void LinkedList<E>::push(E e) {
    Node<E> *new_node = new Node<E>(e, nullptr);
    if (head) {
        Node<E> *last = findNode(len - 1);
        last->next = new_node;
    } else {
        head = new_node;
    }
    len++;
}*/

template<class E>
void LinkedList<E>::push(E e) {
    Node<E> *new_node = new Node<E>(e, end, nullptr);
    if (head) {
        end->next = new_node;
        end = new_node;
    } else {
        head = new_node;
        end = new_node;
    }
    len++;
}

template<class E>
void LinkedList<E>::linkLast(E e) {
    Node<E> *new_node = new Node<E>(e, end, nullptr);
    if (head) {
        end->next = new_node;
        end = new_node;
    } else {
        head = new_node;
        end = new_node;
    }
}

template<class E>
void LinkedList<E>::linkBefore(Node<E> *cur, E e) {
    Node<E> *new_node = new Node<E>(e, cur->pre, cur);
    cur->pre->next = new_node;
    cur->pre = new_node;
}

template<class E>
void LinkedList<E>::insert(int index, E e) {
    if (index > len - 1 || index < 0) {
        LOGE("index:%d illegal,must <= %d or > 0", index, len - 1);
        return;
    }
    Node<E> *node = findNode(index);
    linkBefore(node, e);
    len++;
}

template<class E>
E LinkedList<E>::remove(int index) {
    if (index > len - 1 || index < 0) {
        LOGE("index:%d illegal,must <= %d or > 0", index, len - 1);
        return E();
    }
    Node<E> *cur = findNode(index);
    if (cur->pre) {
        cur->pre->next = cur->next;
    } else {//頭節(jié)點(diǎn)
        head = cur->next;
    }
    if (cur->next) {
        cur->next->pre = cur->pre;
    } else {//尾節(jié)點(diǎn)
        end = cur->pre;
    }
    E value = cur->value;
    delete cur;
    len--;
    return value;
}

template<class E>
E LinkedList<E>::get(int index) {
    return findNode(index)->value;
}

template<class E>
Node<E> *LinkedList<E>::findNode(int index) {
    if (index > len >> 1) {
        //從后往前遍歷
        Node<E> *cur = end;
        for (int i = len - 1; i > index; --i) {
            cur = cur->pre;
        }
        return cur;
    } else {
        //從前往后遍歷
        Node<E> *cur = head;
        for (int i = 0; i < index; ++i) {
            cur = cur->next;
        }
        return cur;
    }
}

template<class E>
int LinkedList<E>::size() {
    return len;
}

#endif

運(yùn)行效果

index:0,value:0
index:1,value:1
index:2,value:2
index:3,value:5
index:4,value:6
index:5,value:7
---------
index:0,value:1
index:1,value:5
index:2,value:6

源碼地址

https://github.com/treech/NDKDemo

最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請(qǐng)聯(lián)系作者
  • 序言:七十年代末铸抑,一起剝皮案震驚了整個(gè)濱河市贡耽,隨后出現(xiàn)的幾起案子,更是在濱河造成了極大的恐慌鹊汛,老刑警劉巖蒲赂,帶你破解...
    沈念sama閱讀 222,464評(píng)論 6 517
  • 序言:濱河連續(xù)發(fā)生了三起死亡事件,死亡現(xiàn)場離奇詭異刁憋,居然都是意外死亡滥嘴,警方通過查閱死者的電腦和手機(jī),發(fā)現(xiàn)死者居然都...
    沈念sama閱讀 95,033評(píng)論 3 399
  • 文/潘曉璐 我一進(jìn)店門至耻,熙熙樓的掌柜王于貴愁眉苦臉地迎上來若皱,“玉大人镊叁,你說我怎么就攤上這事∽叽ィ” “怎么了晦譬?”我有些...
    開封第一講書人閱讀 169,078評(píng)論 0 362
  • 文/不壞的土叔 我叫張陵,是天一觀的道長互广。 經(jīng)常有香客問我敛腌,道長,這世上最難降的妖魔是什么惫皱? 我笑而不...
    開封第一講書人閱讀 59,979評(píng)論 1 299
  • 正文 為了忘掉前任迎瞧,我火速辦了婚禮,結(jié)果婚禮上逸吵,老公的妹妹穿的比我還像新娘凶硅。我一直安慰自己,他們只是感情好扫皱,可當(dāng)我...
    茶點(diǎn)故事閱讀 69,001評(píng)論 6 398
  • 文/花漫 我一把揭開白布足绅。 她就那樣靜靜地躺著,像睡著了一般韩脑。 火紅的嫁衣襯著肌膚如雪氢妈。 梳的紋絲不亂的頭發(fā)上,一...
    開封第一講書人閱讀 52,584評(píng)論 1 312
  • 那天段多,我揣著相機(jī)與錄音首量,去河邊找鬼。 笑死进苍,一個(gè)胖子當(dāng)著我的面吹牛加缘,可吹牛的內(nèi)容都是我干的。 我是一名探鬼主播觉啊,決...
    沈念sama閱讀 41,085評(píng)論 3 422
  • 文/蒼蘭香墨 我猛地睜開眼拣宏,長吁一口氣:“原來是場噩夢(mèng)啊……” “哼!你這毒婦竟也來了杠人?” 一聲冷哼從身側(cè)響起勋乾,我...
    開封第一講書人閱讀 40,023評(píng)論 0 277
  • 序言:老撾萬榮一對(duì)情侶失蹤,失蹤者是張志新(化名)和其女友劉穎嗡善,沒想到半個(gè)月后辑莫,有當(dāng)?shù)厝嗽跇淞掷锇l(fā)現(xiàn)了一具尸體,經(jīng)...
    沈念sama閱讀 46,555評(píng)論 1 319
  • 正文 獨(dú)居荒郊野嶺守林人離奇死亡罩引,尸身上長有42處帶血的膿包…… 初始之章·張勛 以下內(nèi)容為張勛視角 年9月15日...
    茶點(diǎn)故事閱讀 38,626評(píng)論 3 342
  • 正文 我和宋清朗相戀三年各吨,在試婚紗的時(shí)候發(fā)現(xiàn)自己被綠了。 大學(xué)時(shí)的朋友給我發(fā)了我未婚夫和他白月光在一起吃飯的照片蜒程。...
    茶點(diǎn)故事閱讀 40,769評(píng)論 1 353
  • 序言:一個(gè)原本活蹦亂跳的男人離奇死亡绅你,死狀恐怖伺帘,靈堂內(nèi)的尸體忽然破棺而出,到底是詐尸還是另有隱情忌锯,我是刑警寧澤伪嫁,帶...
    沈念sama閱讀 36,439評(píng)論 5 351
  • 正文 年R本政府宣布,位于F島的核電站偶垮,受9級(jí)特大地震影響张咳,放射性物質(zhì)發(fā)生泄漏。R本人自食惡果不足惜似舵,卻給世界環(huán)境...
    茶點(diǎn)故事閱讀 42,115評(píng)論 3 335
  • 文/蒙蒙 一脚猾、第九天 我趴在偏房一處隱蔽的房頂上張望。 院中可真熱鬧砚哗,春花似錦龙助、人聲如沸。這莊子的主人今日做“春日...
    開封第一講書人閱讀 32,601評(píng)論 0 25
  • 文/蒼蘭香墨 我抬頭看了看天上的太陽。三九已至仅淑,卻和暖如春称勋,著一層夾襖步出監(jiān)牢的瞬間,已是汗流浹背涯竟。 一陣腳步聲響...
    開封第一講書人閱讀 33,702評(píng)論 1 274
  • 我被黑心中介騙來泰國打工赡鲜, 沒想到剛下飛機(jī)就差點(diǎn)兒被人妖公主榨干…… 1. 我叫王不留,地道東北人庐船。 一個(gè)月前我還...
    沈念sama閱讀 49,191評(píng)論 3 378
  • 正文 我出身青樓银酬,卻偏偏與公主長得像,于是被迫代替她去往敵國和親醉鳖。 傳聞我的和親對(duì)象是個(gè)殘疾皇子捡硅,可洞房花燭夜當(dāng)晚...
    茶點(diǎn)故事閱讀 45,781評(píng)論 2 361

推薦閱讀更多精彩內(nèi)容