2012-05-27 68 views
-1

編輯:在C++中刪除不起作用

好的,所以這是列表的正確執行。我想很多人都會覺得它很有用。特別感謝你們,特別是在通訊員幫助下我的Agent_L。

正確的鏈表實現

#include <cstdio> 
#include <cmath> 
#include<iostream> 
#include <stdio.h> 
#include <string.h> 
using namespace std; 

class Node{ 
friend class List; 
public: 
    Node(Node* next, int wrt){ 
     this->next = next; 
     this->wrt = wrt; 
    } 

    Node(const Node& obiekt){ 
     this->wrt = obiekt.wrt; 
     this->next = obiekt.next; 
    } 
    //NIE MA DESTRUKTORA BO NIE ALOKUJE ZADNYCH DANYCH !!! 

    void show(){ 
     cout<<this->wrt<<endl; 
    } 




private: 
    Node* next; 
    int wrt; 

}; 


class List{ 

public: 
List(int wrt){ 
    this->root = new Node(NULL, wrt); 
} 


    List(const List& list) 
{ 
    // jesli pusty kopiujemy 
    if (list.root == NULL) 
    { 
     this->root = NULL; 
     return; 
    } 

    //tworzenie nowego korzenia 
    this->root = new Node(NULL, list.root->wrt); 

    Node* list_currentNode = list.root; 
    Node* this_currentNode = this->root; 
    while (list_currentNode->next != NULL) 
    { 
     // tworzenie nastepnika 
     Node* newNode = new Node(NULL, list_currentNode->next->wrt); 
     this_currentNode->next = newNode; 
     this_currentNode = this_currentNode->next; 
     list_currentNode = list_currentNode->next; 
    } 
} 


void add(int wrt){ 
    Node* node = new Node(NULL, wrt); 
    Node* el = this->root; 
    while(el->next != NULL){ 
     //el->show(); 
     el = el->next; 
    } 
    el->next = node; 
} 

void remove(int index){ 
    Node* el = this->root; 
    if(index == 0){ 
     this->root = el->next; 
     delete el; 
    } 
    else{ 
    int i = 0; 
    while(el != NULL && i < index - 1){ 

     el = el->next; 
     i++; 
    } 
    if(el!=NULL){ 
     Node* toRem = el->next; 
     Node* newNext = toRem->next; 
     el->next = newNext; 
     delete toRem; 
    } 
} 
} 

void show(){ 
    Node* el = this->root; 
    while(el != NULL){ 
     el->show(); 
     el = el->next; 
    } 
} 

~List(){ 
    Node* currentNode = this->root; 
    while (currentNode != NULL) 
    { 
     Node* nextNode = currentNode->next; 
     delete currentNode; 
     currentNode = nextNode; 
    } 
} 


private: 
    Node* root; 

}; 

int main(){ 
    List* l = new List(11); 
    l->add(22); l->add(33); 
    l->show(); 
    cout<<endl; 
    List* lala = new List(*l); 
    lala->show(); 
    cout<<endl; 
    lala->add(44); 
    cout<<"lala before remove"<<endl; 
    lala->show(); 
    lala->remove(1); 
    cout<<"l before delete"<<endl; 
    l->show(); 
    cout<<"lala before delete"<<endl; 
    lala->show(); 
    delete l; 
    /* cout<<"l after delete "<<endl; 
    l->show(); */ 
    cout<<"lala after delete"<<endl; 
    lala->show(); 
    return 0; 
    } 

我實現了列表和存在的問題。我在列表中的析構函數不工作,因爲它應該:請看看主要,並看到「l刪除後」它向後打印l列表。 更大的問題是刪除方法沒有刪除它裏面的工作,因爲它應該但當我試圖取消註釋刪除el /刪除雷姆我進入無限循環。 我已經嘗試修復它4個小時。請看一眼。

爲什麼當我調用l-> remove(0)時,這些行特別是87(忘記100)會導致程序崩潰?

http://wklej.org/id/761056/線路87和100

remove方法和列表的析構函數是重要

void remove(int index){ 
    Node* el = this->root; 
    if(index == 0){ 
     this->root = el->next; 
    // delete el; 
    } 
    else{ 
    int i = 0; 
    while(el != NULL && i < index - 1){ 

     el = el->next; 
     i++; 
    } 
    if(el!=NULL){ 
     Node* toRem = el->next; 
     Node* newNext = toRem->next; 
     el->next = newNext; 
     // delete toRem; 
    } 
} 
} 

~List(){ 
    Node* currentNode = this->root; 
    while (currentNode != NULL) 
    { 
     Node* nextNode = currentNode->next; 
     delete currentNode; 
     currentNode = nextNode; 
    } 
} 

