Programming Forums
User Name Password Register
 

RSS Feed
FORUM INDEX | TODAY'S POSTS | UNANSWERED THREADS | ADVANCED SEARCH

Reply
 
Thread Tools Display Modes
Old Nov 14th, 2006, 11:41 PM   #1
fhslacrosse13
Newbie
 
Join Date: Oct 2006
Posts: 25
Rep Power: 0 fhslacrosse13 is on a distinguished road
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]
fhslacrosse13 is offline   Reply With Quote
Old Nov 15th, 2006, 12:09 AM   #2
fhslacrosse13
Newbie
 
Join Date: Oct 2006
Posts: 25
Rep Power: 0 fhslacrosse13 is on a distinguished road
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}
fhslacrosse13 is offline   Reply With Quote
Old Nov 15th, 2006, 12:55 AM   #3
The Dark
Expert Programmer
 
Join Date: Jun 2005
Posts: 894
Rep Power: 4 The Dark is on a distinguished road
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;
I think the red highlighted code should be
p->prev = plist1->prev;
since you are trying to insert the node between plist1->prev and plist1.
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:
when i run it it does something weird
means nothing to me - you need to say what it is doing.
The Dark is offline   Reply With Quote
Reply

Bookmarks

« Previous Thread in Forum | Next Thread in Forum »

Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)
 
Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Forum Jump

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




DaniWeb IT Discussion Community
All times are GMT -5. The time now is 12:10 PM.

Powered by vBulletin® Version 3.7.0, Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
Copyright ©2007 DaniWeb® LLC