312 Intermediate C Programming
{112
Node * arr2 = NULL ;113
while ( arr != NULL )114
{115
arr2 = List_i nsert ( arr2 , arr -> index , arr -> value );116
arr = arr -> next ;117
}118
return arr2 ;119
}120
Below is a sample main function that we can use to test our implementation.
// main . c1
#in clude < stdio .h >2
#in clude < stdlib .h >3
#in clude " sparse .h "4
int main ( i n t argc , char ** argv )5
{6
i f ( argc != 4)7
{8
return EXIT _FAILUR E ;9
}10
Node * arr1 = List_rea d ( argv [1]) ;11
i f ( arr1 == NULL )12
{13
return EXIT _FAILUR E ;14
}15
Node * arr2 = List_rea d ( argv [2]) ;16
i f ( arr2 == NULL )17
{18
List_ destroy ( arr2 );19
return EXIT _FAILUR E ;20
}21
Node * arr3 = List_ m erge ( arr1 , arr2 );22
int ret = List_save ( argv [3] , arr3 );23
List_ destroy ( arr1 );24
List_ destroy ( arr2 );25
List_ destroy ( arr3 );26
i f ( ret == 0)27
{28
return EXIT _FAILUR E ;29
}30
return EXIT _SUCCES S ;31
}32
Here is a Makefile that combines the compiling and testing.
GCC = gcc1
CFLAGS = -g - Wall - Wshadow2
LIBS =3
SOURCES = sparse . c main . c4
TARGET = main5
VALGRIND = valgrind -- tool = memc h eck -- verbose --log - file6