源代码如下:
#include <stdlib.h>
#include <stdio.h>
#define lgNmax 5
typedef char Key;
struct Item{Key key;};
typedef struct STnode* link;
struct STnode{
Item item ;
link *next; //多个链接域组成的数组
int sz ; // 该节点的链接数
};
static link head ,z;
static int N ; //跳跃表的总结点数
static int lgN ; //跳跃表的最高层数
struct Item NULLitem;
Key key(Item item){
return item.key;
}
//新建一个节点
link NEW(Item item , int k){ //k:该节点的链接数
int i;
link x = (link)malloc(sizeof*x);
x->next = (link *)malloc(k*sizeof(link));
x->item = item;
x->sz = k;
for(i=0;i<k;i++)x->next[i]=z;
return x;
}
//初始化
void STinit(){
N = 0; lgN = 0;
z = NEW(NULLitem,RAND_MAX);
head = NEW(NULLitem,lgNmax + 1);
}
//以概率1/2^j产生一个新的J个链接的节点
int randX(){
int i , j, t = rand();
for(i=1,j=2;i<lgNmax;i++,j+=j)
if(t > RAND_MAX / j)break;
if(i > lgN) lgN = i;
return i;
}
//跳跃表的插入操作
void ins