第一,代码不全。这属于人品问题了
第二,link solve(link & L) 这是C代码还是C++代码?
第三,为什么“输入15 1 2 3 4 5 6 7 8 9 10 20 30 40 50 60,正确输出应该是1 3 5 7 9 20 40 60 2 4 6 8 10 30 50”,第一个数 15 哪里去了?
程序代码:
#include <stdio.h>
#include <stdlib.h>
typedef struct node {
int value;
struct node* next;
} *link;
link fuck( const int arr[], size_t n )
{
link head = NULL;
link* last = &head;
for( size_t i=0; i!=n; ++i )
{
(*last) = malloc( sizeof(struct node) );
(*last)->value = arr[i];
last = &(*last)->next;
}
*last = NULL;
return head;
}
void shit( const link L )
{
for( link p=L; p; p=p->next )
printf( "%d%c", p->value, " \n"[p->next==NULL] );
}
int main( void )
{
int arr[] = { 15, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 20, 30, 40, 50, 60 };
link L = fuck( arr, sizeof(arr)/sizeof(*arr) );
shit( L );
extern link solve( link L );
solve( L );
shit( L );
}
link solve( link L )
{
link a_head=NULL, *a_last=&a_head;
link b_head=NULL, *b_last=&b_head;
size_t index = 0;
for( link p=L; p; p=p->next, ++index )
{
if( index%2 == 0 )
{
*a_last = p;
a_last = &(*a_last)->next;
}
else
{
*b_last = p;
b_last = &(*b_last)->next;
}
}
*a_last = b_head;
*b_last = NULL;
return L;
}