the msort_rs(t) doesn't sort linked list t, do i need a pointer to pointer here likeCode:typedef struct node* link; struct node { int item; link next; }; #define key(A) (A) #define less(A, B) (key(A) <= key(B)) /* stable with code below */ link merge(link a, link b) { struct node head; link c = &head; while ((a != NULL) && (b != NULL)) if (less(a->item, b->item)) { c->next = a; c = a; a = a->next; } else { c->next = b; c = b; b = b->next; } c->next = (a == NULL) ? b : a; return head.next; } link msort_rs(link c) { link a, b; if (c->next == NULL) return c; a = c; b = c->next; while ((b != NULL) && (b->next != NULL)) { c = c->next; b = b->next->next; } b = c->next; c->next = NULL; return merge(msort_rs(a), msort_rs(b)); } main() { int i; struct node heada; link t = &heada; for(i = 0; i < 5; i++) { t->next = malloc(sizeof *t); t = t->next; t->next = NULL; t->item = rand() %100; } msort_rs(t); for(i = 0; i < 5; i++) { printf("%d", t->item); t = t->next; } }
and how come when i run it the first element in the list is 65536??
Code:link b; .. b = msort_rs(t); for( i =0, i < 10; i++) { printf("%d", b->item); b = b->next; }



LinkBack URL
About LinkBacks


