示例#1
0
文件: skipList.c 项目: Merfoo/CS-261
/* 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++;
}
示例#2
0
文件: skipList.c 项目: Mankee/CS261
/* 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++;
}
示例#3
0
文件: skipList.c 项目: Mankee/CS261
/* 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;
}