Hey how do u clone a link list
i just want any idea but code is welcome
Thank In Advance
Pamela
Hey how do u clone a link list
i just want any idea but code is welcome
Thank In Advance
Pamela
Very simple stuff:
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.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; }
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.
No actually it was on a test.. Thank You