/* Add a new element to the skip list: param: slst -- pointer to the skip list param: e -- element to be added pre: slst is not null post: the new element is added to the lowest list and randomly to higher-level lists */ void addSkipList(struct skipList *slst, TYPE e) { /* Check if slst is null */ assert(slst); struct skipLink *downLink, *newLink; downLink = skipLinkAdd(slideRightSkipList(slst->topSentinel,e),e); /* FIX ME*/ while(downLink) { /* Create new express lane with new head and new link */ newLink = newSkipLink(e, 0, downLink); struct skipLink* newHead = newSkipLink(0, newLink, slst->topSentinel); /* Update the new skipList top sentinel to point to new list */ slst->topSentinel = newHead; /* If need to make new list, update downLink, else set to null */ if(flipSkipLink()) downLink = newLink; else downLink = 0; } /* Increase size */ slst->size++; }
/* Remove an element from the skip list: param: slst -- pointer to the skip list param: e -- element to be removed pre: slst is not null post: the new element is removed from all internal sorted lists */ void removeSkipList(struct skipList *slst, TYPE e) { /* FIX ME*/ /* Check if slst is null */ assert(slst); /* Only remove elements if skip list is not empty */ if(sizeSkipList(slst) > 0) { int foundLink = 0; struct skipLink* curr = slst->topSentinel; /* Slide right to find link before link with value e */ while(1) { /* Slide right to find link before link with value e*/ curr = slideRightSkipList(curr, e); /* If next link value is e, free it and fix link connections */ if(curr->next && EQ(curr->next->value, e)) { struct skipLink* tmp = curr->next; curr->next = curr->next->next; free(tmp); foundLink = 1; } /* Exit loop is on bottom list, down link is null */ if(curr->down == 0) break; /* Set curr to down link */ curr = curr->down; } if(foundLink) { slst->size--; /* Remove all empty lists */ struct skipLink* head = slst->topSentinel; while(head->down) { if(head->next == 0) { struct skipLink* downHead = head->down; free(head); slst->topSentinel = downHead; head = downHead; } else break; } } } }
/* Checks if an element is in the skip list: param: slst -- pointer to the skip list param: e -- element to be checked pre: slst is not null post: returns true or false */ int containsSkipList(struct skipList *slst, TYPE e) { /* FIX ME*/ /* Check if slst is null */ assert(slst); /* Loop through list if its not empty */ if(sizeSkipList(slst) > 0) { /* Get the link before value e and compare */ struct skipLink* curr = slst->topSentinel; while(1) { curr = slideRightSkipList(curr, e); /* Return true (1) if value e is found */ if(curr->next && EQ(curr->next->value, e)) return 1; else if(curr->down == 0) break; curr = curr->down; } } /* Return false (0) if value e is not found */ return 0; }
/* Checks if an element is in the skip list: param: slst -- pointer to the skip list param: e -- element to be checked pre: slst is not null post: returns true or false */ int containsSkipList(struct skipList *slst, TYPE e) { struct skipLink *current = slst->topSentinel; while (current){ current = slideRightSkipList(current, e); if((current->next !=0) && EQ(current->next->value, e)) return 1; current = current->down; } return 0; }
/* Add a new element to the skip list: param: slst -- pointer to the skip list param: e -- element to be added pre: slst is not null post: the new element is added to the lowest list and randomly to higher-level lists */ void addSkipList(struct skipList *slst, TYPE e) { struct skipLink *downLink, *newLink; downLink = skipLinkAdd(slideRightSkipList(slst->topSentinel, e), e); if (downLink != 0 && flipSkipLink()) { struct skipLink* newTopSentinel = (struct skipLink *) malloc(sizeof(struct skipLink)); newLink = newSkipLink(e, 0, downLink); newTopSentinel->next = newLink; newTopSentinel->down = slst->topSentinel; slst->topSentinel = newTopSentinel; } slst->size++; }
/* Remove an element from the skip list: param: slst -- pointer to the skip list param: e -- element to be removed pre: slst is not null post: the new element is removed from all internal sorted lists */ void removeSkipList(struct skipList *slst, TYPE e) { /* FIX ME*/ struct skipLink *current = slst->topSentinel; while(current != NULL){ current = slideRightSkipList(current, e); if(current->next->value == e) current->next = current->next->next; /*POSSIBLE ERROR!!!!!!!!!!!!!!!*/ if(current->down == NULL) slst->size--; } }
/* Remove an element from the skip list: param: slst -- pointer to the skip list param: e -- element to be removed pre: slst is not null post: the new element is removed from all internal sorted lists */ void removeSkipList(struct skipList *slst, TYPE e) { struct skipLink *current, *temp; current = slst->topSentinel; while (current){ current = slideRightSkipList(current, e); if((current->next != 0) && EQ(current->next->value, e)){ temp = current->next; current->next = current->next->next; free(temp); if(current->down == NULL) slst->size--; } current = current->down; } }
/* Add a new skip link recursively param: current -- pointer to a place in the list where the new element should be inserted param: e -- the value to create a link for pre: current is not NULL post: a link to store the value */ struct skipLink* skipLinkAdd(struct skipLink * current, TYPE e) { struct skipLink *newLink, *downLink; newLink = 0; if (current->down == 0) { newLink = newSkipLink(e, current->next, 0); current->next = newLink; } else { downLink = skipLinkAdd(slideRightSkipList(current->down, e), e); if (downLink != 0 && flipSkipLink()) { newLink = newSkipLink(e, current->next, downLink); current->next = newLink; } } return newLink; }
/* Checks if an element is in the skip list: param: slst -- pointer to the skip list param: e -- element to be checked pre: slst is not null post: returns true or false */ int containsSkipList(struct skipList *slst, TYPE e) { /* FIX ME*/ assert(slst != 0); struct skipLink *current = slst->topSentinel; while(current!=NULL){ current = slideRightSkipList(current, e); if(current->next->value == e) return 1; /*The element is in the list*/ else current = current->down; } return 0; /*The element is not in the list*/ }
/* Add a new element to the skip list: param: slst -- pointer to the skip list param: e -- element to be added pre: slst is not null post: the new element is added to the lowest list and randomly to higher-level lists */ void addSkipList(struct skipList *slst, TYPE e) { int r = rand()%5; struct skipLink *downLink, *topLink, *temp; topLink = (struct skipLink *)malloc(sizeof(struct skipLink)); downLink = skipLinkAdd(slideRightSkipList(slst->topSentinel,e),e); if(r==1){ temp = slst->topSentinel; /* FIX ME*/ slst->size++; assert(topLink!=0); slst->topSentinel = topLink; topLink->next = downLink; topLink->down = temp; } }