Never ending loop in displaying LinkedList in C++

試著忘記壹切 提交于 2019-12-13 06:43:17

问题


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


回答1:


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.




回答2:


You didn't adjust your pointer to the head. Guess that's why it fails.




回答3:


When you remove the first element, you have to point the head of the list to the next element. You didn't do that here.




回答4:


In disp(), you access temp1->link, whether temp1 is NULL or not

if(temp1==NULL)
{cout<<"Empty"<<endl;
}
if(temp1->link==NULL)
{
    cout<<temp1->data<<endl;

}

Same for temp2 in remove().

node *previous; is used uninitialized, when the first node is removed.



来源:https://stackoverflow.com/questions/14118775/never-ending-loop-in-displaying-linkedlist-in-c

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!