Guest User

Untitled

a guest
Feb 19th, 2018
100
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 4.02 KB | None | 0 0
  1. #include<iostream>
  2. #include<cstdlib>
  3. #include<ctime>
  4. #include<limits>
  5.  
  6. using namespace std;
  7.  
  8. template<class T>
  9. class SkipList{
  10. private:
  11. class SkipNode{
  12. public:
  13. T* key;
  14. SkipNode** forward;
  15. int level;
  16. SkipNode(T* key, int maxlvl, int lvl){
  17. forward = new SkipNode*[maxlvl];
  18. this->key=key;
  19. level=lvl;
  20. }
  21. ~SkipNode(){}
  22. };
  23.  
  24. SkipNode *header,*NIL;
  25. float probability;
  26. int level;
  27. int MaxLevel;
  28.  
  29. int randomLevel(){
  30. int lvl = 0;
  31. while( (float(rand())/RAND_MAX < probability) && (lvl < MaxLevel-1) )
  32. lvl++;
  33. return lvl;
  34. }
  35.  
  36. public:
  37. SkipList(float probability, int maxlvl){
  38. this->probability = probability;
  39. MaxLevel = maxlvl;
  40. srand(time(0));
  41. header=new SkipNode(NULL,MaxLevel,0);
  42. T* maxValue = new T;
  43. *maxValue = numeric_limits<T>::max();
  44. NIL = new SkipNode(maxValue,MaxLevel,MaxLevel);
  45. level=0;
  46. for(int i=0; i<MaxLevel; i++){
  47. header->forward[i]=NIL;
  48. }
  49. }
  50.  
  51. ~SkipList(){
  52. delete header;
  53. delete NIL;
  54. }
  55.  
  56. SkipNode* search(T* key){
  57. SkipNode* cursor = header;
  58. for(int i=level; i>=0; i--)
  59. while(*(cursor->forward[i]->key) < (*key))
  60. cursor=cursor->forward[0];
  61. cursor=cursor->forward[0];
  62. if(*(cursor->key) == *key)
  63. return cursor;
  64. return NULL;
  65. }
  66.  
  67. SkipList* insert(T* key){
  68. SkipNode* cursor = header;
  69. SkipNode* update[MaxLevel];
  70. for(int i=level; i>=0; i--){
  71. while(*(cursor->forward[i]->key) < *(key))
  72. cursor=cursor->forward[i];
  73. update[i]=cursor;
  74. }
  75. cursor=cursor->forward[0];
  76. if(*(cursor->key) == *(key)) //Node already inserted
  77. return this;
  78. int lvl = randomLevel();
  79. if(lvl > level){
  80. for(int i=level; i<=lvl; i++)
  81. update[i]=header;
  82. level=lvl;
  83. }
  84. SkipNode* x = new SkipNode(key,MaxLevel,lvl);
  85. for(int i=0; i<=lvl; i++){
  86. x->forward[i] = update[i]->forward[i];
  87. update[i]->forward[i] = x;
  88. }
  89. return this;
  90. }
  91.  
  92. SkipList* erase(T* key){
  93. SkipNode* cursor = header;
  94. SkipNode* update[MaxLevel];
  95. for(int i=level; i>=0; i--){
  96. while(*(cursor->forward[i]->key) < *(key))
  97. cursor=cursor->forward[i];
  98. update[i]=cursor;
  99. }
  100. cursor=cursor->forward[0];
  101. if(*(cursor->key) == *(key)){
  102. for(int i=0; i<=level && update[i]->forward[i] == cursor; i++){
  103. update[i]->forward[i] = cursor->forward[i];
  104. }
  105. delete cursor;
  106. while(level>0 && header->forward[level]==NIL){
  107. level=level-1;
  108. }
  109. }
  110. return this;
  111. }
  112.  
  113. SkipList* print(){
  114. SkipNode* cursor = header->forward[0];
  115. int i=1;
  116. while (cursor != NIL) {
  117. cout << "(" << *cursor->key << "," << cursor->level << ") ";
  118. cursor = cursor->forward[0];
  119. if(i%15==0) cout << endl; i++;
  120. }
  121. cout << endl;
  122. return this;
  123. }
  124. };
  125.  
  126. main(){
  127. SkipList<int>* list = new SkipList<int>(0.80, 10);
  128. int a=2;
  129. int b=3;
  130. list->insert(&a)->insert(&b);
  131. list->print()->erase(&a)->erase(&b)->insert(&a)->print();
  132. int v[100];
  133. for(int i=0; i<100; i++){
  134. v[i]=rand()%100;
  135. list->insert(&v[i]);
  136. }
  137. list->print();
  138. cout << endl;
  139. cout << *list->search(&a)->key;
  140. delete list;
  141. }
Add Comment
Please, Sign In to add comment