deleting an item in cicular linked list

143 views Asked by At

My program is supposed to do 3 operations:

  1. Insert
  2. Delete
  3. Show on a circular linked list.

My problem is in the delete function. here is the code:

void c_list::del()
{
    int num;
    if(isempty())
        cout<<"List is Empty!"<<endl;
    else
    {
        node *temp1=first;
        node *temp2=NULL;
        cout<<"Enter the number that u want to DELETE:"<<endl;
        cin>>num;
        while(temp1->next!=first && temp1->info != num)
        {
            temp2=temp1;
            temp1=temp1->next;
        }
        if(num != temp1->info )
            cout<<"your number was not found in the list"<<endl;
        else
        {
            if(temp2!=NULL)
            {
                temp2->next=temp1->next;
                cout<<temp1->info<<" was deleted"<<endl;        
            }
            else
            {
                first=temp1->next;
                cout<<temp1->info<<"was deleted"<<endl;
            }
        }
    }
    system("pause");
}

Delete function is working in this way: user enters a number, the program searches that number & when it founds the number, removes it from the list.

Now the problem is that, when the user enters a number that does not exist in the list, the "App crash window" appears(I mean this window:Program is not responding), while I have a provided an error message for this case("your number was not found in the list")!!

Can u tell me what the problem is?

3

There are 3 answers

3
Speed8ump On BEST ANSWER

Your insert routine is not creating a circular list. When the list is empty and the initial item is inserted first == NULL. In this case your code leaves the list in a non-circular state. Because:

    newitem->next=first;
    if(first==NULL)
        first=newitem;

At this point first->next == NULL, which should never be the case in a circular list. Your search code fails whenever the item to be found does not exist in the list. This is because it never cycles back around to the first node, since the list is not circular.

1
Luca Davanzo On

Happen that, if you insert a number that is not in list, you have a loop in the first while.

So:

node* temp1 = first;
node* temp2 = 0; 
while(temp1->next!=first && !temp2) {
  if(temp1->info == num) {
     /* save pointer and exit from while */
     temp2 = temp1;
  } else {
    temp1 = temp1->next;
  }
}

Then your code produce garbage because you never call a delete.

Most likely the problem is on the insert method, where maybe, you don't assign correcly pointers.

And then, why system("pause"); ? Take a look here.

2
Validus Oculus On

I think in your while loop you are reaching to the end of list and after below line temp1 gets NULL.

temp1=temp1->next;

Then you are trying to read info attribute from null pointer and this causes error.

if(num != temp1->info )

I know you said it is circular list but i am not sure it is implemented correctly or not. My suggestion is just try to print temp1->info after while loop to be sure that correctness of list and your implementation.