该楼层疑似违规已被系统折叠 隐藏此楼查看此楼
#include
#include
struct list_node
{
int list_val;
struct list_node * list_next;
};
struct list_node * list_create( int num[], int n )
{
int i = 0;
struct list_node * relist = NULL;
for(; i < n; ++i )
{
struct list_node * tmp = (struct list_node*)malloc( sizeof(struct list_node) );
tmp->list_val = num[i];
tmp->list_next = relist;
relist = tmp;
}
return relist;
}
void list_print( struct list_node * head )
{
while( head )
{
printf( "%4d", head->list_val );
head = head->list_next;
}
printf( "\n" );
}
struct list_node * list_reverse( struct list_node * list_head )
{
struct list_node * list_new_head = list_head;
struct list_node * list_pos;
if( list_head == NULL )
return NULL;
while( list_pos = list_head->list_next )
{
list_head->list_next = list_pos->list_next;
list_pos->list_next = list_new_head;
list_new_head = list_pos;
}
return list_new_head;
}
int main( int argc, char * argv[] )
{
int num[] = { 1,2,3,4,5,6,7,8,9,0 };
struct list_node * head = list_create( num, sizeof(num)/sizeof(int) );
list_print( head );
head = list_reverse( head );
list_print( head );
return 0;
}