skip_list

This commit is contained in:
enqidu 2020-07-07 13:46:52 +04:00
parent 94338f527b
commit 2c6b8c3bf7

View File

@ -27,7 +27,7 @@ public:
/*Forward Array*/ /*Forward Array*/
vector<Node*> forward; vector<Node*> forward;
Node(int key, int level, void* value); Node(int key, int level, void* value);
}; }
Node::Node(int key, int level, void* value) { Node::Node(int key, int level, void* value) {
@ -37,7 +37,7 @@ Node::Node(int key, int level, void* value) {
for (int i = 0; i < sizeof(Node*)*(level+1); i++){ for (int i = 0; i < sizeof(Node*)*(level+1); i++){
forward.push_back(NULL); forward.push_back(NULL);
} }
}; }
// Class for Skip list // Class for Skip list
class SkipList { class SkipList {
@ -50,14 +50,13 @@ public:
void deleteElement(int); void deleteElement(int);
void* searchElement(int); void* searchElement(int);
void displayList(); void displayList();
}; }
SkipList::SkipList() { SkipList::SkipList() {
level = 0; level = 0;
/* Header initialization*/ /* Header initialization*/
header = new Node(-1, MAXLVL, NULL); header = new Node(-1, MAXLVL, NULL);
}; }
/** /**
* Returns random level for skip list; * Returns random level for skip list;
@ -66,7 +65,7 @@ int SkipList::randomLevel() {
int lvl = 0; int lvl = 0;
while(rand()%2 < P && lvl < MAXLVL) lvl++; while(rand()%2 < P && lvl < MAXLVL) lvl++;
return lvl; return lvl;
}; }
// Insert given key in skip list // Insert given key in skip list
@ -104,8 +103,7 @@ void SkipList::insertElement(int key, void* value) {
} else { } else {
printf("Exists\n"); printf("Exists\n");
} }
}; }
void SkipList::deleteElement(int key) void SkipList::deleteElement(int key)
{ {
@ -134,7 +132,7 @@ void SkipList::deleteElement(int key)
} else { } else {
printf("Doesnt Exists\n"); printf("Doesnt Exists\n");
} }
}; }
/** /**
@ -156,7 +154,7 @@ void* SkipList::searchElement(int key) {
printf("NOT FOUND\n"); printf("NOT FOUND\n");
return NULL; return NULL;
} }
}; }
// Display skip list level wise // Display skip list level wise
void SkipList::displayList() { void SkipList::displayList() {
@ -170,10 +168,9 @@ void SkipList::displayList() {
} }
printf("\n"); printf("\n");
} }
}; }
int main() int main() {
{
srand((unsigned)time(0)); srand((unsigned)time(0));
SkipList lst; SkipList lst;
@ -184,4 +181,5 @@ int main()
} }
lst.displayList(); lst.displayList();
return 0;
} }