从链表中删除节点时出错



我有:

class Node {
   int* vec;
   Node* next;
};
Class LinkedList{
   Node* head;
}

我创建了一个函数来查找我想要删除的节点:

Node* tmp = find("abc");

我对指针进行了排序并进行了调试,一切正常。

现在我必须删除tmp,所以我尝试:

delete[] tmp->vec;
delete tmp; // here I get an error window.

为什么?

这是我的真实代码:

class Show_Time
{
  private:
    string movieCode;
    string movieName;
    string time; //the time of screening the movie.
};

class Time_LinkedList
{
 private:
    class Time_Node
    {
     public:
        Show_Time* data;
        Time_Node* prev;
        Time_Node* next;
     public:
        Time_Node(string movie_code, string movie_name, string time_of_movie); //"Time_Node" constructor
        ~Time_Node();//"Time_Node" destructor
    };
    Time_Node* head; //pointer to the first node in the linkedlist
};

void Time_LinkedList::delete_TimeNode(string movieCode, string time)
{
    Time_Node* tmp = find_Time_Node(movieCode,time);
    // the case that there is one element in the list
    if (tmp->prev == NULL && tmp->next == NULL)
    {
        head = NULL;
    }
    // the case that it's the first element of the list
    else if (tmp->prev == NULL)
    {
        head = tmp->next;
        tmp->next->prev = head;        
    }
    // the case that it's the last element of the list
    else if (tmp->next == NULL)
    {
        tmp->prev->next = NULL;
    }
    // there are element in the left and right of the element
    else
    {
        tmp->prev->next = tmp->next;
        tmp->next->prev = tmp->prev;
    }
    // delete the temp and its data
    delete tmp->data;
    delete tmp;
}

因此,根据您的回答,您的问题是您正在做double delete,这是未定义的行为。你应该把delete dataTime_LinkedList::delete_TimeNode中去掉,让析构函数来做它的工作。

相关内容

  • 没有找到相关文章