This repository has been archived by the owner on Feb 7, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Skiplist_with_Reverse_Pointer.cpp
83 lines (83 loc) · 2.36 KB
/
Skiplist_with_Reverse_Pointer.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
#include <iostream>
#include <vector>
#include <limits>
class SkipNode {
public:
int key;
int height;
SkipNode* forward;
SkipNode* backward;
SkipNode(int k, int h) : key(k), height(h), forward(nullptr), backward(nullptr) {}
};
class SkipList {
private:
int maxLevel;
SkipNode* header;
int getRandomHeight() {
int height = 1;
while ((rand() % 2 == 0) && (height < maxLevel))
height++;
return height;
}
public:
SkipList(int maxLvl) : maxLevel(maxLvl) {
header = new SkipNode(std::numeric_limits<int>::min(), maxLevel);
}
~SkipList() {
while (header->forward) {
SkipNode* temp = header->forward;
header->forward = temp->forward;
delete temp;
}
delete header;
}
void insert(int key) {
std::vector<SkipNode*> update(maxLevel + 1, nullptr);
SkipNode* current = header;
for (int i = maxLevel; i >= 0; i--) {
while (current->forward && current->forward->key < key)
current = current->forward;
update[i] = current;
}
int newHeight = getRandomHeight();
if (newHeight > maxLevel) {
newHeight = maxLevel + 1;
update.resize(newHeight + 1);
update[maxLevel + 1] = header;
maxLevel = newHeight;
}
SkipNode* newNode = new SkipNode(key, newHeight);
for (int i = 0; i <= newHeight; i++) {
newNode->forward = update[i]->forward;
update[i]->forward = newNode;
if (i > 0) {
if (update[i]->forward)
update[i]->forward->backward = newNode;
newNode->backward = update[i];
}
}
}
void printSkipList() {
for (int i = maxLevel; i >= 0; i--) {
SkipNode* current = header->forward;
while (current) {
if (current->height >= i) {
std::cout << current->key << " ";
}
current = current->forward;
}
std::cout << std::endl;
}
}
};
int main() {
SkipList skipList(4);
skipList.insert(3);
skipList.insert(6);
skipList.insert(7);
skipList.insert(9);
skipList.insert(12);
std::cout << "Skip List:" << std::endl;
skipList.printSkipList();
return 0;
}