Hello and welcome to our community! Is this your first visit?
Enjoy an ad free experience by logging in. Not a member yet? Register.

could someone please explain to me how to edit a value in a linked list

• Originally Posted by Stowelly
could someone please explain to me how to edit a value in a linked list

• sorry ill paste the program in and try to explain it a bit better

ok basically i have a linked list which outputs the value "i" from each node. but i want to add a function which adds a value to "x" and "y" based on a user inputting numbers. but i would like to be able to access any specific node to edit these values.... hope that makes sense

Code:
```class Node
{
public:
int i;
int x;
int y;
Node *next;
Node(){i = 0; next = NULL;}
};
class List
{
public:
List(int n);
void Display();
};

int main(int argc, char* argv[])
{
List bob(10);

bob.Display();
return 0;
}
List::List(int n)
{

Node *Temp;
Node *Last;
for (int i = 0; i <n;i++)
{

{
Temp = new Node;
Temp->next = NULL;
Temp->i = i;

Last = Temp;
}
else
{Temp = new Node;
Temp->i = i;
Last->next = Temp;
Temp->next = NULL;
Last = Last->next;
}

}
}
void List::Display()
{
cout << Temp->i << "  x = " << Temp->x << "   y= " << Temp->y << "\n";

while (Temp->next != NULL)
{
cout << Temp->next->i << "  x = " << Temp->next->x << "   y= " << Temp->next->y << "\n";
Temp = Temp->next;

}
}```

• Try writing two functions

FindNode & UpdateNode

Node* FindNode(List *list,int index)
- This will go through the list and find the Node with the correct index and return the pointer to the node

void UpdateNode(Node *node, int x, int y)
- This will update the node members x and y.

Now in your main, ask the user to select a node by specifying an index, then ask him to enter new values for x and y. once you have them, call FindNode to locate the node he is interested in and update using the UpdateNode.

Let us know how it goes.

• thanks ill have a go at that abit later. im also really confused about another aspect of linked lists.

i have a program that displays a worm made up of a chain 8 segmants stored as a linked list and i want to make it so these segments can be moved to anywhere in the chain...... thus the nodes will have to move position. this is really confusing me im sure its alot easier than it looks

•

Posting Permissions

• You may not post new threads
• You may not post replies
• You may not post attachments
• You may not edit your posts
•