Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
274 views
in Technique[技术] by (71.8m points)

oop - I have created function to modify a node in Linkedlist in C++ but its not working:


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

For starters the variable pos is not used within the function.

Secondly the condition of the while loop

while(curr->next!=NULL) {

is incorrect and in general can invoke undefined behavior because the pointer header can be equal to nullptr. And moreover if the list contains only one node pointed to by the pointer header and its data member isbn is equal to the value of the variable old it will not be changed.

The function should not output any message.

The function can look the following way

void update_data( int old, int new_data ) 
{//Function toupdate node
    for ( Node *curr = header; curr != nullptr; curr = curr->next ) 
    {
        if ( curr->isbn == old )
        {
            curr->isbn = new_data;
        }
    }
}

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...