Reputation: 51
I have a function that needs to replace a node of a specified value in the list (old), and replace that node with a new value specified by the user (new). This is what I've come up with, but it makes my program stop working so something's wrong with what I've conjured up.
node *modifyNode(node *L, int old, int new)
{
while (L != NULL)
{
if(L->val == old)
{
L->val = new;
}
}
L = L->next;
}
Upvotes: 2
Views: 9217
Reputation: 21
new is a keyword in c++, can not be used as a variable name
node *modifyNode(node *L, int old, int _new)
{
while (L != NULL)
{
if(L->val == old)
{
L->val = _new; //new is a keyword
}
L = L->next; //reposition this here
}
}
Upvotes: 0
Reputation: 786
position of the L = L->next;
should be in the loop
node *modifyNode(node *L, int old, int new)
{
while (L != NULL)
{
if(L->val == old)
{
L->val = new;
}
L = L->next; // this should be position to refer next node in the loop
}
}
Upvotes: 1