Java Program to Implement Skip List

This is a Java Program to implement a Skip List. A skip list is a data structure for storing a sorted list of items using a hierarchy of linked lists that connect increasingly sparse subsequences of the items. These auxiliary lists allow item lookup with efficiency comparable to balanced binary search trees (that is, with number of probes proportional to log n instead of n). This program is based on the implementation by Mark Allen Weiss.

Here is the source code of the Java program to implement a Skip List. The Java program is successfully compiled and run on a Windows system. The program output is also shown below.

  1. /*
  2.  * Java program to implement SkipList 
  3.  */
  4. import java.util.Scanner;
  5.  
  6. /* Class SkipNode */
  7. class SkipNode        
  8. {
  9.     int element;
  10.     SkipNode right;
  11.     SkipNode down;
  12.  
  13.     /* Constructor */
  14.     public SkipNode(int x)
  15.     {
  16.         this(x, null, null);
  17.     }
  18.     /* Constructor */
  19.     public SkipNode(int x, SkipNode rt, SkipNode dt)
  20.     {
  21.         element = x;
  22.         right = rt;
  23.         down = dt;
  24.     }
  25. }
  26.  
  27. /* Class SkipList */
  28. class SkipList
  29. {
  30.     private SkipNode header;
  31.     private int infinity;
  32.     private SkipNode bottom = null;
  33.     private SkipNode tail = null;
  34.  
  35.     /* Constructor */
  36.     public SkipList(int inf)
  37.     {
  38.         infinity = inf;
  39.         bottom = new SkipNode(0);
  40.         bottom.right = bottom.down = bottom;
  41.         tail = new SkipNode(infinity);
  42.         tail.right = tail;
  43.         header = new SkipNode(infinity, tail, bottom);
  44.     }
  45.     /* Function to insert element */
  46.     public void insert(int x)
  47.     {
  48.         SkipNode current = header;
  49.         bottom.element = x;
  50.         while (current != bottom)
  51.         {
  52.             while (current.element < x)
  53.             current = current.right;
  54.             /*  If gap size is 3 or at bottom level and must insert, then promote middle element */
  55.             if (current.down.right.right.element < current.element)
  56.             {
  57.                 current.right = new SkipNode(current.element, current.right, current.down.right.right);
  58.                 current.element = current.down.right.element;
  59.             }
  60.             else
  61.                 current = current.down;
  62.         }
  63.         /* Raise height of skiplist if necessary */
  64.         if (header.right != tail)
  65.             header = new SkipNode(infinity, tail, header);
  66.     }
  67.     /* Function to clear list */
  68.     public void makeEmpty()
  69.     {
  70.         header.right = tail;
  71.         header.down = bottom;
  72.     }
  73.     /* Function to check if empty */
  74.     public boolean isEmpty()
  75.     {
  76.         return header.right == tail && header.down == bottom;
  77.     }
  78.     /* Function to get node at a position */
  79.     private int elementAt(SkipNode t)
  80.     {
  81.         return t == bottom ? 0 : t.element;
  82.     }
  83.     /* Function to print list */
  84.     public void printList()
  85.     {
  86.         System.out.print("\nSkiplist = ");
  87.         SkipNode current = header;
  88.         while( current.down != bottom )
  89.             current = current.down;
  90.         while (current.right != tail )
  91.         {
  92.             System.out.print(current.element +" ");
  93.             current = current.right;
  94.         }
  95.         System.out.println();
  96.     }   
  97. }
  98.  
  99. /* Class SkipListTest */
  100. public class SkipListTest    
  101. {    
  102.     public static void main(String[] args)
  103.     {            
  104.         Scanner scan = new Scanner(System.in);
  105.         /* Creating object of SkipList */
  106.         SkipList sl = new SkipList(100000000); 
  107.         System.out.println("SkipList List Test\n");          
  108.         char ch;
  109.         /*  Perform list operations  */
  110.         do
  111.         {
  112.             System.out.println("\nSkipList List Operations\n");
  113.             System.out.println("1. insert");
  114.             System.out.println("2. check empty");
  115.             System.out.println("3. clear");
  116.  
  117.             int choice = scan.nextInt();            
  118.             switch (choice)
  119.             {
  120.             case 1 : 
  121.                 System.out.println("Enter integer element to insert");
  122.                 sl.insert( scan.nextInt() );   
  123.                 break;                          
  124.             case 2 : 
  125.                 System.out.println("Empty status = "+ sl.isEmpty());
  126.                 break;            
  127.             case 3 : 
  128.                 System.out.println("List cleared\n");
  129.                 sl.makeEmpty();
  130.                 break;                         
  131.             default : 
  132.                 System.out.println("Wrong Entry \n ");
  133.                 break;   
  134.             }    
  135.             /*  Display List  */ 
  136.             sl.printList();
  137.             System.out.println("\nDo you want to continue (Type y or n) \n");
  138.             ch = scan.next().charAt(0);    
  139.  
  140.         } while (ch == 'Y'|| ch == 'y');               
  141.     }
  142. }

SkipList List Test
 
 
SkipList List Operations
 
1. insert
2. check empty
3. clear
1
Enter integer element to insert
45
 
Skiplist = 45
 
Do you want to continue (Type y or n)
 
y
 
SkipList List Operations
 
1. insert
2. check empty
3. clear
1
Enter integer element to insert
23
 
Skiplist = 23 45
 
Do you want to continue (Type y or n)
 
y
 
SkipList List Operations
 
1. insert
2. check empty
3. clear
1
Enter integer element to insert
87
 
Skiplist = 23 45 87
 
Do you want to continue (Type y or n)
 
y
 
SkipList List Operations
 
1. insert
2. check empty
3. clear
1
Enter integer element to insert
56
 
Skiplist = 23 45 56 87
 
Do you want to continue (Type y or n)
 
y
 
SkipList List Operations
 
1. insert
2. check empty
3. clear
1
Enter integer element to insert
19
 
Skiplist = 19 23 45 56 87
 
Do you want to continue (Type y or n)
 
y
 
SkipList List Operations
 
1. insert
2. check empty
3. clear
1
Enter integer element to insert
81
 
Skiplist = 19 23 45 56 81 87
 
Do you want to continue (Type y or n)
 
y
 
SkipList List Operations
 
1. insert
2. check empty
3. clear
1
Enter integer element to insert
23
 
Skiplist = 19 23 45 56 81 87
 
Do you want to continue (Type y or n)
 
y
 
SkipList List Operations
 
1. insert
2. check empty
3. clear
3
List cleared
 
 
Skiplist =
 
Do you want to continue (Type y or n)
 
y
 
SkipList List Operations
 
1. insert
2. check empty
3. clear
2
Empty status = true
 
Skiplist =
 
Do you want to continue (Type y or n)
 
n

Sanfoundry Global Education & Learning Series – 1000 Java Programs.

advertisement
advertisement
If you wish to look at all Java Programming examples, go to Java Programs.

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.