-
Notifications
You must be signed in to change notification settings - Fork 0
/
LinkedList.java
488 lines (362 loc) · 9.96 KB
/
LinkedList.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
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
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
/* filename: LinkedList.java (Double-Linked List)
author: Anh Uong
date: October 7, 2014
*/
import java.util.*;
/* LinkedList-method of arranging list (order) with Nodes and pointers
* holds data, user cannot access data without going through LinkedList
* this.head points to the first Node
* this.tail points to the last Node
* numItems keeps track of number of Nodes
* Iterator allows easy traversal of list by keeping track of previous
and next Nodes
*/
public class LinkedList<T> implements Iterable<T>{
private Node head; //head is the list
private int numItems;
private Node tail;
/*initializes LinkedList as empty list
with head and tail pointing to nothing (null)
and numItems in list as 0
*/
public LinkedList(){
this.head = null;
this.numItems = 0;
this.tail = null;
}
/* empties LinkedList */
public void clear(){
this.head = null;
this.numItems = 0;
this.tail = null;
}
/* returns size of LinkedList */
public int size(){
return this.numItems;
}
/* returns the data of the Node head is pointing to */
public T getHead(){
return this.head.getThing();
}
/* adds an item to the LinkedList (OLD) */
public void add(T item){
Node temp = this.head;
this.head = new Node(item);
this.head.setNext(temp);
this.numItems += 1;
}
/* inserts item AT INDEX in LinkedList
* keeps track of head and tail pointers and updates numItems
* exception cases met for if insert into index out of range,
insert into empty list, list of one item, and list of multiple items
*/
public void insert(T item, int index){
if(index > size()){
System.out.println("Index out of range. Must be less than " + size() );
}
//if empty list
else if(size() == 0){
Node newItem = new Node(item);
this.head = newItem;
this.tail = newItem;
this.head.setNext(null);
this.head.setPrev(null);
this.numItems++;
}
//add to first index if size of list is 1
else if(size() == 1 && index == 0){
Node temp = this.head;
Node newItem = new Node(item);
this.head = newItem;
this.head.setNext(temp);
this.head.setPrev(null);
temp.setPrev(newItem);
this.tail = temp;
this.numItems++;
}
//add to any other index in list
else{
//add to first index
if(index == 0){
Node temp = this.head;
Node newItem = new Node(item);
this.head = newItem;
this.head.setNext(temp);
this.head.setPrev(null);
temp.setPrev(newItem);
this.numItems++;
}
//add to last index
else if(index == size()){
Node temp = this.tail;
this.tail = new Node(item);
this.tail.setNext(null);
this.tail.setPrev(temp);
temp.setNext(this.tail);
this.numItems++;
}
//add to any other index in list
else{
Node temp = this.head;
for(int n=0; n<index-1; n++){
temp = temp.getNext();
}
Node newItem = new Node(item);
newItem.setNext(temp.getNext());
newItem.setPrev(temp);
temp.setNext(newItem);
newItem.getNext().setPrev(newItem);
numItems++;
}
}
}
/* removes an object from the LinkedList (OLD) */
public boolean remove(Object obj){
Node temp = this.head;
//if first value is object
if(temp.getThing().equals(obj)){
temp.setNext(temp.getNext().getNext());
this.numItems = this.numItems - 1;
return true;
}
for(int n=0; n<size()-1; n++){
if(temp.getNext().getThing().equals(obj)){
temp.setNext(temp.getNext().getNext());
this.numItems = this.numItems - 1;
return true;
}
else if(temp.getNext().getThing() == null){
return false;
}
temp = temp.getNext();
}
return false;
}
/* deletes given object from list if found in list
* keeps track of head and tail pointers and updates numItems
* handles special cases of if delete from empty list, list with one item,
list with multiple items (if delete from first, last, or middle)
*/
public boolean delete(Object obj){
//if empty list
if(size() == 0){
System.out.println("Cannot remove, no items in list");
return false;
}
//if list has one item
else if(size() == 1){
if(this.head.getThing().equals(obj)){
this.head = null;
this.tail = null;
this.numItems--;
return true;
}
return false;
}
//delete in lists with multiple items
else{
//if first value is object
if(this.head.getThing().equals(obj)){
Node temp = this.head;
this.head = temp.getNext();
temp.getNext().setPrev(null);
this.numItems--;
return true;
}
//if last value is object
else if(this.tail.getThing().equals(obj)){
Node temp = this.tail;
this.tail = temp.getPrev();
temp.getPrev().setNext(null);
this.numItems--;
return true;
}
//if any other value is object
else{
Node temp = this.head;
for(int n=0; n<size()-1; n++){
temp = temp.getNext();
if(temp.getThing().equals(obj)){
temp.getPrev().setNext(temp.getNext());
temp.getNext().setPrev(temp.getPrev());
this.numItems--;
return true;
}
}
return false;
}
}
}
/* creates a new LLIterator with head as list to traverse (constructor) */
public Iterator iterator(){
return new LLIterator(this.head);
}
/* returns an ArrayList of items in LinkedList */
public ArrayList<T> toArrayList(){
ArrayList<T> list = new ArrayList<T>();
for(T item: this){ //this goes through Iterator
list.add(item);
}
return list;
}
/* returns a shuffled ArrayList of items in LinkedList */
public ArrayList<T> toShuffledList(){
ArrayList<T> list = new ArrayList<T>();
for(T item: this){ //this goes through Iterator
list.add(item);
}
Collections.shuffle(list);
return list;
}
/* Nodes hold a piece of data and a pointer to the next Node */
private class Node{
private Node next;
private Node prev;
private T data;
/* initializes empty Node with designated data (item) */
public Node(T item){
this.next = null;
this.prev = null;
this.data = item;
}
/* returns data of Node */
public T getThing(){
return this.data;
}
/* sets the next Node to a certain Node */
public void setNext(Node n){
this.next = n;
}
/* returns the next Node */
public Node getNext(){
return this.next;
}
/* sets the previous Node to a given Node */
public void setPrev(Node p){
this.prev = p;
}
/* returns the previous Node */
public Node getPrev(){
return this.prev;
}
}
/* LLIterator keeps track the Nodes and what it points to next */
private class LLIterator implements Iterator<T>{
private Node nextNode;
private Node prevNode;
/* initializes LLIterator with the head of a list */
public LLIterator(Node head){
this.nextNode = head;
this.prevNode = tail;
}
/* returns true if the Node has a subsequent Node
* returns false if the Node has reached the end and thus
this.nextNode is null
*/
public boolean hasNext(){
if(this.nextNode != null){
return true;
}
else{
return false;
}
}
/* returns the next item in the list
* moves the traversal onto the next Node
*/
public T next(){
T temp = this.nextNode.getThing();
this.nextNode = this.nextNode.getNext();
return temp;
}
/* not needed to implement (yet...) */
public void remove(){
}
/* returns true if the Node has a previous Node
* returns false if the Node has reached the beginning
and thus this.prevNode is null
*/
public boolean hasPev(){
if(this.prevNode != null){
return true;
}
else{
return false;
}
}
/* returns the previous data in the list
* moves the traversal onto the next Node
*/
public T prev(){
T temp = this.prevNode.getThing();
this.prevNode = this.prevNode.getPrev();
return temp;
}
}
public static void main(String[] argv) {
LinkedList<Integer> llist = new LinkedList<Integer>();
//test with insert
llist.insert(5, 0);
llist.insert(10, 1);
llist.insert(0, 0);
System.out.printf("\nAfter setup %d\n", llist.size());
for(Integer item: llist) {
System.out.printf("thing %d\n", item);
}
llist.clear();
for (int i=0;i<20;i+=2) {
llist.insert( i, llist.size() );
}
System.out.printf("\nAfter setting %d\n", llist.size());
for (Integer item: llist) {
System.out.printf("thing %d\n", item);
}
llist.insert(1, 0);
llist.insert(13, llist.size());
llist.insert(11, 3);
System.out.printf("\nAfter inserting %d\n", llist.size());
for (Integer item: llist) {
System.out.printf("thing %d\n", item);
}
llist.insert(0, 15);
llist.clear();
//test with delete
System.out.println(llist.delete(0));
llist.insert(0, 0);
System.out.printf("\nAfter setting %d\n", llist.size());
for (Integer item: llist) {
System.out.printf("thing %d\n", item);
}
System.out.println(llist.delete(0));
System.out.printf("\nAfter deletingFirst %d\n", llist.size());
for (Integer item: llist) {
System.out.printf("thing %d\n", item);
}
llist.insert(1, 0);
llist.insert(0, 1);
System.out.printf("\nAfter setting %d\n", llist.size());
for (Integer item: llist) {
System.out.printf("thing %d\n", item);
}
llist.delete(0);
System.out.printf("\nAfter deletingLast %d\n", llist.size());
for (Integer item: llist) {
System.out.printf("thing %d\n", item);
}
for (int i=0;i<20;i+=2) {
llist.insert( i, llist.size() );
}
System.out.printf("\nAfter setting %d\n", llist.size());
for (Integer item: llist) {
System.out.printf("thing %d\n", item);
}
System.out.println(llist.delete(5));
System.out.println(llist.delete(6));
System.out.println(llist.delete(18));
System.out.println(llist.delete(1));
System.out.printf("\nAfter deleting %d\n", llist.size());
for (Integer item: llist) {
System.out.printf("thing %d\n", item);
}
}
}