-1

編集:

さて、これはリストの正しい実装です。多くの人が便利だと思います。コミュニケーターで私を助けてくれた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;
   }

Listを実装しましたが、問題があります。リストにデストラクタがありますが、正常に機能しません。メインを見て、「削除後のl」を参照してください。リストが逆方向に出力されます。より大きな問題は、内部で削除せずにメソッドを削除することですが、削除el / deleteをRemにコメント解除しようとすると、無限ループに入ります。私はすでに4時間それを修正しようとしています。一目見てください。

l-> remove(0)を呼び出すと、これらの行、特に87(100を忘れる)によってプログラムがクラッシュするのはなぜですか?

http://wklej.org/id/761056/87行目と100行目

削除メソッドとリストデストラクタは重要です

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

3 に答える 3

1

前編

次の3つのステートメントは、無限ループを引き起こしています。

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

第二部

上記の3行を次のように更新しました...

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はゴミの場所を指しているので、幸運while(el != NULL)なことに条件NULLが見つかるまで、無限ループのままになります。

于 2012-05-27T11:34:00.470 に答える
0

いくつかの場所では、コードはアイテムがNULLであるかどうかを考慮しません。例:ルートとは何ですか?または、toRemはここでNULLとは何ですか?(Node * toRem = el-> next; Node * newNext = toRem-> next;)

もっと問題があるかもしれませんが、それは始まりです...

于 2012-05-27T11:25:25.067 に答える
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;
   }
于 2012-05-27T12:01:24.000 に答える