This program maintains a linked list using recursive function. addatend( ), display( ), compare( ), copy( ), length( ) are all recursive functions. compare( ) finds out whether the two linked lists possess same elements. copy( ) copies elements from one linked list to another. length( ) counts the number of nodes present in the linked list. But there are some limitations while using recursion. Since there are number of calls to the same function, the program tends to become slow. Also, same type of variables are created recursively at each call. So consumption of memory is considerable. Above all, there is chance of stack overflow if the calls are too many.

#include "alloc.h"

/* structure containing a data part and link part */

struct node

{

int data ;

} ;

main ( )

{

struct node *p, *first, *second ;

clrscr( ) ;

p = first = second = NULL ; /* empty linked list */

display( p ) ;

printf ( "\nLength of linked list = %d\n", length ( p ) ) ;

/* code to compare two linked lists using recursion */

display( first ) ;

display( second ) ;

if ( compare ( first, second ) )

printf ( "\nBoth linked lists are EQUAL" ) ;

else

printf ( "Linked lists are DIFFERENT" ) ;

/* copy one linked list into another */

display ( first ) ;

printf ( "\n" ) ;

copy ( first, &second ) ;

display ( second ) ;

printf ( "\n" ) ;

display ( p ) ;

}

/* counts the number of nodes in a linked list */

length ( struct node *q )

{

static int l ;

/* if list is empty or if NULL is encountered */

if ( q == NULL )

return ( 0 ) ;

else

{

/* go to next node */

l = 1 + length ( q -> link ) ;

return ( l ) ;

}

}

/* compares 2 linked lists and returns 1 if linked lists are equal and 0 if unequal */

compare ( struct node *q , struct node *r )

{

static int flag ;

if ( ( q == NULL ) && ( r == NULL ) )

flag = 1 ;

else

{

if ( q == NULL || r == NULL )

flag = 0 ;

if ( q -> data != r -> data)

flag = 0 ;

else

}

return ( flag ) ;

}

/* copies a linked list into another */

copy ( struct node *q, struct node **s )

{

if ( q != NULL )

{

*s = malloc ( sizeof ( struct node ) ) ;

( *s ) -> data = q -> data ;

( *s ) -> link = NULL ;

copy ( q->link, & ( ( *s ) -> link ) ) ;

}

}

/* displays the contents of the linked list */

display ( struct node * q )

{

if ( q != NULL )

{

printf ( " %d", q -> data ) ;

display ( q -> link ) ;

}

else

return ;

}

/* adds a new node at the end of the linked list */

addatend ( struct node **s, int num )

{

if ( *s == NULL )

{

*s = malloc ( sizeof ( struct node ) ) ;

( *s ) -> data = num ;

( *s ) -> link = NULL ;

}

else