Thread: SortedMerge() using recursion

  1. #1
    Registered User
    Join Date
    Jul 2005
    Posts
    1

    SortedMerge() using recursion

    I have come upon a code, which entails recurcion, while I was studying linked lists. I don't know if the guy wrote the code the right way it's supposed to be. It handles the first case scenario --if one of the lists to be merged is is empty-- yet in further passes I don't see a condition to stop the recursion properly.
    The condition that will stop the recurcion felt awkward to me and I thought I could ameliorate the code and come up with a better one. I don't know though if mine is perfectly correct.

    Below this code I tried to improve on it in my own fashion, wrong however it might be.

    Code:
    struct node *SortedMerge3(struct node *a, struct node *b)
    {
         struct node *result;
    
         if(a == NULL){                        
    	  return b;
         }
         else if(b == NULL){
    	  return a;
         }
    	  
    
         if(a->data <= b->data){
    	  result = a;
    	  result->next = SortedMerge3(a->next, b);
         }
    
         else{
    	  result = b;
    	  result->next = SortedMerge3(b->next, a);
    
    
    	  return(result);
         }
    
    
    }
    
    
    
    struct node *SortedMerge3(struct node *a, struct node *b)
    {
         struct node *result;
         struct node dummy;
         result = &dummyl;
         dummy->next = NULL;
    
    
         if(a == NULL){
    	  result->next = b;
    	  result = result->next;
    	  return result;
         }
         else if(b == NULL){
    	  result->next = a;
    	  result = result->next;
    	  return result;
         }
    	  
    
         if(a->data <= b->data){
    	  result = a;
    	  result->next = SortedMerge3(a->next, b);
         }
    
         else{
    	  result = b;
    	  result->next = SortedMerge3(b->next, a);
        }
    }

  2. #2
    Frequently Quite Prolix dwks's Avatar
    Join Date
    Apr 2005
    Location
    Canada
    Posts
    8,057
    So which one was the original version? The first one?

    [edit]What's that dummy stuff for?[/edit]
    Last edited by dwks; 07-19-2005 at 04:06 PM.
    dwk

    Seek and ye shall find. quaere et invenies.

    "Simplicity does not precede complexity, but follows it." -- Alan Perlis
    "Testing can only prove the presence of bugs, not their absence." -- Edsger Dijkstra
    "The only real mistake is the one from which we learn nothing." -- John Powell


    Other boards: DaniWeb, TPS
    Unofficial Wiki FAQ: cpwiki.sf.net

    My website: http://dwks.theprogrammingsite.com/
    Projects: codeform, xuni, atlantis, nort, etc.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Template Recursion Pickle
    By SevenThunders in forum C++ Programming
    Replies: 20
    Last Post: 02-05-2009, 09:45 PM
  2. Recursion... why?
    By swgh in forum C++ Programming
    Replies: 4
    Last Post: 06-09-2008, 09:37 AM
  3. a simple recursion question
    By tetra in forum C++ Programming
    Replies: 6
    Last Post: 10-27-2002, 10:56 AM
  4. To Recur(sion) or to Iterate?That is the question
    By jasrajva in forum C Programming
    Replies: 4
    Last Post: 11-07-2001, 09:24 AM
  5. selection sorting using going-down and going-up recursion
    By Unregistered in forum C Programming
    Replies: 1
    Last Post: 11-02-2001, 02:29 PM