![]() |
|
![]() |
|
|
Thread Tools | Display Modes |
|
|
#1 |
|
Newbie
Join Date: Oct 2006
Posts: 25
Rep Power: 0
![]() |
dnode problem
i have a problem with a free function. It is suppost to merge two lists together but when i run it it does something weird that i cant figure out
also, i have some code commented out and replaced so just look over that. [HTML]#include <iostream> #include "d_nodel.h" using namespace std; //free function merge void merge(dnode<int> *list1, dnode<int> *list2); int main(){ int arrA[] = {1,2,6,9,12,23,33,45,55,68,88,95}; int arrB[] = {2,8,12,25,33,48,55,75,105}; dnode<int> *list1 = new dnode<int>; dnode<int> *list2 = new dnode<int>; for (int i = 0; i < 12; i++) insert(list1, arrA[i]); for (int j = 0; j < 9; j++) insert(list2, arrB[j]); writeDLinkedList(list1); cout<<endl; writeDLinkedList(list2); cout<<endl; merge(list1, list2); writeDLinkedList(list1); return 0; } //implement merge void merge(dnode<int> *list1, dnode<int> *list2){ dnode<int> *plist1 = list1->next, *plist2 = list2->next, *p; while (plist1 != list1 && plist2 != list2){ if (plist1->nodeValue < plist2->nodeValue) plist1 = plist1->next; else { /*p = plist2->next; plist2->prev = plist1->next; plist2->next = plist1->prev; plist2 = p;*/ p = plist2; plist2 = plist2->next; p->prev->next = p->next; p->prev = plist1->prev->next; p->next = plist1; plist1->prev->next = p; plist1->prev = p; } } if (plist1 != list1); else{ while (plist2 != list2){ /*p = plist2->next; plist2->prev = plist1->next; plist2->next = plist1->prev; plist2 = p;*/ p = plist2; plist2 = plist2->next; p->prev->next = p->next; p->prev = plist1->prev->next; p->next = plist1; plist1->prev->next = p; plist1->prev = p; } } }[/HTML] |
|
|
|
|
|
#2 |
|
Newbie
Join Date: Oct 2006
Posts: 25
Rep Power: 0
![]() |
i dont think i was descriptive enough with what merge does. It takes to two lists and comparing them and making them one list. when merge finishes list2 should be empty and list1 should be: {1,2,6,8,9,12,23,25,33,45,48,55,68,75,88,95,105}
|
|
|
|
|
|
#3 | |
|
Expert Programmer
Join Date: Jun 2005
Posts: 894
Rep Power: 4
![]() |
In this code
p = plist2; plist2 = plist2->next; p->prev->next = p->next; p->prev = plist1->prev->next; p->next = plist1; plist1->prev->next = p; plist1->prev = p; p->prev = plist1->prev; Also, in the blue highlighted code, you are setting the p->prev->next to p->next to fix the forward link, but you also need to fix up p->next->prev to point to p->prev to fix up the backward link. Both of these would only be stuffing up the reverse links, which may not be causing your problem. Can you post the .h file so I can try it out through a debugger? Also Quote:
|
|
|
|
|
![]() |
| Bookmarks |
| Currently Active Users Viewing This Thread: 1 (0 members and 1 guests) | |
| Thread Tools | |
| Display Modes | |
|
|
Similar Threads
|
||||
| Thread | Thread Starter | Forum | Replies | Last Post |
| Stuck with a C problem | Polaris | C++ | 8 | Aug 19th, 2006 4:30 PM |
| Huge arrays in C (game-oriented problem theme) | Rather Generic | C | 6 | Mar 19th, 2006 2:09 AM |
| cgi/perl script + IE problem | joyceshee | Perl | 2 | Jan 24th, 2006 12:10 PM |
| Variable array problem | Hintshigen | C | 6 | Apr 10th, 2005 3:35 PM |
| string problem when passing in linked list | quantz | C++ | 0 | Feb 27th, 2005 11:11 AM |