-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathMyLinkedList.java
More file actions
218 lines (191 loc) · 5.93 KB
/
MyLinkedList.java
File metadata and controls
218 lines (191 loc) · 5.93 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
package module5.datastructures.birdsurvey;
import java.util.AbstractSequentialList;
import java.util.Collection;
import java.util.ListIterator;
import java.util.NoSuchElementException;
/**
* A custom implementation of a doubly-linked list, based on Java's
* AbstractSequentialList.
*
* @author Mae Morella
*
* @implNote This class differs from Java's LinkedList in that it does not implement
* Deque, so its functionality as a Stack or Queue is relatively limited.
* Due to its hasty implementation, it is undoubtedly less efficient
* and less functional than the built-in LinkedList.
*/
public class MyLinkedList<E> extends AbstractSequentialList<E> implements Cloneable, java.io.Serializable {
private static final long serialVersionUID = 8794565002792734402L;
private int size = 0;
private Node<E> start;
private Node<E> end;
class Node<T> {
public T data;
public Node<T> next;
public Node<T> prev;
public Node(Node<T> prev, T data, Node<T> next) {
this.data = data;
this.next = next;
this.prev = prev;
}
/** Create a new node, and attach it between this node and the one before it */
public void linkBefore(T data) {
Node<T> before = this.prev; // nullable
Node<T> newNode = new Node<>(before, data, this);
this.prev = newNode;
if (before != null) {
before.next = newNode;
}
}
/** Create a new node, and attach it between this node and the one after it */
public void linkAfter(T data) {
Node<T> after = this.next; // nullable
Node<T> newNode = new Node<>(this, data, after);
this.next = newNode;
if (after != null) {
after.prev = newNode;
}
}
/** Remove this node by linking together its neighbors */
public T unlink() {
Node<T> before = this.prev;
Node<T> after = this.next;
before.next = after;
after.next = before;
return this.data;
}
}
/** Instantiate a new empty list */
public MyLinkedList() {}
/** Instantiate the list with the elements of another Collection */
public MyLinkedList(Collection<? extends E> collection) {
addAll(collection);
}
@Override
public int size() {
return size;
}
@Override
public boolean add(E data) {
return addLast(data);
}
/** Add a new element to the end of the List */
public boolean addLast(E data) {
Node<E> endNode = end;
Node<E> newNode = new Node<>(endNode, data, null);
end = newNode;
if (endNode == null) {
start = newNode;
} else {
endNode.next = newNode;
}
size++;
return true;
}
/** Add a new element to the end of the List */
public void addFirst(E data) {
Node<E> startNode = start;
Node<E> newNode = new Node<>(null, data, startNode);
if (startNode == null) {
start = end = newNode;
} else {
startNode.prev = newNode;
}
start = newNode;
size++;
}
@Override
public ListIterator<E> listIterator(int index) {
return new LinkedListIterator(index);
}
private class LinkedListIterator implements ListIterator<E> {
private Node<E> next = start;
private Node<E> lastAccessed;
private int nextIndex = 0;
public LinkedListIterator(int index) {
if (index < 0 || size < index) {
throw new IndexOutOfBoundsException(index);
}
while (nextIndex < index) {
next();
}
}
@Override
public int nextIndex() {
return nextIndex;
}
@Override
public int previousIndex() {
return nextIndex - 1;
}
@Override
public boolean hasNext() {
return (nextIndex < size);
}
@Override
public boolean hasPrevious() {
return (0 <= previousIndex());
}
@Override
public E next() {
if (hasNext()) {
lastAccessed = next;
next = next.next; // nullable
nextIndex++;
return lastAccessed.data;
} else {
throw new NoSuchElementException();
}
}
@Override
public E previous() {
if (hasPrevious()) {
Node<E> prev = next.prev;
lastAccessed = prev;
next = prev; // nullable
nextIndex--;
return lastAccessed.data;
} else {
throw new NoSuchElementException();
}
}
@Override
public void add(E data) {
lastAccessed = null;
if (next == null) {
addLast(data);
} else {
next.linkBefore(data);
size++;
}
nextIndex++; // shift next further forward in the array
}
@Override
public void remove() {
if (lastAccessed != null) {
lastAccessed.unlink();
lastAccessed = null;
size--;
} else {
throw new IllegalStateException();
}
}
@Override
public void set(E data) {
if (lastAccessed != null) {
lastAccessed.data = data;
} else {
throw new IllegalStateException();
}
}
}
/** @return A shallow copy of this linked list */
@Override
public MyLinkedList<E> clone() {
MyLinkedList<E> clone = new MyLinkedList<>(this);
for (E data : this) {
clone.add(data);
}
return clone;
}
}