i can;t see any problem in main(), creatList, printList, and listlenght, where' s the problem, when i run it, it just hang...Code:#include <stdlib.h> #include <stdio.h> #include <time.h> #include <assert.h> 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 msort2(int n, link a, link b) { int n1; link xs1, xs2, c = NULL; if(n == 0) { b = a; return NULL; } if(n == 1) { b = a->next; a->next = NULL; return a; } n1 = n/2; xs1 = msort2(n1,a,b); xs2 = msort2((n-n1),b,c); b = c; return merge(xs1,xs2); } int listlength(link c) { int counter = 0; link list = c; while(list) { counter++; list = list->next; } return counter; } link merge_no_scan(link c) { link b = NULL; int n = listlength(c); /* find the length of the list */ return msort2(n,c,b); } main() { link list; list = createList(5); PrintList(list); list = merge_no_scan(list); PrintList(list); } link createList(int size) { int i; link list; list = 0; for(i = 0;i < size; ++i) { link nw; nw = (link)malloc(sizeof(struct node)); assert(nw!=0); nw->item = rand()%1000; nw->next = list; list = nw; } return list; } void PrintList(link list) { link p; printf("List:"); for(p = list;p!=0; p=p->next) printf("%d ", p->item); printf("\n"); }



LinkBack URL
About LinkBacks