所有代碼

#include <cstdio> 
#include <cmath> 
#include<iostream> 
#include <stdio.h> 
#include <string.h> 
using namespace std; 

class Node{ 
friend class List; 
public: 
    Node(Node* next, int wrt){ 
     this->next = next; 
     this->wrt = wrt; 
    } 

    Node(const Node& obiekt){ 
     this->wrt = obiekt.wrt; 
     this->next = obiekt.next; 
    } 
    //NIE MA DESTRUKTORA BO NIE ALOKUJE ZADNYCH DANYCH !!! 

    void show(){ 
     cout<<this->wrt<<endl; 
    } 




private: 
    Node* next; 
    int wrt; 

}; 


class List{ 

public: 
List(int wrt){ 
    this->root = new Node(NULL, wrt); 
} 


    List(const List& list) 
{ 
    // jesli pusty kopiujemy 
    if (list.root == NULL) 
    { 
     this->root = NULL; 
     return; 
    } 

    //tworzenie nowego korzenia 
    this->root = new Node(NULL, list.root->wrt); 

    Node* list_currentNode = list.root; 
    Node* this_currentNode = this->root; 
    while (list_currentNode->next != NULL) 
    { 
     // tworzenie nastepnika 
     Node* newNode = new Node(NULL, list_currentNode->next->wrt); 
     this_currentNode->next = newNode; 
     this_currentNode = this_currentNode->next; 
     list_currentNode = list_currentNode->next; 
    } 
} 


void add(int wrt){ 
    Node* node = new Node(NULL, wrt); 
    Node* el = this->root; 
    while(el->next != NULL){ 
     //el->show(); 
     el = el->next; 
    } 
    el->next = node; 
} 

void remove(int index){ 
    Node* el = this->root; 
    if(index == 0){ 
     this->root = el->next; 
    // delete el; 
    } 
    else{ 
    int i = 0; 
    while(el != NULL && i < index - 1){ 

     el = el->next; 
     i++; 
    } 
    if(el!=NULL){ 
     Node* toRem = el->next; 
     Node* newNext = toRem->next; 
     el->next = newNext; 
     // delete toRem; 
    } 
} 
} 

void show(){ 
    Node* el = this->root; 
    while(el != NULL){ 
     el->show(); 
     el = el->next; 
    } 
} 

~List(){ 
    Node* currentNode = this->root; 
    while (currentNode != NULL) 
    { 
     Node* nextNode = currentNode->next; 
     delete currentNode; 
     currentNode = nextNode; 
    } 
} 


private: 
    Node* root; 

}; 

int main(){ 
    List* l = new List(10); 
    l->add(12); l->add(13); 
    l->show(); 
    cout<<endl; 
    List* lala = new List(*l); 
    lala->show(); 
    cout<<endl; 
    lala->add(4); 
    cout<<"lala before remove"<<endl; 
    lala->show(); 
    lala->remove(0); 
    cout<<"l before delete"<<endl; 
    l->show(); 
    cout<<"lala before delete"<<endl; 
    lala->show(); 
    delete l; 
    cout<<"l after"<<endl; 
    l->show(); 
    cout<<"lala after delete"<<endl; 
    lala->show(); 
    return 0; 
    } 
+4

代碼太長。 http://sscce.org/ – Fanael

+6

