void SkipList<T>::skipListInsert(const T& key){ nodePtr curr[maxLevel], prev[maxLevel], newNode; int lvl, i; curr[maxLevel-1] = root[maxLevel-1]; prev[maxLevel-1] = 0; for(lvl = maxLevel -1; lvl >= 0; lvl--){ while(curr[lvl] && curr[lvl]->key < key){ prev[lvl] = curr[lvl]; curr[lvl] = *(curr[lvl]->next + lvl); } if(curr[lvl] && curr[lvl]->key == key) return; if(lvl > 0) if(prev[lvl] == 0){ curr[lvl-1] = root[lvl-1]; prev[lvl-1] = 0; } else{ curr[lvl-1] = *(prev[lvl]->next+ lvl -1); prev[lvl-1] = prev[lvl]; } } lvl = chooseLevel(); newNode = new SkipListNode<T>; newNode->next = new nodePtr[sizeof(nodePtr) * (lvl+1)]; newNode->key = key; for(i = 0; i <= lvl; i++){ *(newNode->next + i) == curr[i]; if(prev[i] == 0) root[i] = newNode; else *(prev[i]->next + i) = newNode; } }
void SkipList<T>::insert(const T& info) { // does NOT permit duplicates nodePtr curr[MAX_LEVELS], prev[MAX_LEVELS], newNode; int lvl, i; curr[maxLevel-1] = root[maxLevel-1]; prev[maxLevel-1] = NULL; for (lvl = maxLevel - 1; lvl >= 0; lvl--) { while (curr[lvl] && curr[lvl]->info < info) { // go to the next at the same level prev[lvl] = curr[lvl]; curr[lvl] = *(curr[lvl]->next + lvl); } // while larger and nodes at this level if (curr[lvl] && curr[lvl]->info == info) // don't include duplicates return; if (lvl > 0) // if not at bottom level { if (prev[lvl] == NULL) // if prev is root { curr[lvl - 1] = root[lvl - 1]; // lower curr starts at root prev[lvl - 1] = NULL; // lower prev set to indicate at root } else // else prev is not root { curr[lvl - 1] = *(prev[lvl]->next + lvl - 1); // lower curr starts at the next of the lower level of the prev prev[lvl - 1] = prev[lvl]; // lower prev set to the prev of this level } } } // for each lvl lvl = chooseLevel(); // generate randomly level for newNode; // newNode = new SkipListNode<T>; commented out by Sean // newNode->next = new nodePtr[sizeof(nodePtr) * (lvl+1)]; commented out by Sean // newNode->info = info; commented out by Sean newNode = new SkipListNode<T>(info, new nodePtr[lvl + 1]); // line substituted by Sean for (i = 0; i <= lvl; i++) { // initialize next fields of newNode and reset prev to newNode *(newNode->next + i) = curr[i]; if (prev[i] == NULL) // prev is root root[i] = newNode; else // prev is not root *(prev[i]->next + i) = newNode; } // each level of the new node. } // insert()