-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathMyLinkedList.java
More file actions
297 lines (264 loc) · 5.43 KB
/
Copy pathMyLinkedList.java
File metadata and controls
297 lines (264 loc) · 5.43 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
/**
*
*/
package com.allendowney.thinkdast;
import java.util.Arrays;
import java.util.Collection;
import java.util.Iterator;
import java.util.List;
import java.util.ListIterator;
/**
* @author downey
* @param <E>
*
*/
public class MyLinkedList<E> implements List<E> {
/**
* Node is identical to ListNode from the example, but parameterized with T
*
* @author downey
*
*/
private class Node {
public E data;
public Node next;
public Node(E data) {
this.data = data;
this.next = null;
}
@SuppressWarnings("unused")
public Node(E data, Node next) {
this.data = data;
this.next = next;
}
public String toString() {
return "Node(" + data.toString() + ")";
}
}
private int size; // keeps track of the number of elements
private Node head; // reference to the first node
/**
*
*/
public MyLinkedList() {
head = null;
size = 0;
}
/**
* @param args
*/
public static void main(String[] args) {
// run a few simple tests
List<Integer> mll = new MyLinkedList<Integer>();
mll.add(1);
mll.add(2);
mll.add(3);
System.out.println(Arrays.toString(mll.toArray()) + " size = " + mll.size());
mll.remove(new Integer(2));
System.out.println(Arrays.toString(mll.toArray()) + " size = " + mll.size());
}
@Override
public boolean add(E element) {
if (head == null) {
head = new Node(element);
} else {
Node node = head;
// loop until the last node
for ( ; node.next != null; node = node.next) {}
node.next = new Node(element);
}
size++;
return true;
}
@Override
public void add(int index, E element) {
//TODO: FILL THIS IN!
if (index == 0){
head = new Node(element, head);
} else {
Node node = getNode(index-1);
node.next = new Node(element, node.next);
}
size++;
}
@Override
public boolean addAll(Collection<? extends E> collection) {
boolean flag = true;
for (E element: collection) {
flag &= add(element);
}
return flag;
}
@Override
public boolean addAll(int index, Collection<? extends E> collection) {
throw new UnsupportedOperationException();
}
@Override
public void clear() {
head = null;
size = 0;
}
@Override
public boolean contains(Object obj) {
return indexOf(obj) != -1;
}
@Override
public boolean containsAll(Collection<?> collection) {
for (Object obj: collection) {
if (!contains(obj)) {
return false;
}
}
return true;
}
@Override
public E get(int index) {
Node node = getNode(index);
return node.data;
}
/** Returns the node at the given index.
* @param index
* @return
*/
private Node getNode(int index) {
if (index < 0 || index >= size) {
throw new IndexOutOfBoundsException();
}
Node node = head;
for (int i=0; i<index; i++) {
node = node.next;
}
return node;
}
@Override
public int indexOf(Object target) {
//TODO: FILL THIS IN!
Node node = head;
for (int i=0; i<size; i++){
if (equals(target, node.data)) {
return i;
}
node= node.next;
}
return -1;
}
/** Checks whether an element of the array is the target.
*
* Handles the special case that the target is null.
*
* @param target
* @param object
*/
private boolean equals(Object target, Object element) {
if (target == null) {
return element == null;
} else {
return target.equals(element);
}
}
@Override
public boolean isEmpty() {
return size == 0;
}
@Override
public Iterator<E> iterator() {
E[] array = (E[]) toArray();
return Arrays.asList(array).iterator();
}
@Override
public int lastIndexOf(Object target) {
Node node = head;
int index = -1;
for (int i=0; i<size; i++) {
if (equals(target, node.data)) {
index = i;
}
node = node.next;
}
return index;
}
@Override
public ListIterator<E> listIterator() {
return null;
}
@Override
public ListIterator<E> listIterator(int index) {
return null;
}
@Override
public boolean remove(Object obj) {
int index = indexOf(obj);
if (index == -1) {
return false;
}
remove(index);
return true;
}
@Override
public E remove(int index) {
//TODO: FILL THIS IN!
E element = get(index);
if (index == 0) {
head = head.next;
} else {
Node node = getNode(index-1);
node.next = node.next.next;
}
size--;
return element;
}
@Override
public boolean removeAll(Collection<?> collection) {
boolean flag = true;
for (Object obj: collection) {
flag &= remove(obj);
}
return flag;
}
@Override
public boolean retainAll(Collection<?> collection) {
throw new UnsupportedOperationException();
}
@Override
public E set(int index, E element) {
Node node = getNode(index);
E old = node.data;
node.data = element;
return old;
}
@Override
public int size() {
return size;
}
@Override
public List<E> subList(int fromIndex, int toIndex) {
if (fromIndex < 0 || toIndex >= size || fromIndex > toIndex) {
throw new IndexOutOfBoundsException();
}
// TODO: classify this and improve it.
int i = 0;
MyLinkedList<E> list = new MyLinkedList<E>();
for (Node node=head; node != null; node = node.next) {
if (i >= fromIndex && i <= toIndex) {
list.add(node.data);
}
i++;
}
return list;
}
@Override
public Object[] toArray() {
Object[] array = new Object[size];
int i = 0;
for (Node node=head; node != null; node = node.next) {
// System.out.println(node);
array[i] = node.data;
i++;
}
return array;
}
@Override
public <T> T[] toArray(T[] a) {
throw new UnsupportedOperationException();
}
}