要求陌生人通過檢查發現代碼中的錯誤並不是富有成效的。您應該使用調試器或打印語句來識別(或至少隔離)問題,然後回來一個更具體的問題(一旦您將其縮小到10行[測試案例](http:///sscce.org))。 –

+4

另外,我想換個問題。我懷疑C++中的刪除操作是否被破壞,你可能做錯了。如果它被破壞了,我們在它的世界裏遇到了一個棘手的問題。 – Skalli

回答

1

第一部分

以下三個語句引起無限循環..

l->~List(); 
cout<<"l after"<<endl; 
l->show(); 

因爲你的析構函數~List()錯過一個重要的聲明,這是...

this->root = NULL; 

這是infinite loop的主要原因。所以在這裏你完全析構函數不用..

~List() 
{ 
    Node* currentNode = this->root; 
    while (currentNode != NULL) 
    { 
     Node* nextNode = currentNode->next; 
     delete currentNode; 
     currentNode = nextNode; 
    } 

    this->root = NULL; 
} 

第二部分

現在你有以上三條線到下面的更新...

delete l; 
cout<<"l after"<<endl; 
l->show;    // We should never write this line in general practice.. 

,並考慮到你使用功能在~List()以上。仍然程序進入infinite loop的原因是delete l將取消分配分配給l的內存。你叫l->show()(和l仍然指向到一個可訪問的線性地址),所以現在this->root指向一些垃圾位置,這就是爲什麼直到找到幸運的是​​條件NULL,它會留在無限循環。

+0

我加了它並且效果是一樣的 delete l; cout <<「l刪除後<< << endl; l(); show(); 淵源列表L爲:11,22,33 顯示打印33,22,11 – Yoda

+0

@RobertKilar:我的答案是基於'1->〜名單();'聲明。但是現在你已經把它改成了'delete l;'。前面的語句仍然保留'l'的成員,但是'delete l''也釋放分配給'l'成員的內存區域。你怎麼能期望'l-> show();'應該工作?這就是爲什麼'this-> root'指向一些亂碼區域,你的程序進入無限循環。 – Ammar

0

在幾個地方你的代碼不會考慮項目是否爲NULL。例如。什麼是根是NULL?或者在這裏是什麼? (節點* toRem = EL->未來;節點* newNext = toRem->未來;)

可能會有更大的問題,但它的一個開始......

+0

但是,如果我調用l-> remove(0)第一部分,即使它不爲null也不會工作,但忘記這個。這很奇怪。 – Yoda

+0

注意,如果這是家庭作業,但除此之外,爲什麼你要實現一個鏈表?你應該使用標準的實現。 –

0

好吧,這是列表的正確實施。我想很多人都會覺得它很有用。特別感謝你們,特別是在通訊員幫助下我的Agent_L。

正確的鏈表實現

#include <cstdio> 
#include <cmath> 
#include<iostream> 
#include <stdio.h> 
#include <string.h> 
using namespace std; 

class Node{ 
friend class List; 
public: 
    Node(Node* next, int wrt){ 
     this->next = next; 
     this->wrt = wrt; 
    } 

    Node(const Node& obiekt){ 
     this->wrt = obiekt.wrt; 
     this->next = obiekt.next; 
    } 
    //NIE MA DESTRUKTORA BO NIE ALOKUJE ZADNYCH DANYCH !!! 

    void show(){ 
     cout<<this->wrt<<endl; 
    } 




private: 
    Node* next; 
    int wrt; 

}; 


class List{ 

public: 
List(int wrt){ 
    this->root = new Node(NULL, wrt); 
} 


    List(const List& list) 
{ 
    // jesli pusty kopiujemy 
    if (list.root == NULL) 
    { 
     this->root = NULL; 
     return; 
    } 

    //tworzenie nowego korzenia 
    this->root = new Node(NULL, list.root->wrt); 

    Node* list_currentNode = list.root; 
    Node* this_currentNode = this->root; 
    while (list_currentNode->next != NULL) 
    { 
     // tworzenie nastepnika 
     Node* newNode = new Node(NULL, list_currentNode->next->wrt); 
     this_currentNode->next = newNode; 
     this_currentNode = this_currentNode->next; 
     list_currentNode = list_currentNode->next; 
    } 
} 


void add(int wrt){ 
    Node* node = new Node(NULL, wrt); 
    Node* el = this->root; 
    while(el->next != NULL){ 
     //el->show(); 
     el = el->next; 
    } 
    el->next = node; 
} 

void remove(int index){ 
    Node* el = this->root; 
    if(index == 0){ 
     this->root = el->next; 
     delete el; 
    } 
    else{ 
    int i = 0; 
    while(el != NULL && i < index - 1){ 

     el = el->next; 
     i++; 
    } 
    if(el!=NULL){ 
     Node* toRem = el->next; 
     Node* newNext = toRem->next; 
     el->next = newNext; 
     delete toRem; 
    } 
} 
} 

void show(){ 
    Node* el = this->root; 
    while(el != NULL){ 
     el->show(); 
     el = el->next; 
    } 
} 

~List(){ 
    Node* currentNode = this->root; 
    while (currentNode != NULL) 
    { 
     Node* nextNode = currentNode->next; 
     delete currentNode; 
     currentNode = nextNode; 
    } 
} 


private: 
    Node* root; 

}; 

int main(){ 
    List* l = new List(11); 
    l->add(22); l->add(33); 
    l->show(); 
    cout<<endl; 
    List* lala = new List(*l); 
    lala->show(); 
    cout<<endl; 
    lala->add(44); 
    cout<<"lala before remove"<<endl; 
    lala->show(); 
    lala->remove(1); 
    cout<<"l before delete"<<endl; 
    l->show(); 
    cout<<"lala before delete"<<endl; 
    lala->show(); 
    delete l; 
    /* cout<<"l after delete "<<endl; 
    l->show(); */ 
    cout<<"lala after delete"<<endl; 
    lala->show(); 
    return 0; 
    }