-3

I know that the Create and Display operations are working perfectly fine. But there is something wrong with the delete operation which I'm not able to understand. I have rectified the logic and it seems perfect to me. Please help. I have tried debugger tool from the IDE but it's not giving any helpful response either! Suppose a create a new list. It will ask me for number of nodes and data. Lets say that I create 3 nodes with data 3, 5 and 7. Now if I display it, then "3 5 7" will be printed. Now if I want to delete the value 5 so that "3 7" will be printed, I will use the delete function and pass the value 5. But it is not happening. While run time, the program freezes without giving me any error or cursor to continue typing.

void List::deleteinbtw(int num)
{
    Node *temp=listptr, *temp1;;

    if(listptr==NULL)
        cout << "/nList is Empty!\n";

    else if(temp->data==num)
    {
        listptr=listptr->next;
        delete temp;
    }

    else
    {
        while(temp->next!=NULL)
        {
            if(temp->next->data==num)
            {
                temp1=temp->next;
                temp->next=temp1->next;
                delete temp1;
            }
        }
        temp=temp->next;
    }
}

1 Answers1

0

try this

void List::deleteinbtw(int num)

{ Node *temp=listptr, *temp1;

if(listptr==NULL)
    cout << "/nList is Empty!\n";

else if(temp->data==num)
{
    listptr=listptr->next;
    delete temp;
}

else
{
    while(temp->next!=NULL)
    {
        if(temp->next->data==num)
        {
            temp1=temp->next;
            temp->next=temp1->next;
            delete temp1;
            break;
        }
        temp=temp->next;
    }

}

}

glennmark
  • 524
  • 3
  • 13