-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathOLL.java
107 lines (95 loc) · 1.64 KB
/
OLL.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
import java.util.*;
public class OLL <T extends Comparable<T>>
{
private Node1<T> first;
private int size;
Set<T> values;
public OLL()
{
first = null;
size = 0;
values = new HashSet<T>();
}
public OLL(T obj)
{
first = new Node1<T>(obj);
size = 1;
values = new HashSet<T>();
values.add(obj);
}
public int add(T obj)
{
size++;
values.add(obj);
if(size == 0)
{
first = new Node1<T>(obj);
return 0;
}
else if(size == 1)
{
Node1<T> curr = new Node1<T>(obj);
if(obj.compareTo(first.getValue()) > 0)
{
first.setNext(curr);
return 1;
}
else
{
curr.setNext(first);
first = curr;
return 0;
}
}
else
{
Node1<T> curr = first;
Node1<T> place = new Node1<T>(obj);
if(obj.compareTo(first.getValue()) < 0)
{
place.setNext(first);
first = place;
return 0;
}
else
{
int count = 0;
while(curr.getNext() != null)
{
//System.out.println("obj: " + obj + " curr: " + curr.getValue() + " next: " + curr.getNext().getValue());
if(obj.compareTo(curr.getNext().getValue()) > 0)
{
count++;
curr = curr.getNext();
}
else
{
place.setNext(curr.getNext());
curr.setNext(place);
break;
}
}
curr.setNext(place);
return count;
}
}
}
public boolean isIn(T obj)
{
boolean ret;
ret = values.add(obj);
values.remove(obj);
return !ret;
}
public String toString()
{
String str = first.getValue().toString() + " ";
Node1<T> curr = first;
while(curr.getNext() != null)
{
curr = curr.getNext();
str += curr.getValue().toString() + " ";
}
return str;
}
}