从来没有在C ++中结束显示LinkedList的循环 [英] Never ending loop in displaying LinkedList in C++

查看:114
本文介绍了从来没有在C ++中结束显示LinkedList的循环的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在下面的代码中,当我尝试在删除第一个元素后显示列表,它导致下一个元素的无限循环。当除去第一个元素时,不会发生此问题。我没有线索为什么这是发生?有人可以告诉我错误在哪里?

 #include< iostream& 

using namespace std;

类节点
{
public:
int data;
node * link;

};

class linkedlist
{node * head;
public:
linkedlist()
{
head = NULL;
}
int add(int data1)
{node * insertnode = new node;
insertnode-> data = data1;
insertnode-> link = NULL;

node * temp = head;

if(temp!= NULL)
{
while(temp-> link!= NULL)
{
temp = temp->链接;
}
temp-> link = insertnode;

}
else {head = insertnode;}

}
void disp()
{node * temp1 = head;
cout<< endl;
if(temp1 == NULL)
{cout<<Empty<< endl;
}
if(temp1> link == NULL)
{
cout<< temp1-> data<< endl;

}
else {
do {cout<< temp1-> data<< endl;
temp1 = temp1-> link;
}
while(temp1!= NULL);
}

}

int remove(int removedata)
{
node * temp2 = head;
if(temp2 == NULL)
{}

if(temp2-> link == NULL)
{
delete temp2;
head = NULL;
}

else
{
node * previous;
do
{
if(temp2-> data == removedata)break;
previous = temp2;
temp2 = temp2> link;
} while(temp2!= NULL);

previous-> link = temp2> link;
delete temp2;
}


}



};

int main()
{
链表列表;
list.add(10);
list.add(100);
list.add(200);
list.remove(10);
list.disp();
}

谢谢大家。

$ p

pre> node * previous;
do
{
if(temp2-> data == removedata)break;
previous = temp2;
temp2 = temp2> link;
} while(temp2!= NULL);

previous-> link = temp2> link;
delete temp2;

如果第一个元素是匹配的元素,循环将永久终止,导致 previous ,而不更改 temp2 (仍然是头)。
$ b

您现在要做的是:


  1. 设置 previous->链接为一些值。请注意,这是未定义的行为,因为您不知道之前是什么。

  2. delete temp2 - 这是头元素,但从不改变头。这将导致列表中的第一个元素未定义,一旦访问它,任何事情都可能发生(再次,未定义的行为)。

所以,基本上 - 删除第一个元素后 - 当你尝试显示列表,任何事情都可能发生 - 包括你遇到的无限循环。


In the following code when i try to display the list after removing the first element, its resulting in endless loop of next elements. This problem doesn't occur when other than first element is removed. I have no clue why is this happening? Can someone please tell me where is the mistake?

#include<iostream>

using namespace std;

class node
{
public:
  int data;
  node *link;

};

class linkedlist
{  node *head;
public:
linkedlist()
{
   head=NULL;
}
    int add(int data1)
    { node *insertnode=new node;
        insertnode->data=data1;
        insertnode->link=NULL;

        node *temp=head;

        if(temp!=NULL)
        {
            while(temp->link!=NULL)
            {
                temp=temp->link;
            }
            temp->link=insertnode;

        }
        else{head=insertnode;}

    }
    void disp()
    { node *temp1=head;
    cout<<endl;
    if(temp1==NULL)
    {cout<<"Empty"<<endl;
    }
    if(temp1->link==NULL)
    {
        cout<<temp1->data<<endl;

    }
    else{
    do{cout<<temp1->data<<endl;
    temp1=temp1->link;
    }
    while(temp1!=NULL);
    }

    }

    int remove(int removedata)
    {
        node *temp2=head;
        if(temp2==NULL)
         {}

         if(temp2->link==NULL)
         {
             delete temp2;
             head=NULL;
         }

         else
         {
             node *previous;
             do
             {
                 if(temp2->data==removedata) break;
                 previous=temp2;
                 temp2=temp2->link;
             }while(temp2!=NULL);

            previous->link=temp2->link;
             delete temp2;
         }


    }



};

int main()
{
   linkedlist list;
   list.add(10);
   list.add(100);
   list.add(200);
   list.remove(10);
   list.disp();
}

Thank you all. The problem has been solved

解决方案

Have a look on your deletion:

    node *previous;
         do
         {
             if(temp2->data==removedata) break;
             previous=temp2;
             temp2=temp2->link;
         }while(temp2!=NULL);

        previous->link=temp2->link;
         delete temp2;

If the first element is the matched one, the loop will immidaiately terminate, resulting in uninitializing previous, and not changing temp2 (it is still the head).

What you will now do is:

  1. set previous->link to some value. Note that this is undefined behavior, since you have no idea what previous is.
  2. delete temp2 - which is the head element, but never changing the head. This will result that the first element in the list is undefined, and once accessing it anything can happen (again, undefined behavior).

So, basically - after you delete the first element - when you try to display the list, anything can happen - including the infinite loop you are encountering.

这篇关于从来没有在C ++中结束显示LinkedList的循环的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