#include <vector>
class SkipList {
public:
// A node represents the whole column of element occurences on levels [1..level].
class Node {
public:
int value;
// pointers to successor nodes
std::vector<Node*> forward;
Node(int v, std::size_t level)
: value(v), forward(level, nullptr) {}
std::size_t level() const {
return forward.size();
}
};
public:
SkipList() : max_level_(16) {
head = new Node(0, max_level_);
std::fill(head->forward.begin(), head->forward.end(), nullptr);
}
~SkipList();
private:
// data members
const std::size_t max_level_;
Node* head; // pointer to first node
};
There is an Iterator defined as well, but that works just fine :).
I hope you can help me and thank you in advance!
Mae
The new node is supposed to be inserted at the place where the values before are lower and the values after higher.
So my idea was to store the nodes that come before the insertion place in the vector update, so that i can insert the new node right after update and adjust the pointers accordingly.
And yes, i actually coud let uniform provide a number between 0 and 16, thanks :D.
Okay, so i adjusted my code like this and strangely sometimes it is working well and sometimes it is only inserting some of the values i want to insert.
I see that level() returns 1 for the bottom level, not 0. That might be throwing off some other code.
I added a print() method to show the skiplist and a simple main() to read numbers from cin, insert them, and then print the list after each insertion. With this input:
Okay, here's another question regarding my erase function.
It is working very well for small amounts of data. But as soon as i let the programm process bigger amounts between 100.000 and 200.000 there is a Segmentation fault.