Cloning a Linked List

This is a discussion on Cloning a Linked List within the C Programming forums, part of the General Programming Boards category; Hey how do u clone a link list i just want any idea but code is welcome Thank In Advance ...

  1. #1
    Registered User
    Join Date
    Oct 2001
    Posts
    6

    Cloning a Linked List

    Hey how do u clone a link list


    i just want any idea but code is welcome


    Thank In Advance
    Pamela

  2. #2
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    Very simple stuff:
    Code:
    Node *cloneList( Node *toClone )
    {
        Node *tcIncremen = NULL;
        Node *newList = NULL;
        Node *newNode = NULL;
        Node *nlIncrement = NULL;
    
        tcIncrement = toClone;
        while( tcIncrement )
        {
            /**
            *** Allocate a new node.
            **/
            newNode = newNodeFunction( );
    
            /**
            *** Copy all of its values to the new node.
            *** WATCH OUT FOR POINTER TROUBLE!!!
            **/
    
            newNode->x = tcIncrement->x;
            ...etc etc etc...
    
            if( newList == NULL )
                newList = nlIncrement = newNode;
            else
                nlIncrement->next = newNode;
            nlIncrement = newNode;
            tcIncrement = tcIncrement->next;
        }
        return newList;
    }
    This was probably your homework, but I'm really really tired and have been up waaaaay too long, so I cut you a break and threw together some code. This SHOULD work.

    If your list contains no pointers, other than the 'next'/'prev' pointers, you could just do:

    *newNode = *tcIncrement;

    However: if you have other pointers, and you use this method, you will not make a "copy" of that data. You would in effect be "cloning" that, because both "copies" would point to the same pointer information. (In which case, save yourself some trouble, and "clone" your list this way: newList = oldList; ) Again, watch out for internal pointer trouble.

    Quzah.
    Hope is the first step on the road to disappointment.

  3. #3
    Registered User
    Join Date
    Oct 2001
    Posts
    6
    No actually it was on a test.. Thank You

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Following CTools
    By EstateMatt in forum C Programming
    Replies: 5
    Last Post: 06-26-2008, 10:10 AM
  2. Reverse function for linked list
    By Brigs76 in forum C++ Programming
    Replies: 1
    Last Post: 10-25-2006, 10:01 AM
  3. How can I traverse a huffman tree
    By carrja99 in forum C++ Programming
    Replies: 3
    Last Post: 04-28-2003, 05:46 PM
  4. Template Class for Linked List
    By pecymanski in forum C++ Programming
    Replies: 2
    Last Post: 12-04-2001, 08:07 PM
  5. singly linked list
    By clarinetster in forum C Programming
    Replies: 2
    Last Post: 08-26-2001, 10:21 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21