2004»ò2005ÄꌑµÄ´ú´a¡£
skiplist£º
skiplist.h
/*
* Ëæ»úÐÔ²éÕÒÊý¾Ý½á¹¹µÄÒ»ÖÖÖØÒª·½°¸¡£
* ÔÚ 1990 ÄêÓÉ W. Pugh ·¢Ã÷¡£
*/
typedef int typekey;
/*
* ÌøÔ¾±í½Úµã£¬°üº¬µ½ºó¼Ì½ÚµãµÄÖ¸ÕëÊý×飬ËüµÄ´óС¶ÔÓ¦ÓÚ½ÚµãµÄ¼¶±ð¡£
* ½ÚµãµÄ¼¶±ðÔÚ½¨Á¢Ê±Ëæ»ú·ÖÅ䣬²¢ÇÒ²»ÓÃÔÚ½ÚµãÖб£´æ¡£
*/
typedef struct node{
typekey key;
char *value;
struct node *forward[1];
} node;
/*
* ÌøÔ¾±íÊý¾Ý½á¹¹£¬°üº¬µ±Ç°µÄÕûÌå¼¶±ð¡¢½ÚµãÊýÄ¿ºÍÍ·²¿½Úµã¡£
* Í·²¿½ÚµãµÄÖ¸ÕëÊý×é´óСÊÇ MaxLevel¡£
*/
typedef struct {
int level;
int count;
node * header;
} listnode,* skiplist;
extern skiplist init(void); /* ÌøÔ¾±í½¨Á¢Àý³Ì */
extern void destroy(skiplist); /* ÌøÔ¾±íÇå³ýÀý³Ì */
extern node* search(typekey, skiplist);
extern node* insert(typekey, skiplist);
extern void delete(typekey, skiplist);
|
skiplist.c
#include <stdio.h>
#include <stdlib.h>
#include "./skiplist.h"
skiplist init(void);
void destroy(skiplist);
node* search(typekey, skiplist);
node* insert(typekey, skiplist);
void delete(typekey,skiplist);
static node* makeNode(int,int);
static void freeNode(node *);
static void Error(void);
#define MaxLevel 16 /* ×î´ó¼¶±ðÊý */
#define Probability RAND_MAX/2 /* ÌáÉý¼¶±ðµÄ¸ÅÂÊ£¬ÕâÀïÊÇÓÐ 1/2 µÄ¿ÉÄÜÐÔÌá¼¶ */
node* search(typekey key, skiplist list)
{
node * x;
int i;
x = list->header;
for (i = list->level-1; i>=0; i--) /* ´Ó¸ßµ½µÍ²éÕÒ¸÷¸ö¼¶±ðµÄÁ´±í */
/* ÔÚÕâ¸öÁ´±íÉϰѵ±Ç°Ö¸Õ붨λµ½Ð¡ÓÚÕâ¸ö²éÕÒ¼üÖµµÄ×îºóµÄ½Úµã */
while (x->forward[i] != NULL && x->forward[i]->key < key)
x = x->forward[i];
x = x->forward[0];
if (x != NULL && x->key == key)
return x;
else
return NULL;
}
node* insert(typekey key, skiplist list)
{
/* Òª²åÈëµÄ½ÚµãÔÚÿ¸ö¼¶±ðµÄÁ´±íÉÏÇ°Ãæ×î½üµÄ½Úµã */
node * update[MaxLevel];
node *x;
int i;
int lvl = 1; /* н¨½ÚµãµÄ¼¶±ð */
x = list->header;
/* ÕÒµ½Òª²åÈë½ÚµãÔÚ¸÷¸ö¼¶±ðµÄÁ´±íÉϵÄλÖà */
for (i = list->level-1; i>=0; i--) {
while (x->forward[i] != NULL && x->forward[i]->key < key)
x = x->forward[i];
update[i] = x;
}
x = x->forward[0];
if (x != NULL && x->key == key) {
Error();
return NULL;
} else {
while (rand() < Probability && lvl < MaxLevel) /* Ëæ»úÉú³ÉнڵãµÄ¼¶±ð */
lvl++;
if (lvl > list->level) { /* нڵãµÄ¼¶±ð´óÓÚµ±Ç°µÄÕûÌå¼¶±ð */
for (i = lvl-1; i >= list->level ; i--)
update[i] = list->header; /* ²¹³ä³¬³öµÄ¼¶±ðÉϵÄÇ°Ãæ×î½üµÄ½Úµã */
list->level = lvl; /* ÌáÉýÕûÌå¼¶±ð */
}
x = makeNode(lvl,key);
/* ÔÚ¸÷¸ö¼¶±ðµÄÁ´±íÉϲåÈëнڵã */
for (i = lvl-1; i >= 0; i--) {
x->forward[i] = update[i]->forward[i];
update[i]->forward[i] = x;
}
list->count++;
return x;
}
}
void delete(typekey key,skiplist list)
{
/* Ҫɾ³ýµÄ½ÚµãÔÚÿ¸ö¼¶±ðµÄÁ´±íÉÏÇ°Ãæ×î½üµÄ½Úµã */
node *update[MaxLevel];
node * x;
int i;
x = list->header;
/* ÕÒµ½ÒªÉ¾³ýµÄ½ÚµãÔÚ¸÷¸ö¼¶±ðµÄÁ´±íÉϵÄλÖà */
for (i = list->level-1; i >= 0; i--) {
while (x->forward[i] != NULL && x->forward[i]->key < key)
x = x->forward[i];
update[i] = x;
}
x = x->forward[0];
/* ÔÚ¸÷¸ö¼¶±ðµÄÁ´±íɾ³ýÕâ¸ö½Úµã */
if (x != NULL && x->key == key) {
for (i = 0; i < list->level; i++) {
if (update[i]->forward[i] != x) /* Õâ¸ö½Úµãδ´ïµ½Õâ¸ö¼¶±ð */
break;
update[i]->forward[i] = x->forward[i];
}
freeNode(x);
list->count--;
/* µ±ÒªÉ¾³ýµÄÕâ¸ö½ÚµãÊÇij¸ö¼¶±ðÉÏΨһµÄ½Úµãʱ½«µ¼ÖÂÕûÌåµÄ¼¶±ðϽµ */
while (list->level >1 && list->header->forward[list->level-1] == NULL)
list->level--;
}
else
Error();
}
skiplist init(void)
{
skiplist l;
node * t;
int i;
l = (skiplist)malloc(sizeof(listnode));
t = (node*)malloc(sizeof(node)+(MaxLevel-1)*sizeof(node *));
t->value = NULL;
for(i = MaxLevel-1; i >= 0; i--)
t->forward[i] = NULL;
l->header = t;
l->level = 1;
l->count = 0;
return l;
}
void destroy(skiplist list)
{
node *x, *p;
x = list->header;
while(x != NULL) {
p = x;
x = x->forward[0];
freeNode(p);
}
free(list);
}
node* makeNode(int level,typekey key)
{
node * t;
t = (node*)malloc(sizeof(node)+(level-1)*sizeof(node *));
t->value = NULL;
t->key = key;
return t;
}
void freeNode(node * t)
{
if (t->value != NULL)
free(t->value);
free(t);
}
void Error(void)
{
printf("Skip List error\n");
}
|