int main(){ int i = 0; /*srand(time(NULL));*/ /* Initialize the skip list */ struct skipList* sl1 = (struct skipList*)malloc(sizeof(struct skipList)); initSkipList(sl1); struct skipList* sl2 = (struct skipList*)malloc(sizeof(struct skipList)); initSkipList(sl2); /* Add to the skip list M = 20 random integers in [0,100] */ for(i = 0; i < 20; i++){ addSkipList(sl1, rand() %101); addSkipList(sl2, rand() %101); } /* Print out the contents of the skip list in the breadth-first order, starting from top. While printing the elements, move to a new line every time you reach the end of one level, and move down to the lower level of the skip list. For example, the print out of a skip list with 5 elements should look like 7 7 14 29 3 7 9 14 20 */ printf("---------- skipList 1 -----"); printf("----- size %d -----\n", sizeSkipList(sl1)); printSkipList(sl1); printf("---------- skipList 2 -----"); printf("----- size %d -----\n", sizeSkipList(sl2)); printSkipList(sl2); mergeSkipList(sl1, sl2); printf("---------- mergeSkipList -----"); printf("----- size %d -----\n", sizeSkipList(sl1)); printSkipList(sl1); /* Develop test cases for evaluating the following functions: int containsSkipList(struct skipList *slst, TYPE e) int removeSkipList(struct skipList *slst, TYPE e) */ printf("---------- removed %g from skipList -----", sl1->topSentinel->next->value); removeSkipList(sl1, sl1->topSentinel->next->value); printf("----- size %d -----\n", sizeSkipList(sl1)); printSkipList(sl1); return 0; }
/* Merge two skip lists, by adding elements of skip list 2 to skip list 1 that are not already contained in skip list 1. The function is also supposed to remove the entire skip list 2 from the memory. param: slst1 -- pointer to the skip list 1 param: slst2 -- pointer to the skip list 2 pre: slst1 and slst2 are not null, and skip list 1 and skip list 2 are not empty post: slst1 points to the merger skip list, slst2 is null*/ void mergeSkipList(struct skipList *slst1, struct skipList *slst2) { struct skipLink *current = slst2->topSentinel; while(current->down != NULL){ current = current->down; } while(current->next != NULL){ if(!containsSkipList(slst1, current->next->value)){ addSkipList(slst1, current->next->value); current = current->next; } else{ current = current->next; } } }
/* Merge two skip lists, by adding elements of skip list 2 to skip list 1 that are not already contained in skip list 1. The function is also supposed to remove the entire skip list 2 from the memory. param: slst1 -- pointer to the skip list 1 param: slst2 -- pointer to the skip list 2 pre: slst1 and slst2 are not null, and skip list 1 and skip list 2 are not empty post: slst1 points to the merger skip list, slst2 is null*/ void mergeSkipList(struct skipList *slst1, struct skipList *slst2) { /* FIX ME*/ /* Check if slst1 and slst2 arm null */ assert(slst1 && slst2); /* Check if slst1 and slst2 are empty */ assert(sizeSkipList(slst1) > 0 && sizeSkipList(slst2) > 0); /* Get the head to the bottom list of slst2, the list that has all links */ struct skipLink* head = slst2->topSentinel; struct skipLink* currLink = head; while(head->down) head = head->down; currLink = head->next; /* Loop through all links on bottom list */ while(currLink) { /* Create variable to hold currLink value */ TYPE currVal = currLink->value; /* If slst1 doesn't have currVal, add it */ if(!containsSkipList(slst1, currVal)) addSkipList(slst1, currVal); /* Remove all instances of currVal in slst2 */ while(currLink && currVal == currLink->value) { currLink = currLink->next; removeSkipList(slst2, currVal); } } /* Free slst2 sentinel */ free(slst2->topSentinel); slst2->topSentinel = 0; /* Free slst2 */ free(slst2); slst2 = 0; }
void main(){ int i; int add; /* FIX ME */ struct skipList *list; /* Initialize the skip list */ initSkipList(list); /* Add to the skip list M = 20 random integers in [0,100] */ for(i = 0; i < 20; i++){ add = rand()%101; addSkipList(list, add); } /* Print out the contents of the skip list in the breadth-first order, starting from top. While printing the elements, move to a new line every time you reach the end of one level, and move down to the lower level of the skip list. For example, the print out of a skip list with 5 elements should look like 7 7 14 29 3 7 9 14 20 */ printSkipList(list); removeSkipList(list,94); /* Develop test cases for evaluating the following functions: int containsSkipList(struct skipList *slst, TYPE e) int removeSkipList(struct skipList *slst, TYPE e) */ }
int main (int argc, const char * argv[]) { const long size = atoi(argv[1]); TYPE *task = (TYPE*)malloc(sizeof(TYPE)*size*2); int *flip = (int*)malloc(sizeof(int)*size*2); DynArr mainList; long i; struct skipList skippy; printf("Enter\n"); initDynArr(&mainList, 10000000); /*Avoid effects of resizing*/ initSkipList(&skippy); /* create tasks */ for(i=0;i<size*2; i++) { task[i] = createTask(rand(), "r"); flip[i] = rand()%2; } /* add tasks to the dynamic array */ for(i=0;i<size; i++) { addHeap(&mainList, task[i]); addSkipList(&skippy, task[i]); } long start = clock(); long finish = clock(); long add = 0; long remove = 0; long cnt_add = 0; long cnt_remove = 0; long sadd = 0; long sremove = 0; long scnt_add = 0; long scnt_remove = 0; for(i=0;i<size*2; i++) { if(flip[i]) { start = clock(); addHeap(&mainList, task[i]); finish = clock(); add += (finish-start); cnt_add++; start = clock(); addSkipList(&skippy, task[i]); finish = clock(); sadd += (finish-start); scnt_add++; } else { start = clock(); removeMinHeap(&mainList); finish = clock(); remove += (finish-start); cnt_remove++; start = clock(); removeMinSkipList(&skippy); finish = clock(); sremove += (finish-start); scnt_remove++; } } printf("Below are the average runtimes for a given number of operations.\n"); printf("----HEAP----\n"); printf("Adding %ld tasks took %lf clocks.\n", cnt_add, (double)(add) / (double)(cnt_add)); printf("Removing %ld minimums took %lf clocks.\n", cnt_remove, (double)(remove) / (double)cnt_remove); printf("----SKIP LIST----\n"); printf("Adding %ld tasks took %lf clocks.\n", scnt_add, (double)(sadd) / (double)(scnt_add)); printf("Removing %ld minimums took %lf clocks.\n", scnt_remove, (double)(sremove) / (double)scnt_remove); return 0; }
void test(){ /* Seed psuedo-random number generator */ srand(time(NULL)); int i; /* FIX ME */ /* Initialize the skip list */ struct skipList* list = (struct skipList*)malloc(sizeof(struct skipList)); assert(list); initSkipList(list); struct skipList* listB = (struct skipList*)malloc(sizeof(struct skipList)); assert(listB); initSkipList(listB); addSkipList(listB, 10.0); addSkipList(listB, 11.0); addSkipList(listB, 12.0); addSkipList(listB, 13.0); /* Add to the skip list M = 20 random integers in [0,100] */ i = 0; TYPE val; while(i++ < 1) { val = (double)(rand() % 101); printf("Val: %f\n", val); addSkipList(list, val); } addSkipList(list, 1.0); addSkipList(list, 3.0); addSkipList(list, 7.0); addSkipList(list, 6.0); addSkipList(list, 3.0); /* Print out the contents of the skip list in the breadth-first order, starting from top. While printing the elements, move to a new line every time you reach the end of one level, and move down to the lower level of the skip list. For example, the print out of a skip list with 5 elements should look like 7 7 14 29 3 7 9 14 20 */ printSkipList(list); printf("List size: %d\n", sizeSkipList(list)); /* Develop test cases for evaluating the following functions: int containsSkipList(struct skipList *slst, TYPE e) int removeSkipList(struct skipList *slst, TYPE e) */ /* Contains */ printf("Contains %f? %d\n", val, containsSkipList(list, val)); printf("Contains 50? %d\n", containsSkipList(list, 50.0)); /* Remove */ printf("Remove 3.0\n"); removeSkipList(list, 3.0); printSkipList(list); printf("List size: %d\n", sizeSkipList(list)); /* merge list */ printf("Merge\n"); mergeSkipList(list, listB); printSkipList(list); /* Size */ printf("Size: %d\n", sizeSkipList(list)); /* listB */ /*printf("ListB null ? %d\n", listB->topSentinel->value);*/ }