Recrusive Split on a linked list

Reply

Join Date: Feb 2007
Posts: 1
Reputation: alt234 is an unknown quantity at this point 
Solved Threads: 0
alt234 alt234 is offline Offline
Newbie Poster

Recrusive Split on a linked list

 
0
  #1
Feb 10th, 2007
I'm trying to write a recursive merge sort on a linked list. I'm basically stuck on the split portion of it currently. I came up with a solution quite similar to many other solutions I've found on the web. Many people say it works but so far my solution doesn't seem to. Here is my split code:
  1. LLN* LLN::split() {
  2. if(next == NULL) return NULL;
  3. LLN* ret = this->next;
  4. this->next = ret->next;
  5. ret->next = ret->next->split();
  6.  
  7. return ret;
  8. }
Now, here is my problem. This does seem to split the list, but the list I wish to return just gets lost for some reason. For example, the following data is inserted into the list...
a
b
c
d
e
f
g

When printed each list, I should have the output for one list be...
g
e
c
a

The other should be
f
d
b

The ret list should be the "f d b" one. However, when from that code snippet, I return "ret" or "this" they both give the output "g e c a." I've tried to retrieve the f d b list but it seems to be lost. I've drawn this algorithm out on paper a hundred times and I can't figure it out what is wrong. Any ideas about this?
Reply With Quote Quick reply to this message  
Join Date: Mar 2007
Posts: 1
Reputation: thinkfast is an unknown quantity at this point 
Solved Threads: 0
thinkfast thinkfast is offline Offline
Newbie Poster

Re: Recrusive Split on a linked list

 
0
  #2
Mar 24th, 2007
I think this is head of one list and ret is head of the other one, using both would give you the splitted lists
Reply With Quote Quick reply to this message  
Reply

This thread is more than three months old.
Perhaps start a new thread instead?
Message:


Thread Tools Search this Thread



About Us | Contact Us | Advertise | DaniWeb | Acceptable Use Policy | RSS Feed

©2003 - 2009 DaniWeb® LLC