64 lines
1.3 KiB
C
64 lines
1.3 KiB
C
#ifndef __SKIPLIST
|
|
#define __SKIPLIST
|
|
|
|
#include <inttypes.h>
|
|
#include <limits.h>
|
|
#include <stdio.h>
|
|
|
|
#define KEY_INF_MIN INT_MIN /* -INFINITY */
|
|
#define KEY_INF_MAX INT_MAX /* +INFINITY */
|
|
|
|
#define SL_ADD_NEW 1 /* added new key */
|
|
#define SL_ADD_SAME 0 /* added existing key */
|
|
#define SL_ADD_INV -1 /* added invalid key (+-INFINITY) */
|
|
|
|
/* skip list node */
|
|
typedef struct skiplist_node {
|
|
int key;
|
|
void *data;
|
|
struct skiplist_node *next;
|
|
struct skiplist_node *prev;
|
|
struct skiplist_node *below;
|
|
struct skiplist_node *above;
|
|
} sl_node;
|
|
|
|
/* skiplist object */
|
|
typedef struct skiplist {
|
|
sl_node *topleft;
|
|
sl_node *bottomleft;
|
|
sl_node *topright;
|
|
sl_node *bottomright;
|
|
int maxlevel;
|
|
} slist;
|
|
|
|
/*
|
|
* Initialize empty skip list.
|
|
*/
|
|
void sl_init(slist *sl);
|
|
/*
|
|
* Free skip list memory. After free list is not initialized
|
|
* for subsequent use.
|
|
*/
|
|
void sl_free(slist *sl);
|
|
/*
|
|
* Print skip list level by level and in down to up form.
|
|
*/
|
|
void sl_print(const slist *sl);
|
|
/*
|
|
* Add new key with data into skip list.
|
|
*/
|
|
int sl_add(slist *sl, int key, void *data);
|
|
/*
|
|
* Find node in skip list with given key.
|
|
* If key is not found returns NULL.
|
|
*/
|
|
sl_node *sl_find(slist *sl, int key);
|
|
/*
|
|
* Remove node from skip list with given key.
|
|
* If key is found and removed return 1, otherwise 0.
|
|
*/
|
|
int sl_remove(slist *sl, int key);
|
|
|
|
#endif
|
|
|