-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathLinkedListImpl.java
More file actions
361 lines (318 loc) · 10.6 KB
/
Copy pathLinkedListImpl.java
File metadata and controls
361 lines (318 loc) · 10.6 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
350
351
352
353
354
355
356
357
358
359
360
361
package com.example;
/**
* A simple LinkedList implementation of the interface.
*
* Notes: I could have used a hashMap for O(1) search operations, but as this was a linkedList excersize, i opted to not.
* It also complicated the removal overhead.
*/
public class LinkedListImpl implements InterfaceLL<DataClass> {
//Constructor for the LinkedList
LinkedListImpl(DataClass data){
Node<DataClass> node = new Node<>(data);
this.head = node;
this.tail = node;
size = 1; //added new node, size is now 1
}
/**
* Takes an array of type String and adds to the list
* @param array Array of DataClass to ad to the list
* @throws IllegalStateException if the array given is empty
*/
public void addFromArray(String array[])
{
if (array.length == 0)
{
throw new IllegalStateException("Error: Given array is empty");
}
for (String string : array)
{
this.add(new DataClass(string, this.size+1)); //Zero-ith based numbering
}
}
/**
* Builds a string of the class and returns a string of all the data in reverse order unless the list is empty.
* @return String
*/
public String reversePrint()
{
if (this.isEmpty())
{
return "List is Empty";
}
if (this.size == 1)
{
head.getData().toString();
}
StringBuilder sb = new StringBuilder();
Node<DataClass> currNode = this.tail;
int position = size;
while (currNode != null) {
updatePosition(currNode, position);
sb.append(currNode.getData().toString());
currNode = currNode.previous;
position--;
}
return sb.toString();
}
/**
* Given a number, searches the list for the accociated word.
* @param index
* @throws IndexOutOfBoundsException if the word to search for given the index does not exist
* @return
*/
//@TODO: edit logic so that it starts from the most optimal place in the list for searching. (half of ListSize)
public String searchForWord(int index)
{
//@TODO: check for "off by one" error
if(index > size)
{
throw new IndexOutOfBoundsException("Given index is larger than size of List");
}
//set node placement for best search operation
Node<DataClass> currNode = this.head; //init
boolean isReversed = false;
int position = 1;
if (index >= size / 2)
{
currNode = this.tail;
isReversed = true;
position = size;
}
boolean wordNotFound = true;
String returnWord = "";
while (currNode != null)
{
updatePosition(currNode, position);
if (currNode.data.getNumber() == index)
{
returnWord = currNode.data.getWord();
wordNotFound = false;
break;
} else {
currNode = isReversed ? currNode.previous : currNode.next;
position = isReversed ? position - 1 : position + 1;
}
}
if (wordNotFound)
{
throw new Error("Index given does not exist in list. It may hae been removed or cuorrupted.");
} else return returnWord;
}
/**
* Searches for a word in the list and returns its position number.
* @param word the word to search for
* @throws IndexOutOfBoundsException if the list is empty
* @return Integer the position number of the word
*/
public Integer searchForNumber(String word)
{
word = word.toLowerCase();
if(this.isEmpty())
{
throw new IndexOutOfBoundsException("List is empty");
}
if (word.equals(this.tail.getData().getWord()))
{
return this.tail.getData().getNumber();
}
Node<DataClass> currNode = this.head;
int position = 1;
while (currNode != null)
{
updatePosition(currNode, position);
if (currNode.data.getWord().toLowerCase().equals(word))
{
return currNode.data.getNumber();
}
currNode = currNode.next;
position++;
}
throw new Error("Word given does not exist in list. It may have been removed or corrupted.");
}
//=============================
// implementation Overrides
//==============================
@Override
public void add(DataClass element){
Node<DataClass> node = new Node<>(element);
if(this.isEmpty())
{
size = 1;
node.getData().setNumber(size);
this.head = node;
this.tail = node;
}else
{
size++;
node.getData().setNumber(size);
this.tail.next = node;
node.previous = this.tail;
this.tail = node;
}
}
@Override
public DataClass remove(int index) {
if (this.isEmpty()) {
throw new IllegalStateException("Cannot remove from an empty list");
}
if (index < 1 || index > size) {
throw new IndexOutOfBoundsException("Index " + index + " is out of bounds. List size: " + size);
}
Node<DataClass> currNode;
// Start from the optimal end
if (index <= size / 2) {
currNode = this.head;
for (int i = 1; i < index; i++) {
currNode = currNode.next;
}
} else {
currNode = this.tail;
for (int i = size; i > index; i--) {
currNode = currNode.previous;
}
}
DataClass removedData = currNode.getData();
// Handle removal based on position
if (currNode == this.head && currNode == this.tail) {
// Only one element
this.head = null;
this.tail = null;
} else if (currNode == this.head) {
// Remove head
this.head = currNode.next;
this.head.previous = null;
} else if (currNode == this.tail) {
// Remove tail
this.tail = currNode.previous;
this.tail.next = null;
} else {
// Remove middle element
currNode.previous.next = currNode.next;
currNode.next.previous = currNode.previous;
}
// Update size
size--;
// Lazy renumbering: will be updated when nodes are encountered during iteration/search
return removedData;
}
@Override
public DataClass getHead(){
if (this.isEmpty())
{
throw new IllegalStateException();
}
this.head.getData().setNumber(1); //lazy update
return this.head.getData();
}
@Override
public DataClass getTail(){
if (this.isEmpty())
{
throw new IllegalStateException();
}
this.tail.getData().setNumber(size); //lazy update
return this.tail.getData();
}
@Override
public int size(){
return size;
}
@Override
public boolean isEmpty(){
return size == 0 || head == null;
}
@Override
public String toString() {
if (this.isEmpty())
{
return "List is Empty";
}
if (this.size == 1)
{
updatePosition(this.head, 1);
return head.getData().toString();
}
StringBuilder sb = new StringBuilder();
Node<DataClass> currNode = this.head;
int position = 1;
while (currNode != null) {
updatePosition(currNode, position);
sb.append(currNode.getData().toString());
currNode = currNode.next;
position++;
}
return sb.toString();
}
//=======================================
//LinkedList class variables
//=======================================
private Node<DataClass> head;
private Node<DataClass> tail;
private int size;
//======================================
//Private Methods
// =============================
/**
* Helper method for lazy renumbering since that cuts down on
* the removal method's overhead. Updates a node's number
* when the node is encountered during iteration or search.
* @param node the node to update
* @param position the position in the list
*/
private void updatePosition(Node<DataClass> node, int position) {
int oldNumber = node.getData().getNumber();
if (oldNumber != position) {
node.getData().setNumber(position);
}
}
//=====================================
//LinkedList private classes
//=====================================
/**
* Internal Node implementation to represent each element in the linked list.
*/
public static class Node<DataClass> {
private final DataClass data;
private Node<DataClass> next;
private Node<DataClass> previous;
Node(DataClass data) {
this.data = data;
this.next = null;
this.previous = null;
}
Node(DataClass data, Node<DataClass> next)
{
this.data = data;
this.next = next;
this.previous = null;
}
Node(DataClass data, Node<DataClass> next, Node<DataClass> previous)
{
this.data = data;
this.next = next;
this.previous = previous;
}
public DataClass getData() {
return data;
}
public Node<DataClass> getNext() {
return next;
}
public Node<DataClass> getPrevious()
{
return previous;
}
@Override
public String toString()
{
StringBuilder sb = new StringBuilder();
sb.append(this.data.toString());
sb.append("Next ");
sb.append(this.next.toString());
sb.append("Previous: ");
sb.append(this.previous.toString());
sb.append("\n");
return sb.toString();
}
}
}