Inserting a list element at the tail of a linked list
 

My problem:

 

Inserting a list element at the tail of a linked list
 

The solution of my problem is:

We need to find an (recursive) algorithm that solves My Problem.

Designing a recursive algorithm to insert at tail
 

Consider the smaller problem insert(head->next, e):

 

Designing a recursive algorithm to insert at tail
 

The solution of insert(head->next, e) is:

 

Designing a recursive algorithm to insert at tail
 

Suppose we have the solution of insert(head->next, e) avialable:

How can we use this solution to solve our original problem ?

Designing a recursive algorithm to insert at tail
 

Answer:

So:   what is the (recursive) algorithm ?

Designing a recursive algorithm to insert at tail
 

The recursive algorithm:

helpSol = insert( h->next, e ) ; --- ask someone else to solve this smaller problem
 

Designing a recursive algorithm to insert at tail
 

The recursive algorithm:

helpSol = insert( h->next, e ) ; --- ask someone else to solve this smaller problem
head->next = helpSol ; --- use the smaller solution to solve my problem !

Base case for insert at tail

Base case: (insert in an empty list)

Solution:

Algorithm:
             e->next = NULL;
             return e;

Recursive algorithm to insert at tail of a linked list

   struct List *insert( struct List *head, struct List *e )
   {
      struct List *helpSol;

      /* --------------------------------------------
          Base case: insert at the tail of an empty
         -------------------------------------------- */
      if ( head == null )
      {
            
         e->next = null;     // Mark e as the last list elem
         return(e);         // e is the first list elem !
            
      }
      else
      {
         /* ===========================================================
            Solve the problem USING the solution of a smaller problem
            =========================================================== */

	    
         helpSol = insert( head->next, e ); // Have "someone else" solve
                                           // this smaller problem

	    
         head->next = helpSol;    // Find MY solution using helpSol
         return head;             // Return MY solution
      }
   } 

DEMO:   /home/cs255001/demo/C/Recursion/insert-tail.c

Simplified recursive algorithm to insert at tail of a linked list

   struct List *insert( struct List *head, struct List *e )
   {
      /* --------------------------------------------
          Base case: insert at the tail of an empty
         -------------------------------------------- */
      if ( head == null )
      {
         e->next = null;     // Mark e as the last list elem
         return(e);         // e is the first list elem !
      }
      else
      {
         /* ===========================================================
            Solve the problem USING the solution of a smaller problem
            =========================================================== */
         head->next = insert( head->next, e ); // Link directly to helpSol

         return head;             // Return MY solution
      }
   }