-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathSinglyLinkedList.java
More file actions
414 lines (359 loc) · 10 KB
/
Copy pathSinglyLinkedList.java
File metadata and controls
414 lines (359 loc) · 10 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
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
package edu.cofc.cs.csci230;
import java.util.ArrayList;
/**
* Singly LinkedList Data Structure
*
* @author CSCI 230: Data Structures and Algorithms Fall 2016
*
* @param <AnyType>
*/
public class SinglyLinkedList<AnyType extends Comparable> implements List<AnyType> {
// instance variables
private Node<AnyType> headNode = null;
private int size = 0;
/**
* Appends the specified element to the end of this list.
*
* @param t
*/
public void add(AnyType t) {
addNode(new Node<AnyType>(t));
} // end add() method
/**
* implementation for public add(AnyType t) method
*
* @param t
*/
private void addNode(Node<AnyType> t) {
if (isEmpty())
headNode = t;
else
getNode(size - 1).setNextNode(t);
size++;
} // end addNote() method
/**
* Inserts the specified element at the specified position in this list.
*
* @param index
* @param t
* @throws IndexOutOfBoundsException
*/
public void add(int index, AnyType t) throws IndexOutOfBoundsException {
addNode(index, new Node<AnyType>(t));
} // end add() method
/*
*
* Implementation for public add(int index, AnyType t) method
*
* @param index
*
* @param t
*
* @throws IndexOutOfBoundsException
*/
private void addNode(int index, Node<AnyType> t) throws IndexOutOfBoundsException {
if (index == 0 && size != 0) {
t.setNextNode(headNode);
headNode = t;
size++;
} else if (index == size) {
addNode(t);
} else if (index > 0 && index < size) {
Node<AnyType> beforeNewNode = getNode(index - 1);
Node<AnyType> newNextNode = beforeNewNode.getNextNode();
beforeNewNode.setNextNode(t);
t.setNextNode(newNextNode);
size++;
}
else if(size == 0 && index == 0){
add(t.getData());
}
else if(index > size || index < 0){
throw new IndexOutOfBoundsException();
}
/**
* ------------------------------------------- TODO: You fully implement
* this method
*
*/
} // end addNode() method
/**
* Replaces the element at the specified position in this list with the
* specified element.
*
* @param index
* @param t
* @throws IndexOutOfBoundsException
*/
public void set(int index, AnyType t) throws IndexOutOfBoundsException {
setNode(index, new Node<AnyType>(t));
} // end set() method
/**
*
* Implementation for public set( int index, AnyType t ) method
*
* @param index
* @param t
* @throws IndexOutOfBoundsException
*/
private void setNode(int index, Node<AnyType> t) throws IndexOutOfBoundsException {
getNode(index).setData(t.getData());
/**
* ------------------------------------------- TODO: You fully implement
* this method
*
*/
} // end setNode() method
/**
* Removes the element at the specified position in this list.
*
* @param index
* @return
* @throws IndexOutOfBoundsException
*/
public AnyType remove(int index) throws IndexOutOfBoundsException {
return removeNode(index).getData();
} // end remove() method
/**
*
* Implementation for public remove( int index ) method
*
* @param index
* @return
* @throws IndexOutOfBoundsException
*/
private Node<AnyType> removeNode(int index) throws IndexOutOfBoundsException {
Node<AnyType> removedNode = null;
if (index == 0) {
removedNode = headNode;
headNode = headNode.getNextNode();
} else if (index == size) {
Node<AnyType> beforeNode = getNode(index - 1);
removedNode = beforeNode.getNextNode();
beforeNode.setNextNode(null);
}
else if(index > size || index < 0){
throw new IndexOutOfBoundsException();
}
else {
Node<AnyType> beforeNode = getNode(index - 1);
removedNode = beforeNode.getNextNode();
Node<AnyType> afterNode = removedNode.getNextNode();
beforeNode.setNextNode(afterNode);
}
size--;
return removedNode;
/**
* ------------------------------------------- TODO: You fully implement
* this method
*
*/
} // end removeNode() method
/**
* Returns the element at the specified position in this list.
*
* @param index
* @return
* @throws IndexOutOfBoundsException
*/
public AnyType get(int index) throws IndexOutOfBoundsException {
return getNode(index).getData();
} // end get() method
/**
*
* Implementation for public get(int index) method
*
* @param index
* @return
* @throws IndexOutOfBoundsException
*/
private Node<AnyType> getNode(int index) throws IndexOutOfBoundsException {
if ( index < 0 || index > size )
throw new IndexOutOfBoundsException();
Node<AnyType> currentNode = headNode;
for (int i = 0; i < index; i++) {
currentNode = currentNode.getNextNode();
}
return currentNode;
/**
* ------------------------------------------- TODO: You fully implement
* this method
*
*/
} // end get() method
/**
* Returns the number of elements in this list.
*
* @return
*/
public int size() {
return size;
} // end size() method
/**
* Returns true if this list contains no elements.
*
* @return
*/
public Boolean isEmpty() {
return (size == 0) ? true : false;
} // end isEmpty() method
/**
* Removes all of the elements from this list.
*
*/
public void clear() {
int index = size;
for (int x = 1; x <= index; x++) {
Node<AnyType> nextNode = headNode.getNextNode();
headNode.setNextNode(null);
headNode = nextNode;
size--;
}
headNode = null;
/**
* ------------------------------------------- TODO: You fully implement
* this method
*
*/
} // end clear method
/**
*
* @param args
*/
public static void main(String[] args) {
System.out.println("--TEST--");
// make link list and arraylist
List<Integer> linkList = new SinglyLinkedList<Integer>();
ArrayList<Integer> aList = new ArrayList<Integer>();
// at 1-10 to each list
for (int i = 0; i < 10; i++) {
linkList.add(new Integer(i));
aList.add(new Integer(i));
}
// remove one from first index
try {
linkList.remove(0);
aList.remove(0);
System.out.println("removeing the FIRST index PASSED");
} catch (NullPointerException ex) {
System.out.println("removeing the FIRST index FAILED");
}
// remove one from the middle
try {
linkList.remove(5);
aList.remove(5);
System.out.println("removeing the MIDDLE index PASSED");
} catch (NullPointerException ex) {
System.out.println("removeing the MIDDLE index FAILED");
}
// remove from last index
try {
linkList.remove(linkList.size() - 1);
aList.remove(aList.size() - 1);
System.out.println("removeing the LAST index PASSED");
} catch (IndexOutOfBoundsException ex) {
System.out.println("removeing the LAST index FAILED");
}
// remove from after tail
try {
linkList.remove(linkList.size() + 1);
aList.remove(aList.size() + 1);
System.out.println("removeing AFTER TAIL index FAILD");
} catch (IndexOutOfBoundsException ex) {
System.out.println("removeing AFTER TAIL index PASSED");
}
// remove from before head
try {
linkList.remove(-1);
aList.remove(-1);
System.out.println("removeing BEFORE HEAD index FAILD");
} catch (IndexOutOfBoundsException ex) {
System.out.println("removeing BEFORE HEADindex PASSED");
}
///////////////////////////////////////////////////////////////////////////////////////////////////////
// ADD to FIRST INDEX
try {
linkList.add(0, new Integer(100));
aList.add(0, new Integer(100));
System.out.println("ADDING the FIRST index PASSED");
} catch (IndexOutOfBoundsException ex) {
System.out.println("ADDING the FIRST index FAILED");
}
// //ADD one to the middle
try {
linkList.add(5, new Integer(66));
aList.add(5, new Integer(66));
System.out.println("ADDING the MIDDLE index PASSED");
} catch (IndexOutOfBoundsException ex) {
System.out.println("ADDING the MIDDLE index FAILED");
}
// ADD to TAIL
try {
linkList.add(linkList.size(), new Integer(9999));
aList.add(aList.size(), new Integer(9999));
System.out.println("ADDING the LAST index PASSED");
} catch (IndexOutOfBoundsException ex) {
System.out.println("ADDING the LAST index FAILED");
}
// //REMOVING from after tail
try {
linkList.add(linkList.size() + 1, new Integer(123219));
aList.add(aList.size() + 1, new Integer(122399));
System.out.println("ADDING AFTER TAIL index FAILD");
} catch (IndexOutOfBoundsException ex) {
System.out.println("ADDING AFTER TAIL index PASSED");
System.out.println(ex);
}
// //remove from before head
try {
linkList.add(-1, new Integer(111111111));
aList.add(-1, new Integer(111111111));
System.out.println("removeing BEFORE HEAD index FAILD");
} catch (IndexOutOfBoundsException ex) {
System.out.println("ADDING BEFORE HEAD index PASSED");
System.out.println(ex);
}
/////////////////////////////////////////////////////////////////////////////////////
// set INDEX
try {
linkList.set(0, new Integer(1337));
System.out.println("SET FIRST INDEX PASSED");
} catch (IndexOutOfBoundsException ex) {
System.out.println(ex);
}
// set BEFORE HEAD
try {
linkList.set(-2, new Integer(420));
System.out.println("SET FIRST INDEX FAILED");
} catch (IndexOutOfBoundsException ex) {
System.out.println(ex);
}
// set BEFORE INDEX
try {
linkList.set(linkList.size() + 1, new Integer(420));
System.out.println("SET BEFORE INDEX FAILED");
} catch (IndexOutOfBoundsException ex) {
System.out.println(ex);
}
////////////////////////////////////////////////////////////////////////////////////////
//print out Link List
////////////////////////////////////////////////////////////////////////////////////////
System.out.println("LIST :");
for (int x = 0; x < linkList.size(); x++) {
System.out.println(linkList.get(x).toString());
}
System.out.println("CLEARED LIST");
//clear List
linkList.clear();
//test to see if list is cleared!
linkList.add(3, new Integer(0));
for (int x = 0; x < linkList.size(); x++) {
System.out.println(linkList.get(x).toString());
}
/**
* ------------------------------------------- TODO: You put your test
* cases here
*
*/
} // end main() method
} // end SinglyLinkedList class definition