-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathGenericSingleLinkedList.java
More file actions
264 lines (227 loc) · 6.88 KB
/
GenericSingleLinkedList.java
File metadata and controls
264 lines (227 loc) · 6.88 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
package com.example.data;
import java.util.ArrayList;
import java.util.List;
import com.example.data.exception.EmptyDataStructureException;
import com.example.data.exception.EmptyQueueException;
public class GenericSingleLinkedList<T extends Comparable<? super T>> {
private Node root;
GenericSingleLinkedList() {
}
class Node {
T data;
Node next;
Node minBeforeThis;
Node(T data) {
this.data = data;
this.next = null;
}
Node(T data, Node next) {
this.data = data;
this.next = next;
}
}
class Block {
Node node;
int size;
Block(Node node, int size) {
this.node = node;
this.size = size;
}
}
@SuppressWarnings("unchecked")
public T[] serialize() {
if (this.root == null) return null;
List<T> arrayOfElements = new ArrayList<T>();
Node currentNode = this.root;
while (currentNode != null) {
arrayOfElements.add(currentNode.data);
currentNode = currentNode.next;
}
T[] serializedLinkedList = (T[]) new Object[arrayOfElements.size()];
for (int i = 0; i < arrayOfElements.size(); i++) {
serializedLinkedList[i] = arrayOfElements.get(i);
}
return serializedLinkedList;
}
public T getMin() {
if (this.root == null) return null;
return this.root.minBeforeThis.data;
}
public Node getMinNode() {
if (this.root == null) return null;
return this.root.minBeforeThis;
}
public boolean isEmpty() {
return this.root == null;
}
public GenericSingleLinkedList<T> add(T data) {
Node node = new Node(data, this.root);
if (this.root == null) {
node.minBeforeThis = node;
} else {
node.minBeforeThis = data.compareTo(this.root.minBeforeThis.data) < 0
? node : this.root.minBeforeThis;
}
this.root = node;
return this;
}
public void deleteNodeWithData(T data) {
if (data == null || this.root == null) return;
if (root.minBeforeThis.data.compareTo(data) > 0) {
// Nodes lining up from the head of the data structure
// to the node pointed by the root.minBeforeThis should
// have data values greater than the data value to be
// deleted from the list. Moreover, any subsequent block
// of zero or more nodes pointing to the same node with
// minimum data value with that block should have data
// values greater than that of the query data value.
// This means that there is no node with the query value
// that can be deleted from the list.
return;
}
Node currentNode = this.root;
Node headOfBlock;
Node nodeToReach;
boolean found = false;
// Find the block where to search for the data value.
while (currentNode.minBeforeThis.next
.minBeforeThis.data.compareTo(data) < 0) {
currentNode = currentNode.minBeforeThis.next;
}
// Scan through the block to find a match.
headOfBlock = currentNode;
nodeToReach = currentNode.minBeforeThis.next;
while (!currentNode.equals(nodeToReach)) {
if (currentNode.data.compareTo(data) != 0) {
currentNode = currentNode.next;
} else {
found = true;
break;
}
}
Node temp = headOfBlock;
if (found) {
if (currentNode.minBeforeThis.equals(currentNode)) {
// When the node to delete is the one with
// the minimum data value within the block.
while (!temp.equals(currentNode.next)) {
add(temp.data);
temp = temp.next;
}
headOfBlock.data = currentNode.next.data;
headOfBlock.minBeforeThis = currentNode.next.minBeforeThis;
} else if (currentNode.next.equals(currentNode.minBeforeThis)) {
// When the node to delete is the immediate right neighbor
// of the node with the minimum data value within the block.
currentNode.data = currentNode.next.data;
currentNode.next = currentNode.next.next;
currentNode.minBeforeThis = currentNode;
while (!temp.equals(currentNode.next)) {
temp.minBeforeThis = currentNode;
temp = temp.next;
}
} else {
currentNode.data = currentNode.next.data;
currentNode.next = currentNode.next.next;
}
}
}
public Node peek() {
return this.root;
}
public void reverse() {
if (this.root == null ||
(this.root != null && this.root.next == null))
return;
Node previous = null;
Node current = this.root;
Node next = this.root.next;
while (current != null) {
current.next = previous;
previous = current;
current = next;
if (next != null) next = next.next;
else this.root = previous;
}
}
public GenericSingleLinkedList<T> deleteDuplicates() {
if (this.root == null || this.root.next == null) return this;
boolean retainPrimaryPointer = false;
Node primaryPointer = this.root;
Node secondaryPointer = this.root.next;
while (primaryPointer != null) {
retainPrimaryPointer = false;
secondaryPointer = primaryPointer.next;
while (secondaryPointer != null) {
if (primaryPointer.data.compareTo(secondaryPointer.data) == 0) {
if (secondaryPointer.next == null) {
primaryPointer.data = primaryPointer.next.data;
primaryPointer.next = primaryPointer.next.next;
retainPrimaryPointer = true;
} else {
secondaryPointer.data = secondaryPointer.next.data;
secondaryPointer.next = secondaryPointer.next.next;
}
}
secondaryPointer = secondaryPointer.next;
}
if (!retainPrimaryPointer) primaryPointer = primaryPointer.next;
}
return this;
}
public void sort() {
if (this.root == null || this.root.next == null) return;
GenericQueue<Block> queue = new GenericQueue<Block>();
Node pointer = this.root;
while (pointer != null)
queue.write(new Block(pointer, 1));
queue.write(new Block(null, 0)); // Delimiter
Block blockOne = null;
Block blockTwo = null;
while (true) {
try {
blockOne = queue.read();
if (blockOne.size == 0) {
queue.write(blockOne);
continue;
}
} catch (EmptyQueueException e) {}
try {
blockTwo = queue.read();
if (blockTwo.size == 0) {
if (blockOne.node == this.root) break;
queue.write(blockOne);
queue.write(blockTwo);
continue;
}
} catch (EmptyQueueException e) {}
queue.write(mergeSort(blockOne, blockTwo));
}
}
private Block mergeSort(Block blockOne, Block blockTwo) {
if (blockTwo == null) return blockOne;
Node listOneRoot = blockOne.node;
Node listTwoRoot = blockTwo.node;
int sizeOne = blockOne.size;
int sizeTwo = blockTwo.size;
Node firstPointer = listOneRoot;
Node secondPointer = listTwoRoot;
int firstListCounter = 0;
int secondListCounter = 0;
while (firstListCounter < sizeOne && secondListCounter < sizeTwo) {
if (firstPointer.data.compareTo(secondPointer.data) >= 0) {
Node node = new Node(firstPointer.data, firstPointer.next);
firstPointer.next = node;
firstPointer.data = secondPointer.data;
firstPointer = firstPointer.next;
secondPointer.data = secondPointer.next.data;
secondPointer.next = secondPointer.next.next;
secondListCounter++;
} else {
firstPointer = firstPointer.next;
firstListCounter++;
}
}
return new Block(listOneRoot, blockOne.size + blockTwo.size);
}
}