(cpp实现)设计链表快速实现O尾插和头插

Posted C_YCBX Py_YYDS

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了(cpp实现)设计链表快速实现O尾插和头插相关的知识,希望对你有一定的参考价值。

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

以上是关于(cpp实现)设计链表快速实现O尾插和头插的主要内容,如果未能解决你的问题,请参考以下文章

用Python实现单链表的头插,尾插和中插

C实现头插法和尾插法来构建链表

单链表的头插法与尾插法代码实现及详解

C实现头插法和尾插法来构建单链表(不带头结点)

数据结构(链表——双向链表的实现)

第二节1:Java集合框架之链表及其实现