首页 > 解决方案 > 完成后对链表的操作给出错误

问题描述

我已经为它编写了一个链表类和一些方法。它正在完成所有方法,但程序在之后输出错误。有什么理由会发生这种情况吗?

节点 hpp:

class Node{
    public:
        int data;
        Node * next;
        Node(int val);
};

节点 cpp:

#include <stdio.h>
#include "node.hpp"

Node::Node(int val)
    : next(NULL)
{
    data = val;
}

链表类 hpp:

#include "node.cpp"

class LL {
    private:
        Node *head;
        Node *tail;
    public:

        LL();
        ~LL();


        int LL_append(int value);

        void LL_print();

        int LL_search(int target);

        int LL_catenate(LL * list);

        int LL_insert(int x);


};

链表类cpp:

#include "LL.hpp"
#include <stdio.h>
#include <iostream>

using std::cout;

LL::LL()
    :
      head(NULL),
      tail(NULL)
{

}
LL::~LL(){
    Node * curr = head;
    while(head != NULL){
        if(head == tail){
            delete head;
            return;
        }
        while(curr->next != tail){
            curr = curr->next;
        }
        delete tail;
        tail = curr;
        curr = head;
    }
}

//returns 1 for success and 0 for fail
int LL::LL_append(int value){
    int ret = 0;
    Node * newNode = new Node(value);
    if(value != NULL){
        if(head == NULL){
            head = newNode;
            tail = newNode;

        }   
        else{
            tail->next = newNode;
            tail = newNode;
        }
        ret = 1;
    }
    return ret;
}

//prints out list
void LL::LL_print(){
    Node * curr = head;
    cout << "[ ";
    while(curr != NULL){
        cout << curr->data << " ";
        curr = curr->next;
    }
    cout << "]\n";
}

//returns the number of times it appears in the list. return -1 if failed
int LL::LL_search(int target){
    int count = 0;
    Node * curr = head;
    while(curr != NULL){
        if(curr->data == target){
            count++;
        }
        curr = curr->next;
    }
    return count;
}

//returns 1 on success
int LL::LL_catenate(LL * list){
    if(list->head == NULL){

    }
    else if(head == NULL){
        head = list->head;
        tail = list->tail;
    }
    else{
        tail->next = list->head;
        tail = list->tail;
    }
    return 1;
}

int LL::LL_insert(int x){
    int ret = 0;
    Node * curr = head;
    Node * newNode = new Node(x);
    if(head == NULL){
        head = newNode;
        tail = newNode;
        ret = 1;
    }
    else if(head->data >= x){
        printf("here\n");
        newNode->next = head;
        head = newNode;
    }
    else{
        Node * curr = head;
        while( curr->next != NULL && curr->next->data < x){
            curr = curr->next;
        }
        newNode->next = curr->next;
        curr->next = newNode;
        if(curr->next == NULL){
            tail = newNode;
        }
        ret = 1;


    }

    return ret;
}

这就是我用来测试方法的方法:

int main(){
    LL L1 = LL();
    L1.LL_append(1);
    L1.LL_append(3);
    L1.LL_print();
    printf("%d\n", L1.LL_search(12));
    printf("%d\n", L1.LL_search(1));

    LL L2 = LL();
    L2.LL_append(5);
    L2.LL_append(9);
    L1.LL_catenate(&L2);
    L1.LL_print();

    L1.LL_insert(7);
    L1.LL_print();
    L1.~LL();
    L2.~LL();
    printf("done\n");
}

这是程序的输出:

[ 1 3 ]
0
1
[ 1 3 5 9 ]
[ 1 3 5 7 9 ]
done
double free or corruption (fasttop)
Aborted (core dumped)

我从未见过测试程序运行完成后发生错误。发生这种情况有什么原因吗?我相信这与插入方法有关。

标签: c++linked-list

解决方案


不要手动调用你的析构函数。当链表超出范围时,析构函数将被自动调用。如果您只是删除对析构函数的显式调用,它就可以正常工作。

请注意,您会收到有关使用NULL. 通过使用nullptr来解决这个问题。


推荐阅读