KYSDK_SKIPLIST_INSERT(3)

NAME
kysdk_skiplist_insert - 插入节点

SYNOPSIS
br

#include <skip_linklist.h>

  Functions

  int kysdk_skiplist_insert (kysdk_skiplist * list, int key, kysdk_listdata data)

插入节点

Parameters
    list 需要操作的链表
    key 键
    data 数据

Returns
    int， 成功插入返回0，失败返回-1

Link with -lkydatastruct.

Detailed Description

接口的主要功能是插入节点

EXAMPLES
    #include <skip_linklist.h>
    #include <stdlib.h>
    #include <time.h>
    #include <stdio.h>
    #include <unistd.h>
    void print_list(kysdk_skiplist *list)
    {
        for (int i = list->max_levels - 1; i >= 0; i --)
        {
            kysdk_skiplist_node *curnode = list->children[i];
            printf("Level[%d]: ", i);
            while (curnode)
            {
                printf("%d->", curnode->key);
                curnode = curnode->next[i];
            }
            printf("NULL);
        }
    }
    int test_list_search(kysdk_skiplist *list)
    {
        for (int i = 0; i < 5; i ++)
        {
            int target = random() % 50 + 1;
            kysdk_listdata data = kysdk_skiplist_search(list, target);
            printf("Searching for %d, data is %d, target, data.num);
            kysdk_skiplist_node *curnode = list->children[0];
            short finded = 0;
            while (curnode)
            {
                if (curnode->key == target)
                {
                    finded = 1;
                    break;
                }
                curnode = curnode->next[0];
            }

            if (finded && data.num == -1)
            {
                printf("Skiplist search test failed.);
                return -1;
            }
        }
        printf("Skiplist search test pass.);
        return 0;
    }
    int main()
    {
        kysdk_skiplist *list = kysdk_create_skiplist();
        kysdk_skiplist_setmaxlevels(list, 5);

        srand(time(NULL));
        for (int i = 0; i < 20; i ++)
        {
            int num = random() % 50 + 1;
            kysdk_listdata data;
            data.num = i;
            kysdk_skiplist_insert(list, num, data);
            printf("[%d] %d has been insert., i, num);
            sleep(1);
        }

        print_list(list);
        int res = test_list_search(list);
        kysdk_destroy_skiplist(list);
        return res;
    }

CONFORMING TO
These functions are as per the withdrawn POSIX.1e draft specification. The following functions are Linux extensions: kysdk_create_skiplist(), kysdk_destroy_skiplist(), kysdk_skiplist_setmaxlevels(), kysdk_skiplist_delete() and kysdk_skiplist_search().

SEE ALSO
kysdk_create_skiplist(3), kysdk_destroy_skiplist(3), kysdk_skiplist_setmaxlevels(3), kysdk_skiplist_delete(3) and kysdk_skiplist_search(3).

Author

Generated automatically by Doxygen for skip_linklist.h from the source code.