TheAlgorithms/C++ 1.0.0
All the algorithms implemented in C++
Loading...
Searching...
No Matches
skip_list.cpp
Go to the documentation of this file.
1
16#include <array>
17#include <cstring>
18#include <ctime>
19#include <iostream>
20#include <memory>
21#include <vector>
22
26namespace data_structures {
27constexpr int MAX_LEVEL = 2;
28constexpr float PROBABILITY = 0.5;
29
33struct Node {
34 int key;
35 void* value;
36 std::vector<std::shared_ptr<Node>>
38
44 Node(int key, int level, void* value = nullptr) : key(key), value(value) {
45 // Initialization of forward vector
46 for (int i = 0; i < (level + 1); i++) {
47 forward.push_back(nullptr);
48 }
49 }
50};
51
55class SkipList {
56 int level;
57 std::shared_ptr<Node> header;
58
59 public:
65 level = 0;
66 // Header initialization
67 header = std::make_shared<Node>(-1, MAX_LEVEL);
68 }
69
76 int lvl = 0;
77 while (static_cast<float>(std::rand()) / RAND_MAX < PROBABILITY &&
78 lvl < MAX_LEVEL) {
79 lvl++;
80 }
81 return lvl;
82 }
83
90 void insertElement(int key, void* value) {
91 std::cout << "Inserting" << key << "...";
92 std::shared_ptr<Node> x = header;
93 std::array<std::shared_ptr<Node>, MAX_LEVEL + 1> update;
94 update.fill(nullptr);
95
96 for (int i = level; i >= 0; i--) {
97 while (x->forward[i] != nullptr && x->forward[i]->key < key) {
98 x = x->forward[i];
99 }
100 update[i] = x;
101 }
102
103 x = x->forward[0];
104
105 bool doesnt_exist = (x == nullptr || x->key != key);
106 if (doesnt_exist) {
107 int rlevel = randomLevel();
108
109 if (rlevel > level) {
110 for (int i = level + 1; i < rlevel + 1; i++) update[i] = header;
111
112 // Update current level
113 level = rlevel;
114 }
115
116 std::shared_ptr<Node> n =
117 std::make_shared<Node>(key, rlevel, value);
118 for (int i = 0; i <= rlevel; i++) {
119 n->forward[i] = update[i]->forward[i];
120 update[i]->forward[i] = n;
121 }
122 std::cout << "Inserted" << std::endl;
123
124 } else {
125 std::cout << "Exists" << std::endl;
126 }
127 }
128
133 void deleteElement(int key) {
134 std::shared_ptr<Node> x = header;
135
136 std::array<std::shared_ptr<Node>, MAX_LEVEL + 1> update;
137 update.fill(nullptr);
138
139 for (int i = level; i >= 0; i--) {
140 while (x->forward[i] != nullptr && x->forward[i]->key < key) {
141 x = x->forward[i];
142 }
143 update[i] = x;
144 }
145
146 x = x->forward[0];
147
148 bool doesnt_exist = (x == nullptr || x->key != key);
149
150 if (!doesnt_exist) {
151 for (int i = 0; i <= level; i++) {
152 if (update[i]->forward[i] != x) {
153 break;
154 }
155 update[i]->forward[i] = x->forward[i];
156 }
157 /* Remove empty levels*/
158 while (level > 0 && header->forward[level] == nullptr) level--;
159 std::cout << "Deleted" << std::endl;
160 } else {
161 std::cout << "Doesn't exist" << std::endl;
162 }
163 }
164
170 void* searchElement(int key) {
171 std::shared_ptr<Node> x = header;
172 std::cout << "Searching for " << key << std::endl;
173
174 for (int i = level; i >= 0; i--) {
175 while (x->forward[i] && x->forward[i]->key < key) x = x->forward[i];
176 }
177
178 x = x->forward[0];
179 if (x && x->key == key) {
180 std::cout << "Found" << std::endl;
181 return x->value;
182 } else {
183 std::cout << "Not Found" << std::endl;
184 return nullptr;
185 }
186 }
187
191 void displayList() {
192 std::cout << "Displaying list:\n";
193 for (int i = 0; i <= level; i++) {
194 std::shared_ptr<Node> node = header->forward[i];
195 std::cout << "Level " << (i) << ": ";
196 while (node != nullptr) {
197 std::cout << node->key << " ";
198 node = node->forward[i];
199 }
200 std::cout << std::endl;
201 }
202 }
203};
204
205} // namespace data_structures
206
212int main() {
213 std::srand(std::time(nullptr));
214
216
217 for (int j = 0; j < (1 << (data_structures::MAX_LEVEL + 1)); j++) {
218 int k = (std::rand() % (1 << (data_structures::MAX_LEVEL + 2)) + 1);
219 lst.insertElement(k, &j);
220 }
221
222 lst.displayList();
223
224 return 0;
225}
int level
Maximum level of the skiplist.
Definition skip_list.cpp:56
void insertElement(int key, void *value)
Definition skip_list.cpp:90
void deleteElement(int key)
std::shared_ptr< Node > header
Pointer to the header node.
Definition skip_list.cpp:57
void * searchElement(int key)
for IO operations
constexpr float PROBABILITY
Current probability for "coin toss".
Definition skip_list.cpp:28
constexpr int MAX_LEVEL
Maximum level of skip list.
Definition skip_list.cpp:27
void update(std::vector< int64_t > *segtree, std::vector< int64_t > *lazy, int64_t start, int64_t end, int64_t delta, uint64_t low, uint64_t high, uint64_t pos)
Updates a range of the segment tree.
Definition segtree.cpp:103
int main()
Node(int key, int level, void *value=nullptr)
Definition skip_list.cpp:44
void * value
pointer of value
Definition skip_list.cpp:35
int key
key integer
Definition skip_list.cpp:34
std::vector< std::shared_ptr< Node > > forward
nodes of the given one in all levels
Definition skip_list.cpp:37