-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathImplementingLists.java
More file actions
406 lines (337 loc) · 10.4 KB
/
ImplementingLists.java
File metadata and controls
406 lines (337 loc) · 10.4 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
package sample;
// Sukhamrit Singh
// Implementing Lists
/*
This is a program that implements a revised MyLinkedList class
after I have included all the code needed to fill in and complete
all the methods that were omitted. Next, I wrote a (main) driver
program that initializes a linked list with 10 names, and then completely
tests every one of its methods of ensure that the class meets all its
requirements.
*/
// Import the necessary libraries
import java.util.Collection;
public class ImplementingLists<E> implements MyList<E> {
public static void main(String[] args) {
// Create a list for strings
ImplementingLists<String> list = new ImplementingLists<>();
// Add elements to the list and printing them out
list.add("Sukhamrit");
System.out.println("Added Sukhamrit to the list ---- 1: "
+ list);
list.add(0, "Alex");
System.out.println("Added Alex to the list ---- 2: " + list);
list.add("Bob");
System.out.println("Added Bob to the list ---- 3: " + list);
list.addLast("Jonnathan");
System.out.println("Added Jonnathan to the list ---- 4: "
+ list);
list.add(2, "Nathan");
System.out.println("Added Nathan to the list ---- 5: " + list);
list.add(5, "Claire");
System.out.println("Added Claire to the list ---- 6: " + list);
list.add(0, "Angela");
System.out.println("Added Angela to the list ---- 7: " + list);
list.add(0, "Diego");
System.out.println("Added Diego to the list ---- 8: " + list);
list.add(0, "Kyle");
System.out.println("Added Kyle to the list ---- 9: " + list);
list.add(0, "Daniel");
System.out.println("Added Daniel to the list ---- 10: " + list);
// Remove elements from the list
list.remove(0);
System.out.println("Removed Daniel from the list ---- 11: " + list);
list.remove(2);
System.out.println("Removed Angela from the list ---- 12: " + list);
list.remove(list.size() - 1);
System.out.print("Removed Claire from the list ---- 13: " + list +
"\nThe final list it ---- 14: ");
// using iterator in for-loop to traverse element in LinkedList
for (String s: list)
System.out.print(s.toUpperCase() + " ");
list.clear();
System.out.println("\nThe list size after clearing is: "
+ list.size());
}
private Node<E> head, tail;
private int size = 0; // Number of elements in the list
/** Create an empty list */
public ImplementingLists() {
}
/** Create a list from an array of objects */
public ImplementingLists(E[] objects) {
for (int i = 0; i < objects.length; i++)
add(objects[i]);
}
/** Return the head element in the list */
public E getFirst() {
if (size == 0) {
return null;
}
else {
return head.element;
}
}
/** Return the last element in the list */
public E getLast() {
if (size == 0) {
return null;
}
else {
return tail.element;
}
}
/** Add an element to the beginning of the list */
public void addFirst(E e) {
// Create a node object
Node<E> node = new Node<>(e);
// assign node to head
node.next = head;
head = node;
size++;
// the new node is the only node in list
if (tail == null)
tail = head;
}
/** Add an element to the end of the list */
public void addLast(E e) {
Node<E> node = new Node<>(e);
if (tail == null) {
head = tail = node;
}
else {
// Link the new with the last node
tail.next = node;
tail = node;
}
size++;
}
@Override /** Add a new element at the specified index
* in this list. The index of the head element is 0 */
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++;
}
}
/** Remove the head node and
* return the object that is contained in the removed node. */
public E removeFirst() {
if (size == 0) {
return null;
}
else {
E temp = head.element;
head = head.next;
size--;
if (head == null) {
tail = null;
}
return temp;
}
}
/** Remove the last node and
* return the object that is contained in the removed node. */
public E removeLast() {
if (size == 0) {
return null;
}
else if (size == 1) {
E temp = head.element;
head = tail = null;
size = 0;
return temp;
}
else {
Node<E> current = head;
for (int i = 0; i < size - 2; i++) {
current = current.next;
}
E temp = tail.element;
tail = current;
tail.next = null;
size--;
return temp;
}
}
@Override /** Remove the element at the specified position in this
* list. Return the element that was removed from the list. */
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 /** Override toString() to return elements in the list */
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 /** Clear the list */
public void clear() {
size = 0;
head = tail = null;
}
@Override /** Return true if this list contains the element e */
public boolean contains(Object e) {
// Left as an exercise
return true;
}
@Override /** Return the element at the specified index */
public E get(int index) {
// Left as an exercise
return null;
}
@Override /** Return the index of the head matching element in
* this list. Return -1 if no match. */
public int indexOf(Object e) {
// Left as an exercise
return 0;
}
@Override /** Return the index of the last matching element in
* this list. Return -1 if no match. */
public int lastIndexOf(E e) {
// Left as an exercise
return 0;
}
@Override /** Replace the element at the specified position
* in this list with the specified element. */
public E set(int index, E e) {
// Left as an exercise
return null;
}
@Override /** Override iterator() defined in Iterable */
public java.util.Iterator<E> iterator() {
return new LinkedListIterator();
}
private class LinkedListIterator
implements java.util.Iterator<E> {
private Node<E> current = head; // Current index
@Override
public boolean hasNext() {
return (current != null);
}
@Override
public E next() {
E e = current.element;
current = current.next;
return e;
}
@Override
public void remove() {
}
}
private static class Node<E> {
E element;
Node<E> next;
public Node(E element) {
this.element = element;
}
}
@Override /** Return the number of elements in this list */
public int size() {
return size;
}
}
// Creating a new interface MyList for the LinkedList
interface MyList<E> extends Collection<E> {
// Add new element in specific index
public void add(int index, E e);
// Return an element with specific index
public E get(int index);
//Return the index of first matching element in list
// But if can not find any match, returns -1
public int indexOf(Object e);
//Return the index of last matching element in list
// But if can not find any match, returns -1
public int lastIndexOf(E e);
// Remove element with specific index,
// return element removed from list
public E remove(int index);
// Return element with specific index for the list
public E set(int index, E e);
// Add new element at end of list
@Override
public default boolean add(E e) {
add(size(), e);
return true;
}
//Return true if list contains no elements */
@Override
public default boolean isEmpty() {
return size() == 0;
}
// Remove first element
// Move next elements to left in list
@Override
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;
}
}