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) { /* 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) */ }
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);*/ }