C++雙向鏈表

為什么要使用雙向鏈表

單向鏈表只能從頭節(jié)點往后遍歷,雙向鏈表每個節(jié)點都有指向上一個節(jié)點的指針,支持反向遍歷,如果數(shù)據(jù)量比較大的時候,從后往前遍歷遍歷效率更高,查找的效率會間接影響增刪的效率。

具體邏輯實現(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é)點
        head = cur->next;
    }
    if (cur->next) {
        cur->next->pre = cur->pre;
    } else {//尾節(jié)點
        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

運行效果

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

最后編輯于
?著作權歸作者所有,轉載或內(nèi)容合作請聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時請結合常識與多方信息審慎甄別。
平臺聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點,簡書系信息發(fā)布平臺,僅提供信息存儲服務。

相關閱讀更多精彩內(nèi)容

友情鏈接更多精彩內(nèi)容