IT数码 购物 网址 头条 软件 日历 阅读 图书馆
TxT小说阅读器
↓语音阅读,小说下载,古典文学↓
图片批量下载器
↓批量下载图片,美女图库↓
图片自动播放器
↓图片自动播放器↓
一键清除垃圾
↓轻轻一点,清除系统垃圾↓
开发: C++知识库 Java知识库 JavaScript Python PHP知识库 人工智能 区块链 大数据 移动开发 嵌入式 开发工具 数据结构与算法 开发测试 游戏开发 网络协议 系统运维
教程: HTML教程 CSS教程 JavaScript教程 Go语言教程 JQuery教程 VUE教程 VUE3教程 Bootstrap教程 SQL数据库教程 C语言教程 C++教程 Java教程 Python教程 Python3教程 C#教程
数码: 电脑 笔记本 显卡 显示器 固态硬盘 硬盘 耳机 手机 iphone vivo oppo 小米 华为 单反 装机 图拉丁
 
   -> 数据结构与算法 -> 双链表(C++) -> 正文阅读

[数据结构与算法]双链表(C++)

? ? ? ?在介绍了单链表结构后,我们发现链表结构在使用过程相对数组具有更优的存储性能,但是其存在的不足就是吗,每次需要访问当前节点的前一个节点时需要从头在访问,这大大降低了链表的存储性能,因此引出了双链表结构来存储当前节点的前一个节点。

? ? ? ?双链表结构较单链表结构多了prev字段,让我们可以更加方便的获取前一个节点:

?PS:上图中的绿色箭头就代表prev节点的运行原理,其中双链表的典型定义如下:

// Definition for doubly-linked list.
struct DoublyListNode {
    int val;
    DoublyListNode *next, *prev;
    DoublyListNode(int x) : val(x), next(NULL), prev(NULL) {}
};

1.添加双链表

我们可以将该插入操作分为两个部分:

  1. 链接cur与next和prev,next是prev原始的下一个节点
  2. ??使用cur重新链接prev和next

? ? ? ?这里在刚开始写的过程中我总是直接把next->prev直接赋给prev->next,这样会导致出错,而正确写法应当是将prev和next的赋值分开进行。

  • cur->next = p-next;
  • p->next->prev = cur,
  • p->next = cur;
  • cur->prev = p.

2.删除双链表

? ? ? ?如果想要删除一个双链表节点cur,我们只需要将当前cur前一个节点的next与cur下一个节点的prev链接起来即可。即:

? ? ? ?如果要删除上图中的节点6,可以将节点23的next与15的prev分别链接即可:

? ? ?PS:以上删除和插入操作因为prev节点,所以时间和空间复杂度均是O(1)。

注意:

? ? ?在删除和插入操作中,我们对于在头结点插入和删除,尾结点插入和删除的操作都需要特别注意:

  • 头结点在插入和删除后需要将头节点重新定义赋值,即head = cur;
  • 尾部插入和删除我们需要在尾部的赋空值,即cur->next = NULL;

3. 设计双链表?

class MyLinkedList {
public:
    struct ListNode{
        ListNode *next,*pred;
        int val;
        ListNode(int x)
        {
            val=x;
            next=NULL;
            pred=NULL;
        }
    };
    
    /** Initialize your data structure here. */
    MyLinkedList() {
        head = new ListNode(0);
        size = 0;

    }
    
    /** Get the value of the index-th node in the linked list. If the index is invalid, return -1. */
    // int get(int index) {
    //     if(index<0||index>=size){
    //         return -1;
    //     }
    //     ListNode *cur = head->next;  //从头节点的下一个开始
    //     if(cur == NULL) return -1;
    //     while(index--)
    //     {
    //         cur = cur->next;
    //     }
    //     return cur->val;
    // }
     int get(int index) {
        if(index<0||index>=size)//
            return -1;
        ListNode *p=head->next;
        if(p==NULL)
            return -1;
        while(index--)
            p=p->next;
        return p->val;
    }
    
