在C++中删除链表会导致无限循环[关闭]

2w3rbyxf  于 11个月前  发布在  其他
关注(0)|答案(3)|浏览(83)

这个问题不太可能帮助任何未来的访问者;它只与一个小的地理区域,一个特定的时刻,或者一个非常狭窄的情况有关,通常不适用于互联网的全球观众。
11年前关闭。

编辑

这是名单的正确实现。感谢你们,特别是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,但有一个问题。我在List中设置了析构函数,它不能正常工作:请查看main,并查看“删除后的l”,它确实向后打印了l list。
更大的问题是remove方法没有delelete在里面,它工作,因为它应该,但当我试图取消注解删除el/删除Rem我进入无限循环。
为什么这些行,特别是87行(忘记100行),在我调用l->remove(0)时会导致程序崩溃?
http://wklej.org/id/761056/第87和100行

remove方法和List析构函数很重要

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

o8x7eapl

o8x7eapl1#

第一部分**
以下三个语句会导致无限循环。

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()函数。still程序进入infinite loop的原因是delete l将重新分配分配给l的内存。你调用l->show()(并且由于l仍然指向可访问的线性地址),所以现在this->root指向一些垃圾位置,这就是为什么它会一直停留在无限循环中,直到它幸运地找到while(el != NULL)条件NULL。

t8e9dugd

t8e9dugd2#

在一些地方,您的代码不会考虑项是否为NULL。例如,root是NULL?或者toRem是NULL?(Node* toRem = el->next; Node* newNext = toRem->next;)
可能会有更多的问题,但这是一个开始。

hi3rlvi2

hi3rlvi23#

好的,这是正确的列表实现。我想很多人会发现它很有用。谢谢你们,特别是在通信器上帮助我的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;
   }

字符串

相关问题