-
Notifications
You must be signed in to change notification settings - Fork 0
/
SkipList.h
66 lines (59 loc) · 1.11 KB
/
SkipList.h
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
#pragma once
#include <vector>
#include <climits>
#include "Container.h"
#define MAX_LEVEL 8
enum SKNodeType
{
HEAD = 1,
NORMAL,
NIL
};
struct SKNode
{
int key;
int val;
SKNodeType type;
std::vector<SKNode *> forwards;
SKNode(int _key, int _val, SKNodeType _type)
: key(_key), val(_val), type(_type)
{
for (int i = 0; i < MAX_LEVEL; ++i)
{
forwards.push_back(nullptr);
}
}
};
class SkipList : public Container
{
private:
SKNode *head;
SKNode *NIL;
int randomLevel();
public:
SkipList()
{
head = new SKNode(0, 0, SKNodeType::HEAD);
NIL = new SKNode(INT_MAX, 0, SKNodeType::NIL);
for (int i = 0; i < MAX_LEVEL; ++i)
{
head->forwards[i] = NIL;
}
srand(1);
}
void Insert(int key, int value);
void Search(int key);
void Delete(int key);
void Display();
~SkipList()
{
SKNode *n1 = head;
SKNode *n2;
while (n1)
{
n2 = n1->forwards[0];
delete n1;
n1 = n2;
}
}
};