To begin with, you have malloc
'ed a structure which is totally unnecessary. If the user enters no data, you can return an empty list indicated by root = NULL
. So, the initial part could be:
root = ( struct node * ) NULL;
if ( argc < 2 )
return ( 0 );
As your code stands, the first node will always have 0 for root->grade
. In the loop, you should allocate the memory and convert the argv[i]
to integer using atoi
. Finally, your loop should start with i=1
because argv[0]
is the command name itself.