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