/* Alternatively merge a node from each list. * For card shuffle: * Implement a front back split followed by a shuffle merge. */ struct node* shuffle_merge(struct node* a, struct node* b) { struct node* returned; if(a == NULL && b == NULL) return NULL; else if(a == NULL && b!=NULL) { returned = shuffle_merge(NULL, b->next); b->next = returned; return b; } else if(a!=NULL && b==NULL) { returned = shuffle_merge(a->next, NULL); a->next = returned; return a; } else if(a!=NULL && b!=NULL) //I knw its a redundant statement!! { returned = shuffle_merge(a->next, b->next); b->next = returned; a->next = b; return a; } }
void shuffle_merge_test() { node_t* a = build_n_list(5); node_t* b = build_n_list(7); print_list(a); print_list(b); node_t* c = shuffle_merge(a, b); print_list(c); }
/* Shuffle large input stream by splitting into chunks */ int shuffle_by_chunks() { long i = 0, l = 0, k=1; int fidcounter = 0; char filename[MAX_STRING_LENGTH]; CREC *array; FILE *fin = stdin, *fid; array = malloc(sizeof(CREC) * array_size); //init:200m CREC, 10G:637.5m fprintf(stderr,"SHUFFLING COOCCURRENCES\n"); if(verbose > 0) fprintf(stderr,"array size: %lld\n", array_size); sprintf(filename,"%s_%04d.bin",file_head, fidcounter); //temp_shuffle_0000.bin fid = fopen(filename,"w"); if(fid == NULL) { fprintf(stderr, "Unable to open file %s.\n",filename); return 1; } if(verbose > 1) fprintf(stderr, "Shuffling by chunks: processed 0 lines.\n"); while(1) { //Continue until EOF if(i >= array_size) {// If array is full, shuffle it and save to temporary file shuffle(array, i-2); l += i; if(verbose > 1) fprintf(stderr, "i=%ld, processed %ld lines.", i, l); write_chunk(array,i,fid); fclose(fid); fidcounter++; sprintf(filename,"%s_%04d.bin",file_head, fidcounter); fid = fopen(filename,"w"); if(fid == NULL) { fprintf(stderr, "Unable to open file %s.\n",filename); return 1; } i = 0; } fread(&array[i], sizeof(CREC), 1, fin); //fin=stdin=cooc.bin if (i==k-1){ fprintf(stderr, "i=%ld, word1=%d, word2=%d, val=%f \n", i, array[i].word1, array[i].word2, array[i].val); k=k*2; } if(feof(fin)) break; i++; } shuffle(array, i-1); //Last chunk may be smaller than array_size write_chunk(array,i,fid); l += i; if(verbose > 1) fprintf(stderr, " processed %ld lines.\n", l); if(verbose > 1) fprintf(stderr, "Wrote %d temporary file(s).\n", fidcounter + 1); fclose(fid); free(array); return shuffle_merge(fidcounter + 1); // Merge and shuffle together temporary files }
int main() { struct node* plista = create_list(); struct node* plistb = create_list(); struct node* pmerged; print(plista); print(plistb); pmerged = shuffle_merge(plista, plistb); printf("after shuffle_merge:\n"); print(pmerged); printf("\n"); return 0; }
void main() { struct node* head = BuildOneTwoThree(); print_list(head); // int count = Count(head, 2); // int count = get_nth(head, 0); // printf("Count is: %d\n", count); // delete_list(&head); // print_list(head); // int i = 0; // int length = list_length(head); /* for(i = 0; i<length; i++) { int c = pop(&head); printf("%d\t", c); } printf("\n"); */ // insert_nth(&head,0,10); // print_list(head); #ifdef SORTED_INSERT struct node* new_node1 = (struct node*) malloc(sizeof(struct node)); new_node1->data = 13; sorted_insert(&head, new_node1); print_list(head); struct node* new_node2 = (struct node*) malloc(sizeof(struct node)); new_node2->data = 0; sorted_insert(&head, new_node2); print_list(head); struct node* new_node3 = (struct node*) malloc(sizeof(struct node)); new_node3->data = 8; sorted_insert(&head, new_node3); print_list(head); struct node* new_node4 = (struct node*) malloc(sizeof(struct node)); new_node4->data = 4; sorted_insert(&head, new_node4); print_list(head); struct node* new_node5 = (struct node*) malloc(sizeof(struct node)); new_node5->data = 30; sorted_insert(&head, new_node5); print_list(head); #endif #ifdef INSERT_SORT insert_nth(&head,0,10); insert_nth(&head,1,20); insert_nth(&head,3,14); insert_nth(&head,4,78); insert_nth(&head,1,32); insert_nth(&head,3,42); print_list(head); insert_sort(&head); print_list(head); #endif #ifdef APPEND struct node* head1 = BuildOneTwoThree(); insert_nth(&head1,0,10); print_list(head1); append(&head,&head1); print_list(head); print_list(head1); #endif #ifdef SPLIT_FRONT_BACK struct node* new_node5 = (struct node*) malloc(sizeof(struct node)); new_node5->data = 30; sorted_insert(&head, new_node5); struct node* new_node6 = (struct node*) malloc(sizeof(struct node)); new_node6->data = 10; sorted_insert(&head, new_node6); struct node* head1 = NULL; struct node* head2=NULL; front_back_split(head, &head1, &head2); print_list(head1); print_list(head2); #endif #ifdef REMOVE_DUPLICATES struct node* new_node5 = (struct node*) malloc(sizeof(struct node)); new_node5->data = 2; sorted_insert(&head, new_node5); struct node* new_node6 = (struct node*) malloc(sizeof(struct node)); new_node6->data = 1; sorted_insert(&head, new_node6); print_list(head1); remove_duplicates(head1); print_list(head1); #endif #ifdef MOV_NODE struct node* a = BuildOneTwoThree(); struct node* b = BuildOneTwoThree(); print_list(a); print_list(b); move_node(&a,&b); print_list(a); print_list(b); #endif #ifdef ALTERNATING_SPLIT struct node* a = NULL; struct node* b = NULL; alternating_split(head, &a, &b); print_list(a); print_list(b); #endif #ifdef SHUFFLE_MERGE struct node* a = BuildOneTwoThree(); struct node* b = BuildOneTwoThree(); print_list(a); print_list(b); struct node* c = shuffle_merge(a,b); print_list(c); #endif #ifdef SORTED_MERGE struct node* a = BuildOneTwoThree(); struct node* b = BuildOneTwoThree(); struct node* new_node1 = (struct node*) malloc(sizeof(struct node)); new_node1->data = 13; sorted_insert(&a, new_node1); struct node* new_node2 = (struct node*) malloc(sizeof(struct node)); new_node2->data = 0; sorted_insert(&b, new_node2); struct node* new_node3 = (struct node*) malloc(sizeof(struct node)); new_node3->data = 8; sorted_insert(&a, new_node3); struct node* new_node4 = (struct node*) malloc(sizeof(struct node)); new_node4->data = 4; sorted_insert(&a, new_node4); struct node* new_node5 = (struct node*) malloc(sizeof(struct node)); new_node5->data = 30; sorted_insert(&b, new_node5); print_list(a); print_list(b); struct node* c = sorted_merge(a,b); print_list(c); #endif #ifdef MERGE_SORT insert_nth(&head,0,10); insert_nth(&head,1,20); insert_nth(&head,3,14); insert_nth(&head,4,78); insert_nth(&head,1,32); insert_nth(&head,3,42); print_list(head); merge_sort(&head); // print_list(head); #endif insert_nth(&head,0,10); insert_nth(&head,1,20); insert_nth(&head,3,14); insert_nth(&head,4,78); insert_nth(&head,1,32); insert_nth(&head,3,42); print_list(head); reverse(&head); print_list(head); }