fork download
  1. #include "stdlib.h"
  2. #include "stdio.h"
  3. #include "limits.h"
  4.  
  5. typedef struct _Node{
  6. int key;
  7. int val;
  8.  
  9. int topLevel;
  10. struct _Node **next;
  11. } Node;
  12.  
  13. typedef struct _SkipList{
  14. int maxLevel;
  15. Node *head, *tail;
  16.  
  17. Node **preds, **succs;
  18. } SkipList;
  19.  
  20. Node *create_node(int topLevel, int key, int val){
  21. Node *node;
  22. node = calloc(1, sizeof(Node));
  23. node->next = (Node **) calloc(topLevel+1, sizeof(Node *));
  24. node->key = key;
  25. node->val = val;
  26. node->topLevel = topLevel;
  27. return node;
  28. }
  29.  
  30. void free_node(Node *node){
  31. free(node->next);
  32. free(node);
  33. return;
  34. }
  35.  
  36. int search(SkipList *sl, int key){
  37. int res, level;
  38. Node *pre, *crr;
  39. pre = sl -> head;
  40. res = -1;
  41. for(level = sl->maxLevel - 1; level >= 0; --level){
  42. crr = pre->next[level];
  43. while(key > crr->key){
  44. pre = crr;
  45. crr = pre->next[level];
  46. }
  47. if(res == -1 && key == crr->key) res = level;
  48. sl->preds[level] = pre;
  49. sl->succs[level] = crr;
  50. }
  51. return res;
  52. }
  53.  
  54. int delete(SkipList *sl, int key){
  55. int level;
  56. int res;
  57. Node *node;
  58. if(search(sl, key) == -1) return -1;
  59. node = sl->preds[0]->next[0];
  60. for(level = node->topLevel; level>=0; --level)
  61. sl->preds[level]->next[level] = node->next[level];
  62. res = node->val;
  63. free_node(node);
  64. return res;
  65. }
  66.  
  67. SkipList *init_list(int maxLevel){
  68. SkipList *sl;
  69. Node *head, *tail;
  70. int i;
  71. sl = (SkipList *) calloc(1, sizeof(SkipList));
  72. sl->preds = (Node**) calloc(maxLevel+1, sizeof(Node*));
  73. sl->succs = (Node**) calloc(maxLevel+1, sizeof(Node*));
  74. sl -> maxLevel = maxLevel;
  75.  
  76. head = create_node(maxLevel, INT_MIN, 0);
  77. tail = create_node(maxLevel, INT_MAX, 0);
  78. for(i=0; i<maxLevel; ++i){
  79. head -> next[i] = tail;
  80. tail -> next[i] = NULL;
  81. }
  82. sl -> head = head;
  83. sl -> tail = tail;
  84. return sl;
  85. }
  86.  
  87. void free_list(SkipList *sl){
  88. Node *node;
  89. while(sl->head->next[0] != sl->tail) delete(sl, sl->head->next[0]->key);
  90. free_node(sl->head);
  91. free_node(sl->tail);
  92. free(sl->preds);
  93. free(sl->succs);
  94. free(sl);
  95. }
  96.  
  97.  
  98. int add(SkipList *sl, int key, int val){
  99. int level, topLevel, fLevel;
  100. Node *node;
  101. int r = rand();
  102. if((fLevel = search(sl, key)) != -1) return -1;
  103. topLevel = (r % sl->maxLevel);
  104. node = create_node(topLevel, key, val);
  105. for(level = 0; level <= topLevel; ++level){
  106. node->next[level] = sl->succs[level];
  107. sl->preds[level]->next[level] = node;
  108. }
  109. return 0;
  110. }
  111.  
  112. int find(SkipList *sl, int key){
  113. int res;
  114. if(search(sl, key) != -1) return -1;
  115. return sl->preds[0]->next[0]->val;
  116. }
  117.  
  118. void print_list(SkipList *sl){
  119. int i;
  120. Node *node;
  121. for(i=sl->maxLevel-1; i>=0; --i){
  122. printf("\tlevel %2d: ", i);
  123. for(node = sl->head->next[0]; node != sl->tail; node = node->next[0]){
  124. if(i <= node->topLevel)
  125. printf(" [%5d]", node->key);
  126. else
  127. printf(" ");
  128. }
  129. printf("\n");
  130. }
  131. return;
  132. }
  133.  
  134.  
  135.  
  136. int main(void) {
  137. int i;
  138. SkipList *sl;
  139.  
  140. int *nums;
  141. int t = 10;
  142.  
  143. nums = calloc(t, sizeof(int));
  144. sl = init_list(4);
  145.  
  146. for (i = 0; i < t; i++) {
  147. add(sl, (nums[i] = i), i);
  148. printf("add %d\n", nums[i]);
  149. print_list(sl);
  150. }
  151.  
  152. for (i = t - 1; 1 <= i; i--) {
  153. printf("delete %d\n", delete(sl, nums[i]));
  154. print_list(sl);
  155. }
  156.  
  157. free(nums);
  158. free_list(sl);
  159. exit(0);
  160. return 0;
  161. }
