-
Notifications
You must be signed in to change notification settings - Fork 7
/
SkipList.cpp
85 lines (72 loc) · 1.78 KB
/
SkipList.cpp
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
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
#include<iostream>
using namespace std;
// Link to the problem: https://leetcode.com/problems/design-skiplist/
/**
* Your Skiplist object will be instantiated and called as such:
* Skiplist* obj = new Skiplist();
* bool param_1 = obj->search(target);
* obj->add(num);
* bool param_3 = obj->erase(num);
*/
struct Node {
Node(Node* right, Node* down, int value) {
this->right = right;
this->down = down;
this->value = value;
}
Node* right;
Node* down;
int value;
};
class Skiplist {
private:
Node* head;
public:
Skiplist() { head = new Node(nullptr, nullptr, -1); }
void add(int num) {
Node* p = head;
vector<Node*> insertPoints;
while (p) {
while (p->right and p->right->value < num) p = p->right;
insertPoints.push_back(p);
p = p->down;
}
bool insertUp = true;
Node* downNode = nullptr;
while (insertUp and insertPoints.size() > 0) {
p = insertPoints.back();
insertPoints.pop_back();
p->right = new Node(p->right, downNode, num);
downNode = p->right;
insertUp = (rand() & 1) == 0;
}
if (insertUp) {
p = new Node(new Node(nullptr, downNode, num), head, -1);
}
}
bool search(int num) {
Node* p = head;
while (p) {
while (p->right and p->right->value < num) p = p->right;
if (!p->right or p->right->value > num) p = p->down;
else return true;
}
return false;
}
bool erase(int num) {
Node* p = head;
bool seen = false;
while (p) {
while (p->right and p->right->value < num) p = p->right;
if (!p->right or p->right->value > num) p = p->down;
else {
seen = true;
Node* target = p->right;
p->right = target->right;
p = p->down;
delete target;
}
}
return seen;
}
};