36 std::vector<std::shared_ptr<Node>>
46 for (
int i = 0; i < (level + 1); i++) {
77 while (
static_cast<float>(std::rand()) / RAND_MAX <
PROBABILITY &&
91 std::cout <<
"Inserting" << key <<
"...";
92 std::shared_ptr<Node> x =
header;
96 for (
int i =
level; i >= 0; i--) {
97 while (x->forward[i] !=
nullptr && x->forward[i]->key < key) {
105 bool doesnt_exist = (x ==
nullptr || x->key != key);
109 if (rlevel >
level) {
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;
122 std::cout <<
"Inserted" << std::endl;
125 std::cout <<
"Exists" << std::endl;
134 std::shared_ptr<Node> x =
header;
139 for (
int i =
level; i >= 0; i--) {
140 while (x->forward[i] !=
nullptr && x->forward[i]->key < key) {
148 bool doesnt_exist = (x ==
nullptr || x->key != key);
151 for (
int i = 0; i <=
level; i++) {
152 if (
update[i]->forward[i] != x) {
155 update[i]->forward[i] = x->forward[i];
159 std::cout <<
"Deleted" << std::endl;
161 std::cout <<
"Doesn't exist" << std::endl;
171 std::shared_ptr<Node> x =
header;
172 std::cout <<
"Searching for " << key << std::endl;
174 for (
int i =
level; i >= 0; i--) {
175 while (x->forward[i] && x->forward[i]->key < key) x = x->forward[i];
179 if (x && x->key == key) {
180 std::cout <<
"Found" << std::endl;
183 std::cout <<
"Not Found" << std::endl;
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 <<
" ";
200 std::cout << std::endl;
213 std::srand(std::time(
nullptr));
int level
Maximum level of the skiplist.
void insertElement(int key, void *value)
void deleteElement(int key)
std::shared_ptr< Node > header
Pointer to the header node.
void * searchElement(int key)
constexpr float PROBABILITY
Current probability for "coin toss".
constexpr int MAX_LEVEL
Maximum level of skip list.
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.
Node(int key, int level, void *value=nullptr)
void * value
pointer of value
std::vector< std::shared_ptr< Node > > forward
nodes of the given one in all levels