TheAlgorithms/C++
1.0.0
All the algorithms implemented in C++
Toggle main menu visibility
Loading...
Searching...
No Matches
skip_list.cpp
Go to the documentation of this file.
1
15
16
#include <array>
17
#include <cstring>
18
#include <ctime>
19
#include <iostream>
20
#include <memory>
21
#include <vector>
22
26
namespace
data_structures
{
27
constexpr
int
MAX_LEVEL
= 2;
28
constexpr
float
PROBABILITY
= 0.5;
29
33
struct
Node
{
34
int
key
;
35
void
*
value
;
36
std::vector<std::shared_ptr<Node>>
37
forward
;
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
55
class
SkipList
{
56
int
level
;
57
std::shared_ptr<Node>
header
;
58
59
public
:
64
SkipList
() {
65
level
= 0;
66
// Header initialization
67
header
= std::make_shared<Node>(-1,
MAX_LEVEL
);
68
}
69
75
int
randomLevel
() {
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
212
int
main
() {
213
std::srand(std::time(
nullptr
));
214
215
data_structures::SkipList
lst;
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
}
data_structures::SkipList
Definition
skip_list.cpp:55
data_structures::SkipList::level
int level
Maximum level of the skiplist.
Definition
skip_list.cpp:56
data_structures::SkipList::insertElement
void insertElement(int key, void *value)
Definition
skip_list.cpp:90
data_structures::SkipList::SkipList
SkipList()
Definition
skip_list.cpp:64
data_structures::SkipList::displayList
void displayList()
Definition
skip_list.cpp:191
data_structures::SkipList::deleteElement
void deleteElement(int key)
Definition
skip_list.cpp:133
data_structures::SkipList::randomLevel
int randomLevel()
Definition
skip_list.cpp:75
data_structures::SkipList::header
std::shared_ptr< Node > header
Pointer to the header node.
Definition
skip_list.cpp:57
data_structures::SkipList::searchElement
void * searchElement(int key)
Definition
skip_list.cpp:170
main
int main()
Main function.
Definition
generate_parentheses.cpp:110
data_structures
for IO operations
data_structures::PROBABILITY
constexpr float PROBABILITY
Current probability for "coin toss".
Definition
skip_list.cpp:28
data_structures::MAX_LEVEL
constexpr int MAX_LEVEL
Maximum level of skip list.
Definition
skip_list.cpp:27
update
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
data_structures::Node::Node
Node(int key, int level, void *value=nullptr)
Definition
skip_list.cpp:44
data_structures::Node::value
void * value
pointer of value
Definition
skip_list.cpp:35
data_structures::Node::key
int key
key integer
Definition
skip_list.cpp:34
data_structures::Node::forward
std::vector< std::shared_ptr< Node > > forward
nodes of the given one in all levels
Definition
skip_list.cpp:37
node
Definition
binary_search_tree.cpp:11
data_structures
skip_list.cpp
Generated by
1.18.0