Crunch Posted October 6, 2004 Share Posted October 6, 2004 I have 2 linked lists 1st values: 1, 3, 5, 7, 9, 11, 13, 15, 17, 19 2nd values: 0, 2, 4, 6, 8, 10, 12, 14, 16, 18 and I need to make a 3rd linked list with those two combined am I going it the total wrong direction? Code thus far: final_head = new Node; newPtr = cpyhead; newPtr_2 = cpyhead_2; if (newPtr->item < newPtr_2->item) { final_head->item = newPtr->item; final_head->next = new Node; final_head = final_head->next; } else if (newPtr->item > newPtr_2->item) { final_head->item = newPtr_2->item; final_head->next = new Node; final_head = final_head->next; cout << final_head->item << endl; } Link to comment Share on other sites More sharing options...
0 Mouton Posted October 6, 2004 Share Posted October 6, 2004 You don't have to create new nodes for the final list, unless u really want to keep the 2 initial lists intact. You could simply change the next pointers of the existing nodes to create one list. Also, one of the if() needs to include the =; either if (... <= ...) or else if (... >= ...) Link to comment Share on other sites More sharing options...
0 Crunch Posted October 6, 2004 Author Share Posted October 6, 2004 well i decided to make it simple since the lists will never equal each other one will always be greater yes i need to keep the pointers intact Link to comment Share on other sites More sharing options...
0 Crunch Posted October 6, 2004 Author Share Posted October 6, 2004 (edited) umm in my program cpyhead and cpyhead_2 are deep copies of my 2 starting lists head and head_2 so yes I can drop the final_head and just use cpyhead as my 3rd list cpyhead_2 will just be a temp but would I just add the items from cpyhead_2 into the cpyhead list by using prev = node infront cur = node after newPtr = new Node; prev->next = newPtr newPtr->item = (item i want); newPtr->next = cur; is that the most efficent way? Edited October 6, 2004 by Crunch Link to comment Share on other sites More sharing options...
0 Crunch Posted October 6, 2004 Author Share Posted October 6, 2004 anybody Link to comment Share on other sites More sharing options...
0 Mouton Posted October 6, 2004 Share Posted October 6, 2004 well i decided to make it simple since the lists will never equal each other one will always be greater it's not called simplification. unless your problem clearly states that this application will *never* be used with equals values in both list, u need to put an = sign somewhere. otherwise, it's a pretty obvious programming error. Link to comment Share on other sites More sharing options...
0 Mouton Posted October 6, 2004 Share Posted October 6, 2004 is that the most efficent way? Why don't u use the exact same way u used to fill the 2 initial lists ? just change the loop to loop accros both lists, and instead of inserting i, u insert the item... Link to comment Share on other sites More sharing options...
Question
Crunch
I have 2 linked lists
1st values: 1, 3, 5, 7, 9, 11, 13, 15, 17, 19
2nd values: 0, 2, 4, 6, 8, 10, 12, 14, 16, 18
and I need to make a 3rd linked list with those two combined
am I going it the total wrong direction?
Code thus far:
final_head = new Node; newPtr = cpyhead; newPtr_2 = cpyhead_2; if (newPtr->item < newPtr_2->item) { final_head->item = newPtr->item; final_head->next = new Node; final_head = final_head->next; } else if (newPtr->item > newPtr_2->item) { final_head->item = newPtr_2->item; final_head->next = new Node; final_head = final_head->next; cout << final_head->item << endl; }Link to comment
Share on other sites
6 answers to this question
Recommended Posts