Repository navigation
Expand file tree
/
Copy pathThreeTenLinkedList.java
More file actions
349 lines (297 loc) · 8.49 KB
/
Copy pathThreeTenLinkedList.java
File metadata and controls
349 lines (297 loc) · 8.49 KB
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
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
import java.util.Comparator;
/**
* Class to implement a linked list.
*
* @param <T> Generic parameter
* @author Justin Thomas
*/
class ThreeTenLinkedList<T> {
/**
* method that checks if the specified list is sorted.
*
* @param pairs Node pairs to compare
* @param comp value to use to compare
* @param <X> generic
* @return whether the list is sorted or not
*/
static <X> boolean isSorted(NodePair<X> pairs, Comparator<X> comp) {
//Determine if the provided list is sorted based on the comparator.
if(pairs == null || comp == null){
throw new IllegalArgumentException("Null Values");
}
else if(pairs.head == null && pairs.tail == null){
return true;
}
else{
Node<X> head = pairs.head;
Node<X> tail = pairs.tail;
Node<X> curr = head;
while (curr.next != null && comp.compare(curr.data, curr.next.data) <= 0) {
curr = curr.next;
}
if(curr.data == tail.data){
return true;
}
}
return false;
}
/**
* Sorts the linked list by comparing the nodes and shifting to the left if one is
* element is smaller than the other.
*
* @param pairs Node pairs to compare
* @param comp value to use to compare
* @param <X> generic
* @return new Node pair
*/
static <X> NodePair<X> sort(NodePair<X> pairs, Comparator<X> comp) {
Node<X> head = pairs.head;
Node<X> tail = pairs.tail;
while(!isSorted(pairs, comp)){
Node<X> curr = head;
while (curr != null) {
Node<X> curr2 = curr.next;
while(curr2 != null && comp.compare(curr.data, curr2.data) <= 0){
//System.out.println("Comparing "+curr.data+" with "+curr2.data);
curr2 = curr2.next;
}
if(curr2 != null){
X temp = curr2.data;
curr2.data = curr.data;
curr.data = temp;
}
curr = curr.next;
}
}
return new NodePair<>(head, tail);
}
/**
* initialize Node head.
*/
private Node<T> head;
/**
* initialize Node tail.
*/
private Node<T> tail;
/**
* initialize variable size.
*/
private int size;
/**
* construct an empty linked list.
*/
public ThreeTenLinkedList(){
head = null;
tail = null;
size = 0;
}
/**
* method that inserts a new Node at the head of the linked list.
*
* @param data value being passed to node
*/
public void insertAtHead(T data) {
if(head == null){
head = new Node<>(data);
tail = head;
}
else{
Node<T> node = new Node<>(data);
node.next = head;
head.prev = node;
head = node;
}
size++;
}
/**
* insert a new node at the end of a list.
*
* @param data value being passed to node
*/
public void insert(T data) {
Node<T> newNode = new Node<>(data);
if(head == null) {
head = tail = newNode;
head.prev = null;
tail.next = null;
}
else {
tail.next = newNode;
newNode.prev = tail;
tail = newNode;
tail.next = null;
}
size++;
}
/**
* get the head of the list.
*
* @return head
*/
public Node<T> getHead(){
return head;
}
/**
* get the tail of node.
*
* @return tail
*/
public Node<T> getTail(){
return tail;
}
/**
* get the size of a linked list (# of elements).
*
* @return size
*/
public int getSize(){
return size;
}
/**
* prints out the linked list by printing each element from left to right.
*/
public void display() {
if(head == null) {
return;
}
Node curr = head;
while(curr != null) {
System.out.print(curr.data + " ");
curr = curr.next;
}
System.out.println("");
}
/**
* delete the current list's head and return the value being deleted.
*
* @return the value being deleted or null
*/
public T deleteHead() {
if(head != null){
T data = head.data;
head.prev = null;
head = head.next;
size--;
return data;
}
return null;
}
/**
* method that deletes a specific a value that a user passes and
* it iterates through the list to delete the specific value and
* returns true if it was deleted or else false.
*
* @param data value to be deleted
* @return whether the value that was passed was deleted or not from the list
*/
public boolean delete(T data) {
if (head == null || data == null) {
return false;
}
if (head.data.equals(data)) {
if(deleteHead() != null){
return true;
}
}
if (tail.data.equals(data)) {
tail = tail.prev;
tail.next = null;
size--;
return true;
}
else{
Node<T> curr = head.next;
Node<T> prev = head;
while(curr != null){
if(curr.data.equals(data)){
curr = curr.next;
prev.next = curr;
curr.prev = prev;
size--;
return true;
}
curr = curr.next;
prev = prev.next;
}
}
return false;
}
/**
* main method of the class.
*
* @param args CDL arguments
*/
public static void main(String[] args){
ThreeTenLinkedList<Integer> list = new ThreeTenLinkedList<>();
Comparator<Integer> comp = new Comparator<Integer>() {
public int compare(Integer i1, Integer i2) {
return i1 - i2;
}
};
list.insert(44);
list.insert(20);
list.insert(10);
list.insert(95);
list.insert(47);
list.insert(56);
list.insert(48);
list.insert(7);
list.insert(6);
list.insert(10);
System.out.print("Before Sorting: ");
list.display();
ThreeTenLinkedList.sort(new NodePair<>(list.getHead(), list.getTail()), comp);
System.out.print("After Sorting: ");
list.display();
System.out.println("Sorted? "+ThreeTenLinkedList.isSorted(new NodePair<>(list.getHead(), list.getTail()), comp));
System.exit(0);
list.display();
list.delete(10);
list.insert(15);
list.insert(25);
list.delete(25);
list.insert(235);
list.display();
list.delete(95);
list.delete(56);
list.delete(44);
list.delete(20);
list.delete(48);
list.delete(15);
list.delete(235);
list.delete(47);
list.insert(101);
list.delete(101);
list.display();
list.insertAtHead(99);
list.insertAtHead(98);
list.insertAtHead(97);
System.out.println("Last: ");
list.display();
}
/**
* Node Pair nested class provided.
*
* @param <Y> generic identifier
*/
public static class NodePair<Y> {
/**
* head Node for Y.
*/
public Node<Y> head;
/**
* tail Node for Y.
*/
public Node<Y> tail;
/**
* constructor for NodePair.
*
* @param head Node
* @param tail Node
*/
public NodePair(Node<Y> head, Node<Y> tail) {
this.head = head;
this.tail = tail;
}
}
}