Thread: How to copy nodes in linked list

  1. #1
    Registered User
    Join Date
    Sep 2006
    Posts
    7

    How to copy nodes in linked list

    Hi,

    Say the node has data structure:

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

    TDnode *node = head; (head of list)

    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->task = node->task;
       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. #2
    Registered User SKeane's Avatar
    Join Date
    Sep 2006
    Location
    England
    Posts
    234
    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. #3
    Registered User
    Join Date
    Sep 2006
    Posts
    7
    Quote 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".
    Thanks for the reply.

    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->task= node->task;
       tmp_node->notes = node->notes;
       tmp_node->next = NULL;  //Do I need this line?
       return( tmp_node );
    }

  4. #4
    Registered User SKeane's Avatar
    Join Date
    Sep 2006
    Location
    England
    Posts
    234
    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. #5
    Registered User
    Join Date
    Sep 2006
    Posts
    7
    Quote 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. #6
    Eager young mind
    Join Date
    Jun 2006
    Posts
    342
    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.
    In the middle of difficulty, lies opportunity

  7. #7
    Registered User
    Join Date
    Sep 2006
    Posts
    7
    Quote 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 );
       }
       tmp_node->task = node->task;   
    // 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. #8
    Hurry Slowly vart's Avatar
    Join Date
    Oct 2006
    Location
    Rishon LeZion, Israel
    Posts
    6,788
    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?
    All problems in computer science can be solved by another level of indirection,
    except for the problem of too many layers of indirection.
    – David J. Wheeler

  9. #9
    Eager young mind
    Join Date
    Jun 2006
    Posts
    342
    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.
    In the middle of difficulty, lies opportunity

  10. #10
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    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.
    Hope is the first step on the road to disappointment.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Adding nodes to a linked list
    By bluescreen in forum C Programming
    Replies: 4
    Last Post: 11-09-2006, 01:59 AM
  2. Replies: 5
    Last Post: 11-04-2006, 06:39 PM
  3. Anyone good with linked list.....I am not....
    By chadsxe in forum C++ Programming
    Replies: 11
    Last Post: 11-10-2005, 02:48 PM
  4. List class
    By SilasP in forum C++ Programming
    Replies: 0
    Last Post: 02-10-2002, 05:20 PM
  5. singly linked list
    By clarinetster in forum C Programming
    Replies: 2
    Last Post: 08-26-2001, 10:21 PM