1. ## How to copy nodes in linked list

Hi,

Say the node has data structure:

Code:
```struct tdnode {
char *notes;
TDnode *next;
};```
and "node" is declared as

Now I have to scan through each and every node in the list. The nodes inside this list are called "nodes", correct? If so, how do you copy the node "node" to a node called "tmp"?

1) tmp = node; will not "work" because all that is saying is that "tmp" points to the same node that "node" is pointing to. So if I change the data inside the "node" node then *tmp returns the changed "node" instead of the original "node" before change.

I was thinking of:

Code:
```while( ( node != NULL ) ) {
tmp->notes = node->notes;
node = node->next;
}```
Basically from the above, what I'm trying to do is copy every node called "node" into another node called "tmp" (I know that "node" and "tmp" are pointers, but I hope at the moment I can' think of a more concise way to address my problem).

Pardon my confusing question.

Any help'll be appreciated.

2. To make a copy of a node you'd have to allocate some memory to hold the "copy". Then assign each element in the "copy" the value of the equaivalent element in the "original".

3. Originally Posted by SKeane
To make a copy of a node you'd have to allocate some memory to hold the "copy". Then assign each element in the "copy" the value of the equaivalent element in the "original".

Can you please check if the following is correct, if I wish to do what you said and what I want to do?
(obviously I would need function protocol and everything, but just the relevant bits: )

Code:
```tmp = get_node(); //calling the following function

TDnode * get_node( *node) // would this be the correct argument?
{
TDnode *tmp_node =(TDnode *)malloc( sizeof( TDnode ));
if( tmp_node == NULL ) {
fprintf(stderr,"Error: memory allocation failed.\n");
exit( 1 );
}
tmp_node->notes = node->notes;
tmp_node->next = NULL;  //Do I need this line?
return( tmp_node );
}```

4. Code:
`tmp_node->next = NULL;  //Do I need this line?`
Do you just want a copy of the data in the node, or a proper copy of the node? It depends on what you are going to do to (a) the original node and (b) the copy.

5. Originally Posted by SKeane
Code:
`tmp_node->next = NULL;  //Do I need this line?`
Do you just want a copy of the data in the node, or a proper copy of the node? It depends on what you are going to do to (a) the original node and (b) the copy.
I don't see how they're different...the original node and the copy.

If it's any help, basically I wish to edit the new tmp_node without affecting the original node.

6. Are you trying to create a duplicate list here? If so, you will have to take care of the tmp_next also. But if you are only trying to copy a node for further reference , I dont think you need to worry much about it.

7. Originally Posted by kris.c
Are you trying to create a duplicate list here? If so, you will have to take care of the tmp_next also. But if you are only trying to copy a node for further reference , I dont think you need to worry much about it.
If I just wish to create a copy of one single node (instead of a list), then would this be correct:

Code:
```tmp = get_tmp( node );

TDnode * get_tmp( TDnode *node ) // I'm not sure of the argument?
{
TDnode *tmp_node =( TDnode *)malloc( sizeof( TDnode ));
if( tmp_node == NULL ) {
fprintf(stderr,"Error: memory allocation failed.\n");
exit( 1 );
}
// or should all the following be tmp->data instead of tmp_node->data, or does it not matter?
tmp_node->notes = node->notes;
return( tmp_node );
}```

8. it depends on your goal

Do you want a taks to point an old memory location, or duplicate also the string the task is pointing and point the new location, so the new node and old one be not correlated in any way?

9. I feel it shoud do the job..
You need to take care that some time later , if you are trying to update a particular node with the value you have stored in this node, you dont update the "next" field also as this is going to contain junk and you might end up losing the rest of your list.

10. Code:
```struct foo * dupe( struct foo *bar )
{
struct foo *baz = malloc( sizeof *bar );
if( baz && bar )
*baz = *bar;
return baz;
}```
Now then you've just made a copy of that structure.

Quzah.