-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathLinkedList.java
More file actions
499 lines (380 loc) · 12.7 KB
/
LinkedList.java
File metadata and controls
499 lines (380 loc) · 12.7 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
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
487
488
489
490
491
492
493
494
495
496
497
498
499
//h15h4m
// just a basic Linked List implementation.
import java.util.NoSuchElementException;
public class LinkedList
{
//nested class to represent a node
private class Node
{
public Object data;
public Node next;
}
//only instance variable that points to the first node.
private Node first;
// Constructs an empty linked list.
public LinkedList()
{
first = null;
}
// Returns the first element in the linked list.
public Object getFirst()
{
if (first == null)
{
NoSuchElementException ex
= new NoSuchElementException();
throw ex;
}
else
return first.data;
}
// Removes the first element in the linked list.
public Object removeFirst()
{
if (first == null)
{
NoSuchElementException ex = new NoSuchElementException();
throw ex;
}
else
{
Object element = first.data;
first = first.next; //change the reference since it's removed.
return element;
}
}
// Adds an element to the front of the linked list.
public void addFirst(Object element)
{
//create a new node
Node newNode = new Node();
newNode.data = element;
newNode.next = first;
//change the first reference to the new node.
first = newNode;
}
// Returns an iterator for iterating through this list.
public ListIterator listIterator()
{
return new LinkedListIterator();
}
/*********************************************************
Add your methods here
*********************************************************/
// This method is going to concatenate the strings in the linked list
// and then it is going to return a string
// e.g. { Apple Banana Orange}
public String toString() {
// This is going to be the returned string.
String result = "{ ";
// This object will make us able to move in the linked list.
LinkedListIterator iterator = new LinkedListIterator();
try {
// while it list has a next element.
while(iterator.hasNext() )
{
// getting those strings.
result += iterator.next()+ " ";
}
}
// When I try to list when there is no element in the list the program crashes.
catch (NoSuchElementException e) {
System.out.println("No such element.");
}
// ending the final string.
result += "}\n";
return result;
}
// This method is going to check if the linked list is empty or not.
// true when empty, and false when not.
public boolean isEmpty() {
// so we can move in the linked lists.
LinkedListIterator iterator = new LinkedListIterator();
// if no value: true.
// if value : false
if (!iterator.hasNext())
return true;
else
return false;
}
// This is an additional method to find the size of the linked list.
// this is going to be useful since we are going to check the size of
// list in the other methods.
public int listSize() {
// This object will make us get in the linked list.
// we will need this in order to count the elements.
LinkedListIterator iterator = new LinkedListIterator();
// counter
int i = 0;
// if it has next. move to the next, and count.
while (iterator.hasNext())
{
iterator.next();
i++;
}
// returning the count.
return i;
}
// This method will add the parameter to the linked list in alphabetical order.
public void addElement(Object element) {
// This is going to make move in the list.
ListIterator iterator = listIterator();
// This variable will make it easier for us to
// know how to add the element later.
// true : make this element the last one in the list
boolean insertelementinLast = true;
// This variable will contain the current element value.
String currentelementValue = "";
// while there is a next element.
while ( iterator.hasNext( ) ) {
// contain the current value.
currentelementValue = (String) iterator.next();
// using compareTo() method in order to compare the strings alphabetically.
// We are going through each element until the new element is bigger(alphabetically)
// than the passed element, then we will know that is the place to add the new element.
if (currentelementValue.compareTo(element.toString()) > 0) {
// we will know now that we won't be able to add this element
insertelementinLast = false;
// breaking the loop.
break;
}
}
// when there is no elements in the list, or when the list
// is not empty, but the new element will be added to the end.
if ( isEmpty() || insertelementinLast )
{
iterator.add(element);
}
// The linked list is not empty in all cases
// add the new element to the beginning of the list.
// or add the element somewhere in the middle.
else
{
iterator.set(element);
iterator.add(currentelementValue);
}
}
// This method is going to remove the element in the selected index number
// and return it.
public Object removeElement(int index)
{
// this is going to contain the return value.
Object result = null;
// to access elements in the linked list.
LinkedListIterator iterator = new LinkedListIterator();
// checking our boundaries, so we throw the appropriate exception.
// In other words, making sure that the index parameter does not
// be less than 0 and not more than the list size.
if(index < 0) {
IndexOutOfBoundsException ex = new IndexOutOfBoundsException();
throw ex;
}
if(index > listSize() - 1) {
IndexOutOfBoundsException ex = new IndexOutOfBoundsException();
throw ex;
}
// getting the first element at the specified index.
result = getElement(index);
// checking if the index is less than list size
if( index < listSize() - 1 )
{
// moving toward the index
for( int i = 0 ; i < index + 1; i++ )
{
// go the next.
iterator.next();
}
while( index < listSize() - 2)
{
// setting a value in the current index from the next one.
iterator.set(getElement(index + 1));
// go to the next
iterator.next();
// add 1
index++;
}
// remove the current.
iterator.remove();
}
else
{
for( int i = 0 ; i < index + 1 ; i++ )
{
// go to the next
iterator.next();
}
// remove the current.
iterator.remove();
}
// return final result.
return result;
}
// This method is going to search for the string at the parameter index
// and returns it. The indexing is like any other array; starts from 0.
public Object getElement(int index) {
// to move in the list
LinkedListIterator iterator = new LinkedListIterator();
// checking the bounds
if (index < 0)
{
IndexOutOfBoundsException e = new IndexOutOfBoundsException();
throw e;
}
if (index > listSize() - 1) {
IndexOutOfBoundsException e = new IndexOutOfBoundsException();
throw e;
}
// we are going to stop right before the desired index.
for (int i = 0 ; i < index ; i++)
iterator.next(); // move to the next.
// return the next one.
return iterator.next();
}
// This searches for the oldString in the list and then replaces them
// with the second string newString. (All Occurrences).
public void searchAndReplace(Object oldString, Object newString) {
// so we move.
LinkedListIterator iterator = new LinkedListIterator();
while (iterator.hasNext())
{
// if the string in the array equals the oldString that
// we are looking for.
if ((iterator.next()).equals((String) oldString))
{
// set the value of this index with newString.
iterator.set(newString);
}
}
}
// This method searches for the parameter string with the largest index, and returns
// its value. If the string does not exist returns -1.
public int indexOfLast(Object searchString) {
LinkedListIterator iterator = new LinkedListIterator();
//
int i = 0;
// this will not change if we didn't find the string
// we are looking for.
int lastIndex = -1;
while (iterator.hasNext())
{
// checking if the string values in the index equals the parameter.
if ((iterator.next()).equals((String) searchString))
{
// saving the last index.
lastIndex = i;
}
// add 1 to i.
i++;
}
// return.
return lastIndex;
}
//nested class to define its iterator
private class LinkedListIterator implements ListIterator
{
private Node position; //current position
private Node previous; //it is used for remove() method
// Constructs an iterator that points to the front
// of the linked list.
public LinkedListIterator()
{
position = null;
previous = null;
}
// Tests if there is an element after the iterator position.
public boolean hasNext()
{
if (position == null) //not traversed yet
{
if (first != null)
return true;
else
return false;
}
else
{
if (position.next != null)
return true;
else
return false;
}
}
// Moves the iterator past the next element, and returns
// the traversed element's data.
public Object next()
{
if (!hasNext())
{
NoSuchElementException ex = new NoSuchElementException();
throw ex;
}
else
{
previous = position; // Remember for remove
if (position == null)
position = first;
else
position = position.next;
return position.data;
}
}
// Adds an element before the iterator position
// and moves the iterator past the inserted element.
public void add(Object element)
{
if (position == null) //never traversed yet
{
addFirst(element);
position = first;
}
else
{
//making a new node to add
Node newNode = new Node();
newNode.data = element;
newNode.next = position.next;
//change the link to insert the new node
position.next = newNode;
//move the position forward to the new node
position = newNode;
}
//this means that we cannot call remove() right after add()
previous = position;
}
// Removes the last traversed element. This method may
// only be called after a call to the next() method.
public void remove()
{
if (previous == position) //not after next() is called
{
IllegalStateException ex = new IllegalStateException();
throw ex;
}
else
{
if (position == first)
{
removeFirst();
}
else
{
previous.next = position.next; //removing
}
//stepping back
//this also means that remove() cannot be called twice in a row.
position = previous;
}
}
// Sets the last traversed element to a different value.
public void set(Object element)
{
if (position == null)
{
NoSuchElementException ex = new NoSuchElementException();
throw ex;
}
else
position.data = element;
}
} //end of LinkedListIterator class
} //end of LinkedList class