Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 9061655
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 16, 20262026-06-16T15:32:26+00:00 2026-06-16T15:32:26+00:00

In the following code when i try to display the list after removing the

  • 0

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 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-16T15:32:27+00:00Added an answer on June 16, 2026 at 3:32 pm

    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.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I try to user the following code to display my list in a descending
I am using the following code to try and display in a time in
Using following code I try to get updated list of checkbuttons' corresponding text values,
I use the following code to display list of contacts, and then retrieve contact
I've got the following code: try { ... try { // This is *never*
I have the following code: try: redirect_url = self.auth.get_authorization_url() webbrowser.open_new(redirect_url) except tweepy.TweepError: print 'Error!
I use the following code try to create an array of string vectors, I
We have following code: try { // some code throwing MyException } catch (MyException
I am creating date using following code try { newdatetime = new DateTime(2012, 2,
Hy ... I have the following code : try { Nrtrde record = new

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.