Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations SkipVought on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

adding one list to the other

Status
Not open for further replies.

anatazi

Programmer
Jun 24, 2002
33
0
0
US
How do write the loop to add one list to the other; add each node to the list?
 
Well, if you don't need to make a copy of the list that you're adding, all you need to do is make the last node of the first list have its "next" member (or whatever you call it) point to the first node of the second list.

If you need to make copies of all the nodes in the second list, then you would do something like this:

node *pEndofFirstList, *pCurNode;

pEndofFirstList = &FirstList;
while ( pEndofFirstList->next ){
pEndofFirstList = pEndofFirstList->next;
}

pCurNode = &SecondList;
while ( pCurNode ) {
pEndofFirstList->next = new node; // or use malloc
pEndofFirstList->next->prev = pEndofFirstList;
pEndofFirstList = pEndofFirstList->next;
// copies all the data from pCurNode to pEndofFirstList
memcpy(pEndofFirstList, pCurNode, sizeof(node));
pEndofFirstList->next = NULL;

pCurNode = pCurNode->next;
}
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top