void testsortedmerge() { struct node* a = buildlistinsortedorder(5); struct node* b = buildlistinsortedorder(5); printlist(a, "a"); printlist(b, "b"); struct node* node = sortedmerge(a, b); printlist(node, "node"); }
struct node* sortedmerge(struct node* a, struct node* b) { struct node* result = NULL; /* Base cases */ if (a == NULL) return(b); else if (b==NULL) return(a); /* Pick either a or b, and recur */ if (a->data <= b->data) { result = a; result->next = sortedmerge(a->next, b); } else { result = b; result->next = sortedmerge(a, b->next); } return(result); }
int main() { struct node* res = NULL; struct node* a = NULL; struct node* b = NULL; push(&a, 15); push(&a, 10); push(&a, 5); push(&b, 20); push(&b, 3); push(&b, 2); res = sortedmerge(a, b); printf("\n Merged Linked List is: \n"); printList(res); getchar(); return 0; }