int test_list_one_less() { struct node *a = BuildOneTwoThree(); int b_spec[5] = {7,13,1,99,42}; struct node *b = ListFromArray(b_spec, 5); printf("a: "); PrintList(a); printf("b: "); PrintList(b); struct node *shuffled_list = ShuffleMerge(a, b); printf("shuffled_list: "); PrintList(shuffled_list); }
int test_problem() { struct node *a = BuildOneTwoThree(); int b_spec[3] = {7,13,1}; struct node *b = ListFromArray(b_spec, 3); printf("a: "); PrintList(a); printf("b: "); PrintList(b); struct node *shuffled_list = ShuffleMerge(a, b); printf("shuffled_list: "); PrintList(shuffled_list); }
main() { struct node *a = BuildOneTwoThree(); struct node *b = BuildOneTwoThree1(); struct node *c = ShuffleMerge(a,b); struct node *d = c; while (d != 0) { printf("%d ",d->data); d = d->next; } return 0; }
// SuffleMerge() — Recursive // The recursive solution is the most compact of all, but is probably not appropriate for // production code since it uses stack space proportionate to the lengths of the lists. struct node* ShuffleMerge4(struct node* a, struct node* b) { struct node* result; struct node* recur; if (a==NULL) return(b); // see if either list is empty else if (b==NULL) return(a); else { // it turns out to be convenient to do the recursive call first -- // otherwise a->next and b->next need temporary storage. recur = ShuffleMerge(a->next, b->next); result = a; // one node from a a->next = b; // one from b b->next = recur; // then the rest return(result); } }
int main(int argc, char *arg[]) { struct node* listA = NULL; Push(&listA, 3); Push(&listA, 2); Push(&listA, 1); struct node* listB = NULL; Push(&listB, 9); Push(&listB, 10); Push(&listB, 14); Push(&listB, 1); Push(&listB, 13); Push(&listB, 7); PrintList(listA); PrintList(listB); struct node* newList = NULL; newList = ShuffleMerge(listA, listB); PrintList(newList); }