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