944,082 Members | Top Members by Rank

Ad:
  • C Discussion Thread
  • Unsolved
  • Views: 3112
  • C RSS
Feb 10th, 2007
0

Recrusive Split on a linked list

Expand Post »
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?
Reputation Points: 10
Solved Threads: 0
Newbie Poster
alt234 is offline Offline
1 posts
since Feb 2007
Mar 24th, 2007
0

Re: Recrusive Split on a linked list

I think this is head of one list and ret is head of the other one, using both would give you the splitted lists
Reputation Points: 10
Solved Threads: 0
Newbie Poster
thinkfast is offline Offline
1 posts
since Mar 2007

This thread is more than three months old

No one has posted to this discussion for at least three months. Please let old threads die and do not reply to them unless you feel you have something new and valuable to contribute that absolutely must be added to make the discussion complete. Otherwise, please start a new thread in this forum instead.
Message:
Previous Thread in C Forum Timeline: Help with child counter
Next Thread in C Forum Timeline: Returning An Array





About Us | Contact Us | Advertise | Acceptable Use Policy
Forum Index | Build Custom RSS Feed


Follow us on Twitter


© 2011 DaniWeb® LLC