具体实现过程可以点击下面链接
链接:https://leetcode.cn/problems/design-skiplist/solutions/1696545/she-ji-tiao-biao-by-leetcode-solution-e8yh/
#include<iostream>
#include<iostream>
#include<time.h>
#include<stdio.h>
using namespace std;
#include <stdlib.h>
#define MAX(a, b) ((a) > (b) ? (a) : (b))
const int MAX_LEVEL = 32;
const int P_FACTOR = RAND_MAX >> 2;//0.25的机会加一层
typedef struct SkiplistNode {
int val;//保存值
int maxLevel;//当前结点最大层数
struct SkiplistNode **forward;//指向下一个结点的指针数组
} SkiplistNode;
typedef struct {
SkiplistNode *head;//跳表的头节点
int level;//跳表的最高层数
} Skiplist;
SkiplistNode *skiplistNodeCreat(int val, int maxLevel) {
SkiplistNode *obj = (SkiplistNode *)malloc(sizeof(SkiplistNode));//开辟空间
obj->val = val;//数据赋值
obj->maxLevel = maxLevel;//结点最大层数赋值
obj->forward = (SkiplistNode **)malloc(sizeof(SkiplistNode *) * maxLevel);//为指针数组开辟空间
for (int i = 0; i < maxLevel; i++) {
obj->forward[i] = NULL;//下一节点为空指针
}
return obj;
}
void skiplistNodeFree(SkiplistNode* obj) {//释放空间
if (obj->forward) {
free(obj->forward);
obj->forward = NULL;
obj->maxLevel = 0;
}
free(obj);
}
Skiplist* skiplistCreate() {//跳表初始化
Skiplist *obj = (Skiplist *)malloc(sizeof(Skiplist));
obj->head = skiplistNodeCreat(-1, MAX_LEVEL);
obj->level = 0;
srand(time(NULL));//设置随机数种子
return obj;
}
static inline int randomLevel() {
int lv = 1;
/* 随机生成 lv */
while (rand() < P_FACTOR && lv < MAX_LEVEL) {//如果随机生成小于
lv++;
}
return lv;
}
bool skiplistSearch(Skiplist* obj, int target) {
SkiplistNode *curr = obj->head;
for (int i = obj->level - 1; i >= 0; i--) {
/* 找到第 i 层小于且最接近 target 的元素*/
while (curr->forward[i] && curr->forward[i]->val < target) {
curr = curr->forward[i];
}
}
curr = curr->forward[0];
/* 检测当前元素的值是否等于 target */
if (curr && curr->val == target) {
return true;
}
return false;
}
void skiplistAdd(Skiplist* obj, int num) {
SkiplistNode *update[MAX_LEVEL];
SkiplistNode *curr = obj->head;
for (int i = obj->level - 1; i >= 0; i--) {
/* 找到第 i 层小于且最接近 num 的元素*/
while (curr->forward[i] && curr->forward[i]->val < num) {
curr = curr->forward[i];
}
update[i] = curr;
}
int lv = randomLevel();
if (lv > obj->level) {
for (int i = obj->level; i < lv; i++) {
update[i] = obj->head;
}
obj->level = lv;
}
SkiplistNode *newNode = skiplistNodeCreat(num, lv);
for (int i = 0; i < lv; i++) {
/* 对第 i 层的状态进行更新,将当前元素的 forward 指向新的节点 */
newNode->forward[i] = update[i]->forward[i];
update[i]->forward[i] = newNode;
}
}
bool skiplistErase(Skiplist* obj, int num) {
SkiplistNode *update[MAX_LEVEL];
SkiplistNode *curr = obj->head;
for (int i = obj->level - 1; i >= 0; i--) {
/* 找到第 i 层小于且最接近 num 的元素*/
while (curr->forward[i] && curr->forward[i]->val < num) {
curr = curr->forward[i];
}
update[i] = curr;
}
curr = curr->forward[0];
/* 如果值不存在则返回 false */
if (!curr || curr->val != num) {
return false;
}
for (int i = 0; i < obj->level; i++) {
if (update[i]->forward[i] != curr) {
break;
}
/* 对第 i 层的状态进行更新,将 forward 指向被删除节点的下一跳 */
update[i]->forward[i] = curr->forward[i];
}
skiplistNodeFree(curr);
/* 更新当前的 level */
while (obj->level > 1 && obj->head->forward[obj->level - 1] == NULL) {
obj->level--;
}
return true;
}
void skiplistFree(Skiplist* obj) {
for (SkiplistNode * curr = obj->head; curr; ) {
SkiplistNode *prev = curr;
curr = curr->forward[0];
skiplistNodeFree(prev);
}
free(obj);
}
int main(){
Skiplist* L=skiplistCreate();
skiplistAdd(L,1);
cout<<skiplistSearch(L,2)<<endl;
skiplistAdd(L,2);
cout<<skiplistSearch(L,2)<<endl;
cout<<skiplistErase(L,2)<<endl;
cout<<skiplistSearch(L,2)<<endl;
skiplistFree(L);
}