Regi Nox
Regi Nox

Reputation: 23

C++ Linked List : It won't delete the first input

I wanna ask why my delete function won't work on the first input but on the other input such as the 2nd and so on, it does do the work. Thank you so much for the help!

void deleteStudent(){
cout<<endl;
cout<<"~DELETING A STUDENT~"<<endl;
cout<<endl;

string deletion;
cout << "Enter student ID number: ";
cin >> deletion;

student *prev = head;
student *current = head->next;

while (current)
{
    if (current->studentNum == deletion){
        prev->next = current->next;
        delete current;
        return;
    }
    prev = current;
    current = current->next;
}
if (!current){
    cout << "That value is not in the list" << endl;
}

}

Upvotes: 0

Views: 84

Answers (3)

bscharan
bscharan

Reputation: 157

From your code, it seems like variable "head" is your first node. and current corresponds to the second node.

    while (current)
    {
        if (current->studentNum == deletion){
            prev->next = current->next;
            delete current;
            return;
        }
        prev = current;
        current = current->next;
    }

In the above code, you are basically iterating from 2nd node to end of the list. and that is why you are not able to delete the first node.

Change the condition in while loop from current to prev. and take care that removing the first node is basically removing the pointer to the whole list and can result in unwanted behaviour, this can be avoided by using a check condition whether you are removing the first node and if so update the pointer corresponding to your list accordingly.

Upvotes: 0

Vlad from Moscow
Vlad from Moscow

Reputation: 311078

You are using as I call it Java-approach that is bad.

For example the code in the very beginning invokes undefined behavior

student *prev = head;
student *current = head->next;

because in general head can be equal to nullptr.

And moreover head can be the node that should be deleted. However you are skipping it.

This code snippet

student *prev = head;
student *current = head->next;

while (current)
{
    if (current->studentNum == deletion){
        prev->next = current->next;
        delete current;
        return;
    }
    prev = current;
    current = current->next;
}
if (!current){
    cout << "That value is not in the list" << endl;
}

can be rewritten the following way

student **current = &head;

while ( *current != nullptr && ( *current )->studentNum != deletion )
{
    current = &( *current )->next;
}

if ( *current != nullptr )
{
    student *tmp = *current;
    *current = ( *current )->next;
    delete tmp;
}
else
{
    cout << "That value is not in the list" << endl;
}

Upvotes: 1

Tanveer Badar
Tanveer Badar

Reputation: 5512

This line is wrong.

student *current = head->next;

See my correction below.

void deleteStudent(){
cout<<endl;
cout<<"~DELETING A STUDENT~"<<endl;
cout<<endl;

string deletion;
cout << "Enter student ID number: ";
cin >> deletion;

student *prev = head;
student *current = head;

while (current)
{
    if (current->studentNum == deletion){
        if(current == head)
           head = prev;
        prev->next = current->next;
        delete current;
        return;
    }
    prev = current;
    current = current->next;
}
if (!current){
    cout << "That value is not in the list" << endl;
}
} 

Upvotes: 0

Related Questions