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 小米 华为 单反 装机 图拉丁
 
   -> 数据结构与算法 -> (cpp实现)设计链表快速实现O(1)尾插和头插 -> 正文阅读

[数据结构与算法](cpp实现)设计链表快速实现O(1)尾插和头插

image.png

内存细节讨论

  1. 不要用静态分配的方式设计!!!因为一旦离开该数据的作用范围,它会被自动销毁,而动态分配则完全不同,它由于不会销毁内存,所以只要找到了地址便可以得到该块内存区域的数据。
  2. 由于是动态分配,动态分配在 C/C++ 中它并不会自动回收,需要手动释放内存,所以最好把析构函数写好。

构造函数和析构函数 以及基本的结构定义

struct listNode{
    int val;
    listNode* next;
    listNode(int v):val(v),next(nullptr){}
    
};
private: int size;listNode* head;listNode* rear;
public:
    /** Initialize your data structure here. */
    MyLinkedList() {
        head =new listNode(0);
        rear = head;
        size = 0;
    }
    ~MyLinkedList(){
        listNode* pre = head;
        listNode* cur = head->next;
        while(cur){
            delete pre;
            pre = cur;
            cur = cur->next;
        }
        delete pre;
    }    

头插和尾插的O(1)实现

    /** 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) {
        listNode* q = new listNode(val);
        q->next = head->next;
        head->next = q;
        if(size<1)rear = q;
        size++;
    }
    
    /** Append a node of value val to the last element of the linked list. */
    void addAtTail(int val) {
        listNode* t =new listNode(val);
        rear->next = t;
        rear = rear->next;
        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) {
        if(index==size){
            addAtTail(val);
            return;}
        else if(index<=0){
            addAtHead(val);
            return;}
        else if(index>size)
            return;
        else{
            listNode* t = new listNode(val);
            listNode* q = head;
            for(int i=0;i<index;i++){
                q = q->next;
            }
            t->next = q->next;
            q->next = t;
        }
        size++;
}

指定位置删除和得到指定位置的值的实现

    /** Delete the index-th node in the linked list, if the index is valid. */
    void deleteAtIndex(int index) {
        if(index<0 or index>size-1)
            return;
        listNode* p = head;
        for(int i=0;i<index;i++){
            p = p->next;
        }
//这个地方非常细节,删除结点的时候,如果删除的是最后一个结点,则会把原来的rear指针所指向的区域销毁,导致尾插无法正常进行
        if(index == size-1)
            rear = p;
        listNode* t = p->next;
        p->next = t->next;
        delete t;
        size--;
    }
    /** 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>size-1 or index<0)
            return -1;
        listNode* p = head;
        for(int i=0;i<=index;i++){
            p = p->next;
        }
        return p->val;
    }

汇总代码得到答案

class MyLinkedList {
struct listNode{
    int val;
    listNode* next;
    listNode(int v):val(v),next(nullptr){}
    
};
private: int size;listNode* head;listNode* rear;
//不能用静态分配的方式设计!!!因为一旦离开该数据的作用范围,它会被自动销毁,而动态分配则完全不同,它由于不会销毁内存,所以只要找到了地址便可以得到该块内存区域的数据。
public:
    /** Initialize your data structure here. */
    MyLinkedList() {
        head =new listNode(0);
        rear = head;
        size = 0;
    }
    ~MyLinkedList(){
        listNode* pre = head;
        listNode* cur = head->next;
        while(cur){
            delete pre;
            pre = cur;
            cur = cur->next;
        }
        delete pre;
    }    
    
    /** 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>size-1 or index<0)
            return -1;
        listNode* p = head;
        for(int i=0;i<=index;i++){
            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) {
        listNode* q = new listNode(val);
        q->next = head->next;
        head->next = q;
        if(size<1)rear = q;
        size++;
    }
    
    /** Append a node of value val to the last element of the linked list. */
    void addAtTail(int val) {
        listNode* t =new listNode(val);
        rear->next = t;
        rear = rear->next;
        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) {
        if(index==size){
            addAtTail(val);
            return;}
        else if(index<=0){
            addAtHead(val);
            return;}
        else if(index>size)
            return;
        else{
            listNode* t = new listNode(val);
            listNode* q = head;
            for(int i=0;i<index;i++){
                q = q->next;
            }
            t->next = q->next;
            q->next = t;
        }
        size++;
}
    
    /** Delete the index-th node in the linked list, if the index is valid. */
    void deleteAtIndex(int index) {
        if(index<0 or index>size-1)
            return;
        listNode* p = head;
        for(int i=0;i<index;i++){
            p = p->next;
        }
//这个地方非常细节,删除结点的时候,如果删除的是最后一个结点,则会把原来的rear指针所指向的区域销毁,导致尾插无法正常进行
        if(index == size-1)
            rear = p;
        listNode* t = p->next;
        p->next = t->next;
        delete t;
        size--;
    }
};

  数据结构与算法 最新文章
【力扣106】 从中序与后续遍历序列构造二叉
leetcode 322 零钱兑换
哈希的应用:海量数据处理
动态规划|最短Hamilton路径
华为机试_HJ41 称砝码【中等】【menset】【
【C与数据结构】——寒假提高每日练习Day1
基础算法——堆排序
2023王道数据结构线性表--单链表课后习题部
LeetCode 之 反转链表的一部分
【题解】lintcode必刷50题<有效的括号序列
上一篇文章      下一篇文章      查看所有文章
加:2021-07-11 16:51:09  更:2021-07-11 16:52:42 
 
开发: 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/9 8:48:24-

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