美文网首页
C++双向链表

C++双向链表

作者: 爱学习的猫叔 | 来源:发表于2024-11-19 08:51 被阅读0次

为什么要使用双向链表

单向链表只能从头节点往后遍历,双向链表每个节点都有指向上一个节点的指针,支持反向遍历,如果数据量比较大的时候,从后往前遍历遍历效率更高,查找的效率会间接影响增删的效率。

具体逻辑实现

//
// 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 {//头节点
        head = cur->next;
    }
    if (cur->next) {
        cur->next->pre = cur->pre;
    } else {//尾节点
        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

相关文章

  • C++实现双向循环链表

    本次博文是关于利用C++模板的方式实现的双向循环链表以及双向循环链表的基本操作,在之前的博文C++语言实现双向链表...

  • C++语言实现双向链表

    这篇文章是关于利用C++模板的方式实现的双向链表以及双向链表的基本操作,在之前的博文C语言实现双向链表中,已经给大...

  • 双向链表&双向循环链表

    链表分为:单链表、单向循环链表、双向链表、双向循环链表本节主要说明:双向链表、双向循环链表 定义结点 一、双向链表...

  • 容器(2) - LinkedList

    LinkedList 基本实现 LinkedList 类似 C/C++ 的双向链表,这种链表中任意一个存储单元都可...

  • 线性表-双向链表与双向循环链表

    双向链表 双向链表示意图如下: 数据结构定义 创建双向链表 双向链表插入元素 双向链表删除元素 双向链表打印元素 ...

  • day03-双向链表

    双向链表: 单向链表只能单向查找,双向链表可以双向查找。 啥是双向链表? 双向链表可以双向查数据,所以就不存在单向...

  • 线性表--链式存储结构--双向链表

    双向链表 一、双向链表结构 双向链表结点结构 既然单链表可以有循环链表,那么双向链表当然也可以有。 由于这是双向链...

  • 双向链表和双向循环链表

    双向链表 线性表-双向链表的结点结构: 带头结点的双向链表: 1.双向链表初始化 2.遍历双向链表 2.双向链表插...

  • 数据结构与算法之双向链表(3.3)

    目录 双向链表简介双向链表重要方法讲解实战检测双向链表,单向链表性能对比 一 双向链表简介 双向链表-只有一个元素...

  • 双向链表&双向循环链表

    一、双向链表 带有前驱结点、后区节点 双向链表的创建 双向链表插入-逻辑 双向链表删除 删除双向链表指定的元素 二...

网友评论

      本文标题:C++双向链表

      本文链接:https://www.haomeiwen.com/subject/flevjjtx.html