...

View Full Version : Linked Lists



Stowelly
04-28-2004, 10:08 PM
could someone please explain to me how to edit a value in a linked list

Roy Sinclair
04-28-2004, 11:07 PM
could someone please explain to me how to edit a value in a linked list

Details! We can't help you if you don't provide enough details!

Stowelly
04-28-2004, 11:14 PM
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





class Node
{
public:
int i;
int x;
int y;
Node *next;
Node(){i = 0; next = NULL;}
};
class List
{
public:
Node *Head;
List() {Head = NULL;}
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;
Head = NULL;
for (int i = 0; i <n;i++)
{

if (Head == NULL)
{
Temp = new Node;
Head = Temp;
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()
{
Node *Temp = Head;
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;

}
}

Unit
04-29-2004, 02:31 AM
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.

Stowelly
04-29-2004, 03:17 AM
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



EZ Archive Ads Plugin for vBulletin Copyright 2006 Computer Help Forum