-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathSkipListDriver.java
121 lines (102 loc) · 2.62 KB
/
SkipListDriver.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
package rsn170330.lp2;
import rsn170330.lp2.Timer;
import java.io.File;
import java.io.FileNotFoundException;
import java.util.Iterator;
import java.util.Scanner;
//Driver program for skip list implementation.
public class SkipListDriver {
public static void main(String[] args) throws FileNotFoundException {
Scanner sc;
if (args.length > 0) {
File file = new File(args[0]);
sc = new Scanner(file);
}
else {
sc = new Scanner(System.in);
}
String operation = "";
long operand = 0;
int modValue = 999983;
long result = 0;
Long returnValue = null;
SkipList<Long> skipList = new SkipList<>();
// Initialize the timer
Timer timer = new Timer();
while (!((operation = sc.next()).equals("End"))) {
switch (operation) {
case "Add": {
operand = sc.nextLong();
if(skipList.add(operand)) {
result = (result + 1) % modValue;
}
break;
}
case "Ceiling": {
operand = sc.nextLong();
returnValue = skipList.ceiling(operand);
// System.out.println("Ceiling: " + returnValue);
if (returnValue != null) {
result = (result + returnValue) % modValue;
}
break;
}
case "First": {
returnValue = skipList.first();
if (returnValue != null) {
result = (result + returnValue) % modValue;
}
break;
}
case "Get": {
int intOperand = sc.nextInt();
returnValue = skipList.get(intOperand);
// System.out.println("Get: " + returnValue);
// skipList.printListSpan();
if (returnValue != null) {
result = (result + returnValue) % modValue;
}
break;
}
case "Last": {
returnValue = skipList.last();
if (returnValue != null) {
result = (result + returnValue) % modValue;
}
break;
}
case "Floor": {
operand = sc.nextLong();
returnValue = skipList.floor(operand);
// System.out.println("Floor: " + returnValue);
if (returnValue != null) {
result = (result + returnValue) % modValue;
}
break;
}
case "Remove": {
operand = sc.nextLong();
if (skipList.remove(operand) != null) {
result = (result + 1) % modValue;
}
break;
}
case "Contains":{
operand = sc.nextLong();
if (skipList.contains(operand)) {
result = (result + 1) % modValue;
}
break;
}
}
// System.out.print(result + "\n");
}
//int n = 33000;
//System.out.println("Linear: "+skipList.getLinear(n));;
//System.out.println("Log: \t"+skipList.getLog(n));;
// End Time
timer.end();
System.out.println(result);
System.out.println(timer);
}
}