-
Notifications
You must be signed in to change notification settings - Fork 1
/
Chapter 24
385 lines (316 loc) · 9.99 KB
/
Chapter 24
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
import java.util.*;
public class Lists {
public static void main(String[] args) {
MyLinkedList<String> names = new MyLinkedList<>();
names.add("David");
names.add("Nam");
names.add("Andrew");
names.add("Aleah");
names.add("Jay");
names.add("Kelly");
names.add("Kevin");
names.add("Percy");
names.add("Jhess");
names.add("Jesus"); //adding 10 names to linked list
System.out.println("Tests for all methods in MyLinkedList:" + names);
System.out.println("getFirst(): " + names.getFirst()); //tests getFirst()
System.out.println("getLast(): " + names.getLast()); //tests getLast()
names.addFirst("Steven"); //tests addFirst(E e)
System.out.println("addFirst('Steven'): " + names);
names.addLast("Tony"); //tests addLastE e)
System.out.println("addFirst('Tony'): " + names);
names.add(2, "Tiana"); //tests add(index int, E e)
System.out.println("add(2, 'Tiana'): " + names);
System.out.println("removeFirst(): " + names.removeFirst()); //tests removeFirst()
System.out.println("removeLast(): " + names.removeLast()); //tests removeLast()
System.out.println("remove(5): " + names.remove(5)); //tests remove()
System.out.println("contains('Kevin'): " + names.contains("Kevin")); //tests contains()
System.out.println("get(8): " + names.get(8)); //tests get()
System.out.println("indexOf('Kelly'): " + names.indexOf("Kelly")); //tests indexOf()
System.out.println("lastIndexOf('Percy'): " + names.lastIndexOf("Percy")); //tests lastIndexOf()
names.set(5, "Beyonce"); //tests set()
System.out.println("set(5, Beyonce): " + names);
System.out.println("size(): " + names.size()); //tests size()
names.clear(); //tests clear()
System.out.println("clear(): " + names);
}
}
class MyLinkedList<E> implements MyList<E> {
private Node<E> head; //first element of the linked structure
private Node<E> tail; //last element of the linked structure
int size; //integer value of amount of elements in the linked structure
public MyLinkedList() { //creates an empty list
}
public MyLinkedList(E[] objects) {
for (int i = 0; i < objects.length; i++) {
add(objects[i]);
}
}
public E getFirst() {
if (size == 0) {
return null; //the list is empty
} else {
return head.element;
}
}
public E getLast() {
if (size == 0) {
return null;
} else {
return tail.element;
}
}
public void addFirst(E e) {
Node<E> newNode = new Node<>(e);
newNode.next = head;
head = newNode;
size++;
if (tail == null) {
tail = head;
}
}
public void addLast(E e) {
Node<E> newNode = new Node<>(e);
if (tail == null) {
head = tail = newNode;
} else {
tail.next = newNode;
tail = newNode;
}
size++;
}
@Override
public void add(int index, E e) {
if (index == 0) {
addFirst(e);
} else if (index >= size) {
addLast(e);
} else {
Node<E> current = head;
for (int i = 1; i < index; i++) {
current = current.next;
}
Node<E> temp = current.next;
current.next = new Node<>(e);
(current.next).next = temp;
size++;
}
}
public E removeFirst() {
if (size == 0) {
return null;
} else {
Node<E> temp = head;
head = head.next;
size--;
if (head == null) {
tail = null;
}
return temp.element;
}
}
public E removeLast() {
if (size == 0) {
return null;
} else if (size == 1) {
Node<E> temp = head;
head = tail = null;
size = 0;
return temp.element;
} else {
Node<E> current = head;
for (int i = 0; i < size - 2; i++) {
current = current.next;
}
Node<E> temp = tail;
tail = current;
tail.next = null;
size--;
return temp.element;
}
}
public E remove(int index) {
if (index < 0 || index >= size) {
return null;
} else if (index == 0) {
return removeFirst();
} else if (index == size - 1) {
return removeLast();
} else {
Node<E> previous = head;
for (int i = 1; i < index; i++) {
previous = previous.next;
}
Node<E> current = previous.next;
previous.next = current.next;
size--;
return current.element;
}
}
@Override
public String toString() {
StringBuilder result = new StringBuilder("[");
Node<E> current = head;
for (int i = 0; i < size; i++) {
result.append(current.element);
current = current.next;
if (current != null) {
result.append(", ");
} else {
result.append("]");
}
}
return result.toString();
}
@Override
public void clear() {
size = 0;
head = tail = null;
}
@Override
public boolean contains(Object e) {
Node<E> current = head;
for (int i = 0; i < size; i++) {
if (current.element.equals(e)) { //if current element equals to object, return true
return true;
}
current = current.next; //else, keep iterating and repeat
}
return false; //if none of the elements equal object, return false
}
@Override
public E get(int index) {
Node<E> current = head;
for (int i = 0; i < size; i++) {
if (i == index) { //iterating, if iteration equals index, stop and return index
return current.element;
}
current = current.next; //if index is not specified index, keep iterating and repeat
}
return null; //index does not exist
}
@Override
public int indexOf(Object e) {
Node<E> current = head;
for (int i = 0; i < size; i++) {
if (current.element.equals(e)) {
return i; //returns order/index
}
current = current.next;
}
return -1; //no match (weird how the book had return 0 initially...)
}
@Override
public int lastIndexOf(E e) { //returns the index f th head matching element in the list
Node<E> current = head;
for (int i = 0; i < size; i++) {
if (current.element.equals(e)) {
return i;
}
current = current.next;
}
return -1;
}
@Override
public E set(int index, E e) {
Node<E> current = head;
for (int i = 0; i < size; i++) {
if (i == index) {
current.element = e;
}
current = current.next;
}
return null;
}
@Override
public int size() {
return size;
}
@Override
public Iterator<E> iterator() {
return new LinkedListIterator();
}
private class LinkedListIterator implements Iterator<E> {
private Node<E> current = head;
@Override
public boolean hasNext() {
return (current != null);
}
@Override
public E next() {
E e = current.element;
current = current.next;
return e;
}
@Override
public void remove() {
//finish
}
}
private static class Node<E> {
E element;
Node<E> next;
public Node(E element) {
this.element = element;
}
}
}
interface MyList<E> extends Collection<E> {
//Add a new element at the specified index in this list.
public void add(int index, E e);
//Return the element from this list at the specified index.
public E get(int index);
//Return the index of the first matching element in this list. Return -1 if no match.
public int indexOf(Object e);
//Return the index of the last matching element in this list Return -1 if no match.
public int lastIndexOf(E e);
//Remove the element at the specified position in this list. Shift any subsequent elements to the left. Return the element that was removed from the list.
public E remove(int index);
//Replace the element at the specified position in this list with the specified element and returns the new set.
public E set(int index, E e);
@Override
//Add a new element at the end of this list
public default boolean add(E e) {
add(size(), e);
return true;
}
@Override
//Return true if this list contains no elements
public default boolean isEmpty() {
return size() == 0;
}
@Override
//Remove the first occurrence of the element e from this list. Shift any subsequent elements to the left. Return true if the element is removed.
public default boolean remove(Object e) {
if (indexOf(e) >= 0) {
remove(indexOf(e));
return true;
} else {
return false;
}
}
@Override
public default boolean containsAll(Collection<?> c) {
return true;
}
@Override
public default boolean addAll(Collection<? extends E> c) {
return true;
}
@Override
public default boolean removeAll(Collection<?> c) {
return true;
}
@Override
public default boolean retainAll(Collection<?> c) {
return true;
}
@Override
public default Object[] toArray() {
return null;
}
@Override
public default <T> T[] toArray(T[] array) {
return null;
}
}