简体   繁体   中英

why do I get member access within null pointer error (linked list)

below are the error message and my code for linked list. could you please explain to me why do I get this error.

"Line 80: Char 21: runtime error: member access within null pointer of type 'Node' (solution.cpp) SUMMARY: UndefinedBehaviorSanitizer: undefined-behavior prog_joined.cpp:85:21"

struct Node{
    int val;
    Node* prev;
    Node* next;
    Node(): val(-1), prev(nullptr), next(nullptr){}
    Node(int x): val(x), prev(nullptr), next(nullptr){}
};

class MyLinkedList {
private:
    Node* head;
    Node* tail;
    size_t size;
public:
    MyLinkedList() {
        head = new Node();
        tail = new Node();
        head->next = tail;
        tail->prev = head;
        size = 0;
    }
    
    int get(int index) {
        if(index < 0 || index > size) return -1;
        Node* temp = head->next;
        while(index--) temp = temp->next;
        return temp->val;
    }
    
    void addAtHead(int val) {
         Node* newNode = new Node(val);
         Node* next = head->next;

         next->prev = newNode;
         newNode->next = next;
         head->next = newNode;
         newNode->prev = head;
         size++;
    }
    void addAtTail(int val) {
        Node* last_node = new Node(val);
        Node* temp = tail->prev;
        
        temp->next = last_node;
        last_node->prev = temp;
        last_node->next = tail;
        tail->prev = last_node;
        size++;
    }
    
    void addAtIndex(int index, int val) {
        if(index < 0 || index > size+1) return;
        if(index == 0) addAtHead(val);
        else if(index == size+1) addAtTail(val);
        else{
            Node* index_node = new Node(val);
            Node* temp = head;
            while(index--) temp = temp->next;

            index_node->next = temp->next;
            temp->next->prev = index_node;
            temp->next = index_node;
            index_node->prev = temp;
            size++;
        }
    }
    
    void deleteAtIndex(int index) {
        if(index > size) return;
        Node* temp = head;
        while(index--) temp = temp->next;
        temp->next = temp->next->next;
        temp->next->prev = temp;
        size--;
    }
};

thank you so much

Nevermind turns out this little guy was the issue all along:

 void addAtHead(int val) {
         Node* newNode = new Node(val);
         Node* next = head->next;

         next->prev = newNode;
         newNode->next = next;
         head->next = newNode;
         newNode->prev = head;
         size++;
    }

You intialized tail to head next, but in your addToHead function you never moved tail but added to head->next, so after one addition your new node will be the tail, and after two iteratorations, your new node will be past your tail, be ahead of your tail. And then you call your addToTail function so when you do, tail->prev = last_node, remember what tail-prev was before: that's right head, so you would not have a head node anymore hence the chain is broken, next time you try to call addToHead, you cannot find your head you do not have a pointer to it, so it crashes.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM