-
-
Save paranoidxc/464615950a11f994423e to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/* | |
链表排序 | |
*/ | |
#include <stdlib.h> | |
typedef struct node* link; | |
struct node | |
{ | |
int item; | |
link next; | |
}; | |
main( int argc, char *argv[] ) { | |
int N = 10, i; | |
struct node heada, headb; | |
link t, u, x, a = &heada, b; | |
for ( i = 0, t = a; i < N; i++ ) { | |
t = t->next = malloc( sizeof *t ); | |
// printf("%p\n", &t ); | |
// printf("%p\n", &(t->next) ); | |
//t = t->next; | |
t->next = NULL; | |
t->item = rand() % 1000; | |
} | |
// ouput link a | |
for ( t = a->next; t != NULL; t = u ) { | |
printf("%d\n", t->item ); | |
u = t->next; | |
} | |
printf("\n" ); | |
printf("\n" ); | |
// reorder link a | |
b = &headb; b->next = NULL; | |
for ( t = a->next; t != NULL; t = u ) { | |
u = t->next; | |
//printf("%d\n", b->item ); | |
for( x = b; x->next != NULL; x = x->next ) { | |
if( x->next->item > t->item ) break; | |
} | |
//printf("%d\n", x->item ); | |
t->next = x->next; | |
x->next = t; | |
} | |
printf("\n" ); | |
printf("\n" ); | |
// ouput reorder link a | |
for ( t = a->next; t != NULL; t = u ) { | |
printf("%d\n", t->item ); | |
u = t->next; | |
} | |
system("pause"); | |
} | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment