C Program to Implement Skip List

This is a C Program to implement Skip List. The idea is simple, we create multiple layers so that we can skip some nodes. The worst case time complexity is number of nodes on “express lane” plus number of nodes in a segment (A segment is number of “normal lane” nodes between two “express lane” nodes) of “normal lane”. So if we have n nodes on “normal lane”, √n nodes on “express lane” and we equally divide the “normal lane”, then there will be √n nodes in every segment of “normal lane” . √n is actually optimal division with two layers. With this arrangement, the number of nodes traversed for a search will be O(√n). Therefore, with O(√n) extra space, we are able to reduce the time complexity to O(√n).

Here is source code of the C Program to Implement Skip List. The C program is successfully compiled and run on a Linux system. The program output is also shown below.

  1. /* Skip Lists: A Probabilistic Alternative to Balanced Trees */
  2.  
  3. #include <stdlib.h>
  4. #include <stdio.h>
  5. #include <limits.h>
  6.  
  7. #define SKIPLIST_MAX_LEVEL 6
  8.  
  9. typedef struct snode {
  10.     int key;
  11.     int value;
  12.     struct snode **forward;
  13. } snode;
  14.  
  15. typedef struct skiplist {
  16.     int level;
  17.     int size;
  18.     struct snode *header;
  19. } skiplist;
  20.  
  21. skiplist *skiplist_init(skiplist *list) {
  22.     int i;
  23.     snode *header = (snode *) malloc(sizeof(struct snode));
  24.     list->header = header;
  25.     header->key = INT_MAX;
  26.     header->forward = (snode **) malloc(
  27.             sizeof(snode*) * (SKIPLIST_MAX_LEVEL + 1));
  28.     for (i = 0; i <= SKIPLIST_MAX_LEVEL; i++) {
  29.         header->forward[i] = list->header;
  30.     }
  31.  
  32.     list->level = 1;
  33.     list->size = 0;
  34.  
  35.     return list;
  36. }
  37.  
  38. static int rand_level() {
  39.     int level = 1;
  40.     while (rand() < RAND_MAX / 2 && level < SKIPLIST_MAX_LEVEL)
  41.         level++;
  42.     return level;
  43. }
  44.  
  45. int skiplist_insert(skiplist *list, int key, int value) {
  46.     snode *update[SKIPLIST_MAX_LEVEL + 1];
  47.     snode *x = list->header;
  48.     int i, level;
  49.     for (i = list->level; i >= 1; i--) {
  50.         while (x->forward[i]->key < key)
  51.             x = x->forward[i];
  52.         update[i] = x;
  53.     }
  54.     x = x->forward[1];
  55.  
  56.     if (key == x->key) {
  57.         x->value = value;
  58.         return 0;
  59.     } else {
  60.         level = rand_level();
  61.         if (level > list->level) {
  62.             for (i = list->level + 1; i <= level; i++) {
  63.                 update[i] = list->header;
  64.             }
  65.             list->level = level;
  66.         }
  67.  
  68.         x = (snode *) malloc(sizeof(snode));
  69.         x->key = key;
  70.         x->value = value;
  71.         x->forward = (snode **) malloc(sizeof(snode*) * (level + 1));
  72.         for (i = 1; i <= level; i++) {
  73.             x->forward[i] = update[i]->forward[i];
  74.             update[i]->forward[i] = x;
  75.         }
  76.     }
  77.     return 0;
  78. }
  79.  
  80. snode *skiplist_search(skiplist *list, int key) {
  81.     snode *x = list->header;
  82.     int i;
  83.     for (i = list->level; i >= 1; i--) {
  84.         while (x->forward[i]->key < key)
  85.             x = x->forward[i];
  86.     }
  87.     if (x->forward[1]->key == key) {
  88.         return x->forward[1];
  89.     } else {
  90.         return NULL;
  91.     }
  92.     return NULL;
  93. }
  94.  
  95. static void skiplist_node_free(snode *x) {
  96.     if (x) {
  97.         free(x->forward);
  98.         free(x);
  99.     }
  100. }
  101.  
  102. int skiplist_delete(skiplist *list, int key) {
  103.     int i;
  104.     snode *update[SKIPLIST_MAX_LEVEL + 1];
  105.     snode *x = list->header;
  106.     for (i = list->level; i >= 1; i--) {
  107.         while (x->forward[i]->key < key)
  108.             x = x->forward[i];
  109.         update[i] = x;
  110.     }
  111.  
  112.     x = x->forward[1];
  113.     if (x->key == key) {
  114.         for (i = 1; i <= list->level; i++) {
  115.             if (update[i]->forward[i] != x)
  116.                 break;
  117.             update[i]->forward[1] = x->forward[i];
  118.         }
  119.         skiplist_node_free(x);
  120.  
  121.         while (list->level > 1 && list->header->forward[list->level]
  122.                 == list->header)
  123.             list->level--;
  124.         return 0;
  125.     }
  126.     return 1;
  127. }
  128.  
  129. static void skiplist_dump(skiplist *list) {
  130.     snode *x = list->header;
  131.     while (x && x->forward[1] != list->header) {
  132.         printf("%d[%d]->", x->forward[1]->key, x->forward[1]->value);
  133.         x = x->forward[1];
  134.     }
  135.     printf("NIL\n");
  136. }
  137.  
  138. int main() {
  139.     int arr[] = { 3, 6, 9, 2, 11, 1, 4 }, i;
  140.     skiplist list;
  141.     skiplist_init(&list);
  142.  
  143.     printf("Insert:--------------------\n");
  144.     for (i = 0; i < sizeof(arr) / sizeof(arr[0]); i++) {
  145.         skiplist_insert(&list, arr[i], arr[i]);
  146.     }
  147.     skiplist_dump(&list);
  148.  
  149.     printf("Search:--------------------\n");
  150.     int keys[] = { 3, 4, 7, 10, 111 };
  151.  
  152.     for (i = 0; i < sizeof(keys) / sizeof(keys[0]); i++) {
  153.         snode *x = skiplist_search(&list, keys[i]);
  154.         if (x) {
  155.             printf("key = %d, value = %d\n", keys[i], x->value);
  156.         } else {
  157.             printf("key = %d, not fuound\n", keys[i]);
  158.         }
  159.     }
  160.  
  161.     printf("Search:--------------------\n");
  162.     skiplist_delete(&list, 3);
  163.     skiplist_delete(&list, 9);
  164.     skiplist_dump(&list);
  165.  
  166.     return 0;
  167. }

Output:

$ gcc Skiplist.c
$ ./a.out
 
Insert:--------------------
1[1]->2[2]->3[3]->4[4]->6[6]->9[9]->11[11]->NIL
Search:--------------------
key = 3, value = 3
key = 4, value = 4
key = 7, not fuound
key = 10, not fuound
key = 111, not fuound
Search:--------------------
1[1]->2[2]->4[4]->6[6]->11[11]->NIL

Sanfoundry Global Education & Learning Series – 1000 C Programs.

advertisement
advertisement

Here’s the list of Best Books in C Programming, Data Structures and Algorithms.

If you find any mistake above, kindly email to [email protected]

advertisement
advertisement
Subscribe to our Newsletters (Subject-wise). Participate in the Sanfoundry Certification contest to get free Certificate of Merit. Join our social networks below and stay updated with latest contests, videos, internships and jobs!

Youtube | Telegram | LinkedIn | Instagram | Facebook | Twitter | Pinterest
Manish Bhojasia - Founder & CTO at Sanfoundry
Manish Bhojasia, a technology veteran with 20+ years @ Cisco & Wipro, is Founder and CTO at Sanfoundry. He lives in Bangalore, and focuses on development of Linux Kernel, SAN Technologies, Advanced C, Data Structures & Alogrithms. Stay connected with him at LinkedIn.

Subscribe to his free Masterclasses at Youtube & discussions at Telegram SanfoundryClasses.