This is the recursive insert method I wrote in a previous webpage: click here
class List // I want to use 1 prgram file for this example
{
int value;
List next;
}
public class demo
{
public static void main(String[] args)
{
List head = null; // Empty list
List p;
/* -------------------------------------------------
Create a list element and insert in List at head
------------------------------------------------- */
for( int i=1; i <= 10; i++ )
{
p = new List( );
p.value = i;
head = insert(head, p); // Insert p in head
print(head);
}
}
/* =========================================
insert(h, e): insert e at tail of list h
return new list
A List variable is a reference variable !!!
========================================= */
public static List insert( List head, 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
}
}
// Print the list
public static void print(List h)
{
while (h != null)
{
System.out.printf("%d ", h.value);
h = h.next;
}
System.out.printf("\n\n");
}
}
|
The final result is:
#include <stdio.h>
#include <stdlib.h>
struct List // This is the List class definition in C
{
int value;
struct List *next;
};
struct List *insert( struct List *head, struct List *e );
void print(struct List *h);
void main(int argc, char *argv[])
{
struct List *head = NULL; // Nul pointer NULL instead of null
struct List *p;
/* -------------------------------------------------
Create a list element and insert in List at head
------------------------------------------------- */
for ( int i=1; i <= 4; i++ )
{
p = malloc( sizeof(struct List) );
p->value = i;
head = insert(head, p); // Insert p in head
print(head);
}
}
/* =======================================================
insert(head, e): insert at tail
======================================================= */
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
}
}
// Print the list
void print(struct List *h)
{
while (h != NULL)
{
printf("%d ", h->value);
h = h->next;
}
printf("\n\n");
}
|
|