English 中文(简体)
删除 C++ 链接列表中的 C++ 链接列表的结果为无限循环 [闭
原标题:Delete in C++ linked list results in an infinite loop [closed]

Edit

这是对名单的正确执行, 感谢你们,特别是 私下帮我的探员

<强度 > 正确链接的执行名单

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

我已执行过清单,但有问题。我在清单中已经删除了不起作用的“毁灭者”:请看主页,看“删除后一页”,它会向后打印“I”列表。

最大的问题是去除方法 而不在内部变色 但当我试图删除 el/delete到Rem时 我进入无限循环

为什么这些线条, 特别是87条( 忘记100条) 导致程序崩溃, 当我叫 I- & gt; remove( 0) 时?

http://wklej.org/id/76056/" rel="不跟随 nofollown noreferrerr">http://wklej.org/id/76856/ Lines 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;
   }
最佳回答

<强度 > 第一部分

以下三个语句造成无穷无尽的循环。

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

因为你的毁灭者 错过了一个重要的声明, 也就是说...

this->root = NULL;

这是 infinite roil 的主要原因。 因此, 您的完整毁灭器在此运行 。

 ~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 () 函数。 仍然保留程序的原因出现在一个 < code> infinite 环 中, 原因是 < code > delete l 将解除指定给 l 的内存。 您调用 l- gt;show (和由于 < code> l/code > 仍然指向一个可访问的线性地址), 所以现在 < code> nthis- gt; root 指向一些垃圾位置, 这就是为什么直到找到幸运的 < code > 时间(! = NULLL) 状态 NULLL, 它会留在无穷处 。

问题回答

在一些地方, 您的代码不考虑项目是 NULL 。 E. 例如, 什么是 NULL? 或者什么是 NULL 是 NULL? ( 节点 * toRem = el- gt; next; 节点 * next = rem- gt; next; )

可能还有更多的问题 但是它的开始...

OK,所以这是正确执行清单。 我认为许多人会认为它有用。 谢谢你们这些在交谈中帮助我的人。

<强度 > 正确链接的执行名单

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




相关问题
Undefined reference

I m getting this linker error. I know a way around it, but it s bugging me because another part of the project s linking fine and it s designed almost identically. First, I have namespace LCD. Then I ...

C++ Equivalent of Tidy

Is there an equivalent to tidy for HTML code for C++? I have searched on the internet, but I find nothing but C++ wrappers for tidy, etc... I think the keyword tidy is what has me hung up. I am ...

Template Classes in C++ ... a required skill set?

I m new to C++ and am wondering how much time I should invest in learning how to implement template classes. Are they widely used in industry, or is this something I should move through quickly?

Print possible strings created from a Number

Given a 10 digit Telephone Number, we have to print all possible strings created from that. The mapping of the numbers is the one as exactly on a phone s keypad. i.e. for 1,0-> No Letter for 2->...

typedef ing STL wstring

Why is it when i do the following i get errors when relating to with wchar_t? namespace Foo { typedef std::wstring String; } Now i declare all my strings as Foo::String through out the program, ...

C# Marshal / Pinvoke CBitmap?

I cannot figure out how to marshal a C++ CBitmap to a C# Bitmap or Image class. My import looks like this: [DllImport(@"test.dll", CharSet = CharSet.Unicode)] public static extern IntPtr ...

Window iconification status via Xlib

Is it possible to check with the means of pure X11/Xlib only whether the given window is iconified/minimized, and, if it is, how?

热门标签