Success #stdin #stdout 0.01s 1852KB
stdin
Standard input is empty
stdout
add 0
	level  3:  [    0]
	level  2:  [    0]
	level  1:  [    0]
	level  0:  [    0]
add 1
	level  3:  [    0]        
	level  2:  [    0] [    1]
	level  1:  [    0] [    1]
	level  0:  [    0] [    1]
add 2
	level  3:  [    0]                
	level  2:  [    0] [    1]        
	level  1:  [    0] [    1] [    2]
	level  0:  [    0] [    1] [    2]
add 3
	level  3:  [    0]                 [    3]
	level  2:  [    0] [    1]         [    3]
	level  1:  [    0] [    1] [    2] [    3]
	level  0:  [    0] [    1] [    2] [    3]
add 4
	level  3:  [    0]                 [    3]        
	level  2:  [    0] [    1]         [    3]        
	level  1:  [    0] [    1] [    2] [    3] [    4]
	level  0:  [    0] [    1] [    2] [    3] [    4]
add 5
	level  3:  [    0]                 [    3]         [    5]
	level  2:  [    0] [    1]         [    3]         [    5]
	level  1:  [    0] [    1] [    2] [    3] [    4] [    5]
	level  0:  [    0] [    1] [    2] [    3] [    4] [    5]
add 6
	level  3:  [    0]                 [    3]         [    5]        
	level  2:  [    0] [    1]         [    3]         [    5] [    6]
	level  1:  [    0] [    1] [    2] [    3] [    4] [    5] [    6]
	level  0:  [    0] [    1] [    2] [    3] [    4] [    5] [    6]
add 7
	level  3:  [    0]                 [    3]         [    5]                
	level  2:  [    0] [    1]         [    3]         [    5] [    6]        
	level  1:  [    0] [    1] [    2] [    3] [    4] [    5] [    6]        
	level  0:  [    0] [    1] [    2] [    3] [    4] [    5] [    6] [    7]
add 8
	level  3:  [    0]                 [    3]         [    5]                        
	level  2:  [    0] [    1]         [    3]         [    5] [    6]                
	level  1:  [    0] [    1] [    2] [    3] [    4] [    5] [    6]         [    8]
	level  0:  [    0] [    1] [    2] [    3] [    4] [    5] [    6] [    7] [    8]
add 9
	level  3:  [    0]                 [    3]         [    5]                                
	level  2:  [    0] [    1]         [    3]         [    5] [    6]                        
	level  1:  [    0] [    1] [    2] [    3] [    4] [    5] [    6]         [    8] [    9]
	level  0:  [    0] [    1] [    2] [    3] [    4] [    5] [    6] [    7] [    8] [    9]
delete 9
	level  3:  [    0]                 [    3]         [    5]                        
	level  2:  [    0] [    1]         [    3]         [    5] [    6]                
	level  1:  [    0] [    1] [    2] [    3] [    4] [    5] [    6]         [    8]
	level  0:  [    0] [    1] [    2] [    3] [    4] [    5] [    6] [    7] [    8]
delete 8
	level  3:  [    0]                 [    3]         [    5]                
	level  2:  [    0] [    1]         [    3]         [    5] [    6]        
	level  1:  [    0] [    1] [    2] [    3] [    4] [    5] [    6]        
	level  0:  [    0] [    1] [    2] [    3] [    4] [    5] [    6] [    7]
delete 7
	level  3:  [    0]                 [    3]         [    5]        
	level  2:  [    0] [    1]         [    3]         [    5] [    6]
	level  1:  [    0] [    1] [    2] [    3] [    4] [    5] [    6]
	level  0:  [    0] [    1] [    2] [    3] [    4] [    5] [    6]
delete 6
	level  3:  [    0]                 [    3]         [    5]
	level  2:  [    0] [    1]         [    3]         [    5]
	level  1:  [    0] [    1] [    2] [    3] [    4] [    5]
	level  0:  [    0] [    1] [    2] [    3] [    4] [    5]
delete 5
	level  3:  [    0]                 [    3]        
	level  2:  [    0] [    1]         [    3]        
	level  1:  [    0] [    1] [    2] [    3] [    4]
	level  0:  [    0] [    1] [    2] [    3] [    4]
delete 4
	level  3:  [    0]                 [    3]
	level  2:  [    0] [    1]         [    3]
	level  1:  [    0] [    1] [    2] [    3]
	level  0:  [    0] [    1] [    2] [    3]
delete 3
	level  3:  [    0]                
	level  2:  [    0] [    1]        
	level  1:  [    0] [    1] [    2]
	level  0:  [    0] [    1] [    2]
delete 2
	level  3:  [    0]        
	level  2:  [    0] [    1]
	level  1:  [    0] [    1]
	level  0:  [    0] [    1]
delete 1
	level  3:  [    0]
	level  2:  [    0]
	level  1:  [    0]
	level  0:  [    0]