Skip to content

Instantly share code, notes, and snippets.

@paranoidxc
Last active August 29, 2015 14:01
Show Gist options
  • Save paranoidxc/464615950a11f994423e to your computer and use it in GitHub Desktop.
Save paranoidxc/464615950a11f994423e to your computer and use it in GitHub Desktop.
/*
链表排序
*/
#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