    /** Add a node of value val before the first element of the linked list. After the insertion, the new node will be the first node of the linked list. */
    void addAtHead(int val) {              //next和pred的赋值是分开进行的
        ListNode *cur = new ListNode(val);
        cur->next = head->next;
        if(head->next)
        {
            head->next->pred = cur->next;
        }
        head->next = cur;
        cur->pred = head; 
        size++; 
    }
    
    /** Append a node of value val to the last element of the linked list. */
    void addAtTail(int val) {        //bingo
        ListNode *cur = head;
        ListNode *newNode = new ListNode(val);
        while(cur!=NULL) cur = cur->next;
        if(cur==NULL) return;
        cur->next = newNode;
        newNode->pred = cur;
        newNode->next = NULL;
        size++;
    }
    
    /** Add a node of value val before the index-th node in the linked list. If index equals to the length of linked list, the node will be appended to the end of linked list. If index is greater than the length, the node will not be inserted. */
    void addAtIndex(int index, int val) {
        ListNode *cur = head;
        ListNode *newNode = new ListNode(val);
        if(index>size) return;
        else if(index==size) addAtTail(val);
        else if(index <= 0) addAtHead(val);
        else{
            while(index--)
        {
            cur = cur->next;
        }
        if(cur==NULL&&cur->next==NULL) return;
        newNode->next = cur->next;
        cur->next->pred = newNode;
        cur->next = newNode;
        newNode->pred = cur;
        size++;
        }   
    }
    
    /** Delete the index-th node in the linked list, if the index is valid. */
    void deleteAtIndex(int index) {      //区别删除的是否为开始节点 head删除后需要调整head节点
        ListNode *cur = head;
        if(index<0||index>=size)
        return;
        if(index==0)
        {
            if(head!=NULL&&head->next!=NULL&&head->next->next!=NULL)
            {
                head->next = head->next->next;
                head->next->pred = head;
            }
            else{
                head->next = NULL;
            }
        }
        else{
             while(index--)
        {
            cur = cur->next;
        }
        if(cur==NULL||cur->next==NULL) return;
        cur->next = cur->next->next;
        //cur->next->next->pred = cur;   注意已经交换结束后直接为cur->next
        if(cur->next)
        cur->next->pred = cur;
        }
       size--;
    }
private:
        int size;
        ListNode *head;
};

  数据结构与算法 最新文章
【力扣106】 从中序与后续遍历序列构造二叉
leetcode 322 零钱兑换
哈希的应用:海量数据处理
动态规划|最短Hamilton路径
华为机试_HJ41 称砝码【中等】【menset】【
【C与数据结构】——寒假提高每日练习Day1
基础算法——堆排序
2023王道数据结构线性表--单链表课后习题部
LeetCode 之 反转链表的一部分
【题解】lintcode必刷50题<有效的括号序列
上一篇文章      下一篇文章      查看所有文章
加:2021-09-24 10:50:25  更:2021-09-24 10:54:15 
 
开发: C++知识库 Java知识库 JavaScript Python PHP知识库 人工智能 区块链 大数据 移动开发 嵌入式 开发工具 数据结构与算法 开发测试 游戏开发 网络协议 系统运维
教程: HTML教程 CSS教程 JavaScript教程 Go语言教程 JQuery教程 VUE教程 VUE3教程 Bootstrap教程 SQL数据库教程 C语言教程 C++教程 Java教程 Python教程 Python3教程 C#教程
数码: 电脑 笔记本 显卡 显示器 固态硬盘 硬盘 耳机 手机 iphone vivo oppo 小米 华为 单反 装机 图拉丁

360图书馆 购物 三丰科技 阅读网 日历 万年历 2024年5日历 -2024/5/17 10:01:51-

图片自动播放器
↓图片自动播放器↓
TxT小说阅读器
↓语音阅读,小说下载,古典文学↓
一键清除垃圾
↓轻轻一点,清除系统垃圾↓
图片批量下载器
↓批量下载图片,美女图库↓
  网站联系: qq:121756557 email:121756557@qq.com  IT数码