Node file code below #ifndef NODE_H #define NODE_H #include using namespace std; // the node class Node{ public: int value; // Array of pointers to the next nodes (which may be located at various levels) // next[i] is the next link for level i // the size of this vector determines the number of levels that the current node is part of vector next; vector prev; // previous links for each level the node is part of Node(int val, int level){ value = val; next = vector(level+1, nullptr); // initialize array of pointers to nulls prev = vector(level+1, nullptr); // initialize array of pointers to nulls }; }; #endif SKIPLIST.H FILE CODE BELOW #include #include #include #include #include #include #include "Node.h" #ifndef SKIPLIST_H #define SKIPLIST_H using namespace std; class SkipList{ public: // Maximum allowed level index int MAXIMUM_ALLOWED_LEVEL_INDEX; // current maximum level amongst the inserted nodes int currentHighestLevelIndex; // the head node's next links are connected to the first node at every level Node *head; Node* tail; // last node at every level SkipList(int maxLevels){ MAXIMUM_ALLOWED_LEVEL_INDEX = maxLevels; // initially we have the bottom-most level only currentHighestLevelIndex = 0; // create the header node, value is irrelevant (as long as it doesn't match an inserted value - NO REPEATS), number of next links is important (initially this node is the first node at every level) head = new Node(INT_MIN, MAXIMUM_ALLOWED_LEVEL_INDEX); tail = new Node(INT_MAX, MAXIMUM_ALLOWED_LEVEL_INDEX); // last nodes at each level // connect head to tail at every level for(int i = 0; i <= MAXIMUM_ALLOWED_LEVEL_INDEX; i++){ head->next[i] = tail; // head's prev is null tail->prev[i] = head; // tail's next is null } } int RandomLevel(){ float probablity = (float)rand()/RAND_MAX; // flip a coin int lvl = 0; while (probablity < 0.5 && lvl < MAXIMUM_ALLOWED_LEVEL_INDEX){ lvl++; // landed heads so increase level by 1 probablity = (float)rand()/RAND_MAX; // flip a coin again } return lvl; } Node* CreateNode(int value, int level){ // create a new node with next links for every level that this node will be part of // it will use these links to connect to the next node at each level return new Node(value, level); } void InsertElement(int value){ Node *current = head; // start at head node vector update(MAXIMUM_ALLOWED_LEVEL_INDEX+1, nullptr); // this will hold the nodes that need updating at every level after the insert takes place for (int i = currentHighestLevelIndex; i >= 0; i--){ // start at the highest level and move down so that more nodes may be skipped // for level i, if value is to be inserted here then find out where (i.e. after which node) while (current->next[i] != nullptr && current->next[i]->value < value){ current = current->next[i]; } // found the node after which the value is to be placed at level i update[i] = current; // move down a level, if possible } // at level 0, where current is pointing to by the end of the preceding loop, move over.