2013-04-04 67 views
0

我:錯誤,當我從一個鏈表中刪除一個節點

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. 

爲什麼呢?

THIS IS MY REAL CODE:

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

你能告訴我們你分配'vec'的代碼嗎? – 2013-04-04 02:32:09

+2

可以有很多原因。您將需要向我們展示更多代碼! – Nbr44 2013-04-04 02:37:20

+0

好吧,我正在爲真正的代碼更新我的主題。謝謝。 – 2013-04-04 02:39:08

回答

1

所以要根據你的迴應你的問題是,你正在做一個double delete這是undefined behavior。你應該從Time_LinkedList::delete_TimeNode中刪除delete data,讓析構函數完成它的工作。