Posts

Showing posts with the label Program to compare two linked lists using recursion

Program to compare two linked lists using recursion

#include #include #include struct node { int data ; struct node *link ; } ; void append ( struct node **, int ) ; int compare ( struct node *, struct node * ) ; void main( ) { struct node *first, *second ; first = second = NULL ; /* empty linked lists */ append ( &first, 1 ) ; append ( &first, 2 ) ; append ( &first, 3 ) ; append ( &second, 1 ) ; append ( &second, 2 ) ; append ( &second, 3 ) ; clrscr( ) ; if ( compare ( first, second ) ) printf ( "Both linked lists are EQUAL" ) ; else printf ( "Linked lists are DIFFERENT" ) ; } /* adds a node at the end of a linked list */ void append ( struct node **q, int num ) { struct node *temp ; temp = *q ; if ( *q == NULL ) /* if the list is empty, create first node */ { *q = malloc ( sizeof ( struct node ) ) ; temp = *q ; } else { /* go to last node */ while ( temp -> link != NULL ) temp = temp -> link ; /* add node at the end */ temp -> link = malloc (...