动态行主序。
链表写法以后你自己学到链表自己写吧。
而且你的题目要求是数组,用链表就不符合要求了。
没有释放内存,没有必要,反正只运行一次。
程序代码:
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#define INITSIZE 5
#define LINESIZE 101
int
getbookname( char *dst, int size );
int
main( void )
{
char **s, **temp;
char t[ LINESIZE ];
int ix, i, j;
int currentsize;
int ch;
currentsize = INITSIZE;
s = ( char ** )malloc( currentsize * sizeof( char * ) );
if( NULL == s )
return EXIT_FAILURE;
for( ix = 0; ix < currentsize; ++ix )
{
s[ ix ] = ( char * )malloc( LINESIZE * sizeof( char ) );
if( NULL == s[ ix ] )
return EXIT_FAILURE;
}
for( ix = 0;;++ix )
{
if( ix == currentsize )
{
currentsize += INITSIZE;
temp = ( char ** )realloc( s, currentsize * sizeof( char * ) );
if( NULL == temp )
return EXIT_FAILURE;
s = temp;
for( i = ix; i < currentsize; ++i )
{
s[ i ] = ( char * )malloc( LINESIZE * sizeof( char ) );
if( NULL == s[ i ] )
return EXIT_FAILURE;
}
}
if( EOF == ( ch = getbookname( s[ ix ], LINESIZE ) ) )
break;
}
for( i = 0; i < ix; ++i )
for( j = i + 1; j < ix; ++j )
if( 0 < strcmp( s[ i ], s[ j ] ) )
{
strcpy( t, s[ i ] );
strcpy( s[ i ], s[ j ] );
strcpy( s[ j ], t );
}
for( i = 0; i < ix; ++i )
printf( "%s", s[ i ] );
return 0;
}
int
getbookname( char *dst, int size )
{
int ch;
int ix;
for( ix = 0; ix < size - 1 && EOF != ( ch = getchar() ) && '\n' != ch; ++ix )
dst[ ix ] = ch;
if( '\n' == ch )
dst[ ix++ ] = ch;
dst[ ix ] = '\0';
return ch;
}