(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--;
    }
};

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 1
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值