1 |
tim |
1.1 |
/* |
2 |
dl |
1.6 |
* %W% %E% |
3 |
tim |
1.1 |
* |
4 |
jsr166 |
1.23 |
* Copyright 2005 Sun Microsystems, Inc. All rights reserved. |
5 |
tim |
1.1 |
* SUN PROPRIETARY/CONFIDENTIAL. Use is subject to license terms. |
6 |
|
|
*/ |
7 |
|
|
|
8 |
jsr166 |
1.24 |
package java.util; |
9 |
tim |
1.1 |
|
10 |
|
|
/** |
11 |
|
|
* Linked list implementation of the <tt>List</tt> interface. Implements all |
12 |
|
|
* optional list operations, and permits all elements (including |
13 |
|
|
* <tt>null</tt>). In addition to implementing the <tt>List</tt> interface, |
14 |
|
|
* the <tt>LinkedList</tt> class provides uniformly named methods to |
15 |
|
|
* <tt>get</tt>, <tt>remove</tt> and <tt>insert</tt> an element at the |
16 |
|
|
* beginning and end of the list. These operations allow linked lists to be |
17 |
dl |
1.19 |
* used as a stack, {@linkplain Queue queue}, or {@linkplain Deque |
18 |
|
|
* double-ended queue}. <p> |
19 |
tim |
1.1 |
* |
20 |
dl |
1.17 |
* The class implements the <tt>Deque</tt> interface, providing |
21 |
dl |
1.3 |
* first-in-first-out queue operations for <tt>add</tt>, |
22 |
dl |
1.21 |
* <tt>poll</tt>, along with other stack and deque operations.<p> |
23 |
tim |
1.1 |
* |
24 |
|
|
* All of the operations perform as could be expected for a doubly-linked |
25 |
|
|
* list. Operations that index into the list will traverse the list from |
26 |
dl |
1.8 |
* the beginning or the end, whichever is closer to the specified index.<p> |
27 |
tim |
1.1 |
* |
28 |
|
|
* <b>Note that this implementation is not synchronized.</b> If multiple |
29 |
|
|
* threads access a list concurrently, and at least one of the threads |
30 |
|
|
* modifies the list structurally, it <i>must</i> be synchronized |
31 |
|
|
* externally. (A structural modification is any operation that adds or |
32 |
|
|
* deletes one or more elements; merely setting the value of an element is not |
33 |
|
|
* a structural modification.) This is typically accomplished by |
34 |
|
|
* synchronizing on some object that naturally encapsulates the list. If no |
35 |
|
|
* such object exists, the list should be "wrapped" using the |
36 |
|
|
* Collections.synchronizedList method. This is best done at creation time, |
37 |
|
|
* to prevent accidental unsynchronized access to the list: <pre> |
38 |
|
|
* List list = Collections.synchronizedList(new LinkedList(...)); |
39 |
jsr166 |
1.24 |
* </pre> |
40 |
tim |
1.1 |
* |
41 |
jsr166 |
1.24 |
* <p>The iterators returned by this class's <tt>iterator</tt> and |
42 |
tim |
1.1 |
* <tt>listIterator</tt> methods are <i>fail-fast</i>: if the list is |
43 |
jsr166 |
1.24 |
* structurally modified at any time after the iterator is created, in |
44 |
|
|
* any way except through the Iterator's own <tt>remove</tt> or |
45 |
|
|
* <tt>add</tt> methods, the iterator will throw a {@link |
46 |
|
|
* ConcurrentModificationException}. Thus, in the face of concurrent |
47 |
|
|
* modification, the iterator fails quickly and cleanly, rather than |
48 |
|
|
* risking arbitrary, non-deterministic behavior at an undetermined |
49 |
|
|
* time in the future. |
50 |
tim |
1.1 |
* |
51 |
|
|
* <p>Note that the fail-fast behavior of an iterator cannot be guaranteed |
52 |
|
|
* as it is, generally speaking, impossible to make any hard guarantees in the |
53 |
|
|
* presence of unsynchronized concurrent modification. Fail-fast iterators |
54 |
dl |
1.3 |
* throw <tt>ConcurrentModificationException</tt> on a best-effort basis. |
55 |
tim |
1.1 |
* Therefore, it would be wrong to write a program that depended on this |
56 |
|
|
* exception for its correctness: <i>the fail-fast behavior of iterators |
57 |
jsr166 |
1.24 |
* should be used only to detect bugs.</i> |
58 |
dl |
1.3 |
* |
59 |
jsr166 |
1.24 |
* <p>This class is a member of the |
60 |
dl |
1.3 |
* <a href="{@docRoot}/../guide/collections/index.html"> |
61 |
|
|
* Java Collections Framework</a>. |
62 |
tim |
1.1 |
* |
63 |
|
|
* @author Josh Bloch |
64 |
dl |
1.6 |
* @version %I%, %G% |
65 |
jsr166 |
1.14 |
* @see List |
66 |
|
|
* @see ArrayList |
67 |
|
|
* @see Vector |
68 |
|
|
* @see Collections#synchronizedList(List) |
69 |
tim |
1.1 |
* @since 1.2 |
70 |
dl |
1.10 |
* @param <E> the type of elements held in this collection |
71 |
tim |
1.1 |
*/ |
72 |
|
|
|
73 |
dl |
1.3 |
public class LinkedList<E> |
74 |
|
|
extends AbstractSequentialList<E> |
75 |
dl |
1.17 |
implements List<E>, Deque<E>, Cloneable, java.io.Serializable |
76 |
tim |
1.1 |
{ |
77 |
dl |
1.3 |
private transient Entry<E> header = new Entry<E>(null, null, null); |
78 |
tim |
1.1 |
private transient int size = 0; |
79 |
|
|
|
80 |
|
|
/** |
81 |
|
|
* Constructs an empty list. |
82 |
|
|
*/ |
83 |
|
|
public LinkedList() { |
84 |
|
|
header.next = header.previous = header; |
85 |
|
|
} |
86 |
|
|
|
87 |
|
|
/** |
88 |
|
|
* Constructs a list containing the elements of the specified |
89 |
|
|
* collection, in the order they are returned by the collection's |
90 |
|
|
* iterator. |
91 |
|
|
* |
92 |
jsr166 |
1.27 |
* @param c the collection whose elements are to be placed into this list |
93 |
|
|
* @throws NullPointerException if the specified collection is null |
94 |
tim |
1.1 |
*/ |
95 |
jsr166 |
1.14 |
public LinkedList(Collection<? extends E> c) { |
96 |
|
|
this(); |
97 |
|
|
addAll(c); |
98 |
|
|
} |
99 |
tim |
1.1 |
|
100 |
|
|
/** |
101 |
|
|
* Returns the first element in this list. |
102 |
|
|
* |
103 |
jsr166 |
1.27 |
* @return the first element in this list |
104 |
|
|
* @throws NoSuchElementException if this list is empty |
105 |
tim |
1.1 |
*/ |
106 |
dl |
1.3 |
public E getFirst() { |
107 |
jsr166 |
1.14 |
if (size==0) |
108 |
|
|
throw new NoSuchElementException(); |
109 |
tim |
1.1 |
|
110 |
jsr166 |
1.14 |
return header.next.element; |
111 |
tim |
1.1 |
} |
112 |
|
|
|
113 |
|
|
/** |
114 |
|
|
* Returns the last element in this list. |
115 |
|
|
* |
116 |
jsr166 |
1.27 |
* @return the last element in this list |
117 |
|
|
* @throws NoSuchElementException if this list is empty |
118 |
tim |
1.1 |
*/ |
119 |
dl |
1.3 |
public E getLast() { |
120 |
jsr166 |
1.14 |
if (size==0) |
121 |
|
|
throw new NoSuchElementException(); |
122 |
tim |
1.1 |
|
123 |
jsr166 |
1.14 |
return header.previous.element; |
124 |
tim |
1.1 |
} |
125 |
|
|
|
126 |
|
|
/** |
127 |
|
|
* Removes and returns the first element from this list. |
128 |
|
|
* |
129 |
jsr166 |
1.27 |
* @return the first element from this list |
130 |
|
|
* @throws NoSuchElementException if this list is empty |
131 |
tim |
1.1 |
*/ |
132 |
dl |
1.3 |
public E removeFirst() { |
133 |
jsr166 |
1.14 |
return remove(header.next); |
134 |
tim |
1.1 |
} |
135 |
|
|
|
136 |
|
|
/** |
137 |
|
|
* Removes and returns the last element from this list. |
138 |
|
|
* |
139 |
jsr166 |
1.27 |
* @return the last element from this list |
140 |
|
|
* @throws NoSuchElementException if this list is empty |
141 |
tim |
1.1 |
*/ |
142 |
dl |
1.3 |
public E removeLast() { |
143 |
jsr166 |
1.14 |
return remove(header.previous); |
144 |
tim |
1.1 |
} |
145 |
|
|
|
146 |
|
|
/** |
147 |
|
|
* Inserts the given element at the beginning of this list. |
148 |
dl |
1.3 |
* |
149 |
jsr166 |
1.27 |
* @param e the element to be inserted at the beginning of this list |
150 |
tim |
1.1 |
*/ |
151 |
jsr166 |
1.25 |
public void addFirst(E e) { |
152 |
|
|
addBefore(e, header.next); |
153 |
tim |
1.1 |
} |
154 |
|
|
|
155 |
|
|
/** |
156 |
jsr166 |
1.24 |
* Appends the given element to the end of this list. (Identical in |
157 |
tim |
1.1 |
* function to the <tt>add</tt> method; included only for consistency.) |
158 |
dl |
1.3 |
* |
159 |
jsr166 |
1.27 |
* @param e the element to be inserted at the end of this list |
160 |
tim |
1.1 |
*/ |
161 |
jsr166 |
1.25 |
public void addLast(E e) { |
162 |
|
|
addBefore(e, header); |
163 |
tim |
1.1 |
} |
164 |
|
|
|
165 |
|
|
/** |
166 |
|
|
* Returns <tt>true</tt> if this list contains the specified element. |
167 |
|
|
* More formally, returns <tt>true</tt> if and only if this list contains |
168 |
jsr166 |
1.28 |
* at least one element <tt>e</tt> such that |
169 |
|
|
* <tt>(o==null ? e==null : o.equals(e))</tt>. |
170 |
tim |
1.1 |
* |
171 |
jsr166 |
1.27 |
* @param o element whose presence in this list is to be tested |
172 |
|
|
* @return <tt>true</tt> if this list contains the specified element |
173 |
tim |
1.1 |
*/ |
174 |
|
|
public boolean contains(Object o) { |
175 |
|
|
return indexOf(o) != -1; |
176 |
|
|
} |
177 |
|
|
|
178 |
|
|
/** |
179 |
|
|
* Returns the number of elements in this list. |
180 |
|
|
* |
181 |
jsr166 |
1.27 |
* @return the number of elements in this list |
182 |
tim |
1.1 |
*/ |
183 |
|
|
public int size() { |
184 |
jsr166 |
1.14 |
return size; |
185 |
tim |
1.1 |
} |
186 |
|
|
|
187 |
|
|
/** |
188 |
jsr166 |
1.24 |
* Appends the specified element to the end of this list. |
189 |
tim |
1.1 |
* |
190 |
jsr166 |
1.27 |
* @param e element to be appended to this list |
191 |
|
|
* @return <tt>true</tt> (as per the spec for {@link Collection#add}) |
192 |
tim |
1.1 |
*/ |
193 |
jsr166 |
1.26 |
public boolean add(E e) { |
194 |
|
|
addBefore(e, header); |
195 |
tim |
1.1 |
return true; |
196 |
|
|
} |
197 |
|
|
|
198 |
|
|
/** |
199 |
jsr166 |
1.28 |
* Removes the first occurrence of the specified element from this list, |
200 |
|
|
* if it is present. If this list does not contain the element, it is |
201 |
|
|
* unchanged. More formally, removes the element with the lowest index |
202 |
|
|
* <tt>i</tt> such that |
203 |
|
|
* <tt>(o==null ? get(i)==null : o.equals(get(i)))</tt> |
204 |
|
|
* (if such an element exists). Returns <tt>true</tt> if this list |
205 |
|
|
* contained the specified element (or equivalently, if this list |
206 |
|
|
* changed as a result of the call). |
207 |
tim |
1.1 |
* |
208 |
jsr166 |
1.27 |
* @param o element to be removed from this list, if present |
209 |
jsr166 |
1.28 |
* @return <tt>true</tt> if this list contained the specified element |
210 |
tim |
1.1 |
*/ |
211 |
|
|
public boolean remove(Object o) { |
212 |
|
|
if (o==null) { |
213 |
dl |
1.3 |
for (Entry<E> e = header.next; e != header; e = e.next) { |
214 |
tim |
1.1 |
if (e.element==null) { |
215 |
|
|
remove(e); |
216 |
|
|
return true; |
217 |
|
|
} |
218 |
|
|
} |
219 |
|
|
} else { |
220 |
dl |
1.3 |
for (Entry<E> e = header.next; e != header; e = e.next) { |
221 |
tim |
1.1 |
if (o.equals(e.element)) { |
222 |
|
|
remove(e); |
223 |
|
|
return true; |
224 |
|
|
} |
225 |
|
|
} |
226 |
|
|
} |
227 |
|
|
return false; |
228 |
|
|
} |
229 |
|
|
|
230 |
|
|
/** |
231 |
jsr166 |
1.24 |
* Appends all of the elements in the specified collection to the end of |
232 |
tim |
1.1 |
* this list, in the order that they are returned by the specified |
233 |
|
|
* collection's iterator. The behavior of this operation is undefined if |
234 |
|
|
* the specified collection is modified while the operation is in |
235 |
|
|
* progress. (This implies that the behavior of this call is undefined if |
236 |
|
|
* the specified Collection is this list, and this list is nonempty.) |
237 |
|
|
* |
238 |
jsr166 |
1.27 |
* @param c the elements to be inserted into this list |
239 |
|
|
* @return <tt>true</tt> if this list changed as a result of the call |
240 |
|
|
* @throws NullPointerException if the specified collection is null |
241 |
tim |
1.1 |
*/ |
242 |
dl |
1.3 |
public boolean addAll(Collection<? extends E> c) { |
243 |
tim |
1.1 |
return addAll(size, c); |
244 |
|
|
} |
245 |
|
|
|
246 |
|
|
/** |
247 |
|
|
* Inserts all of the elements in the specified collection into this |
248 |
|
|
* list, starting at the specified position. Shifts the element |
249 |
|
|
* currently at that position (if any) and any subsequent elements to |
250 |
|
|
* the right (increases their indices). The new elements will appear |
251 |
|
|
* in the list in the order that they are returned by the |
252 |
|
|
* specified collection's iterator. |
253 |
|
|
* |
254 |
jsr166 |
1.27 |
* @param index index at which to insert the first element |
255 |
|
|
* from the specified collection |
256 |
|
|
* @param c elements to be inserted into this list |
257 |
|
|
* @return <tt>true</tt> if this list changed as a result of the call |
258 |
jsr166 |
1.28 |
* @throws IndexOutOfBoundsException {@inheritDoc} |
259 |
jsr166 |
1.27 |
* @throws NullPointerException if the specified collection is null |
260 |
tim |
1.1 |
*/ |
261 |
dl |
1.3 |
public boolean addAll(int index, Collection<? extends E> c) { |
262 |
dl |
1.4 |
if (index < 0 || index > size) |
263 |
dl |
1.3 |
throw new IndexOutOfBoundsException("Index: "+index+ |
264 |
|
|
", Size: "+size); |
265 |
|
|
Object[] a = c.toArray(); |
266 |
|
|
int numNew = a.length; |
267 |
|
|
if (numNew==0) |
268 |
|
|
return false; |
269 |
jsr166 |
1.14 |
modCount++; |
270 |
dl |
1.3 |
|
271 |
|
|
Entry<E> successor = (index==size ? header : entry(index)); |
272 |
|
|
Entry<E> predecessor = successor.previous; |
273 |
jsr166 |
1.14 |
for (int i=0; i<numNew; i++) { |
274 |
dl |
1.3 |
Entry<E> e = new Entry<E>((E)a[i], successor, predecessor); |
275 |
tim |
1.1 |
predecessor.next = e; |
276 |
|
|
predecessor = e; |
277 |
|
|
} |
278 |
|
|
successor.previous = predecessor; |
279 |
|
|
|
280 |
|
|
size += numNew; |
281 |
|
|
return true; |
282 |
|
|
} |
283 |
|
|
|
284 |
|
|
/** |
285 |
|
|
* Removes all of the elements from this list. |
286 |
|
|
*/ |
287 |
|
|
public void clear() { |
288 |
jsr166 |
1.14 |
Entry<E> e = header.next; |
289 |
|
|
while (e != header) { |
290 |
|
|
Entry<E> next = e.next; |
291 |
|
|
e.next = e.previous = null; |
292 |
|
|
e.element = null; |
293 |
|
|
e = next; |
294 |
|
|
} |
295 |
tim |
1.1 |
header.next = header.previous = header; |
296 |
jozart |
1.12 |
size = 0; |
297 |
jsr166 |
1.14 |
modCount++; |
298 |
tim |
1.1 |
} |
299 |
|
|
|
300 |
|
|
|
301 |
|
|
// Positional Access Operations |
302 |
|
|
|
303 |
|
|
/** |
304 |
|
|
* Returns the element at the specified position in this list. |
305 |
|
|
* |
306 |
jsr166 |
1.28 |
* @param index index of the element to return |
307 |
jsr166 |
1.27 |
* @return the element at the specified position in this list |
308 |
jsr166 |
1.28 |
* @throws IndexOutOfBoundsException {@inheritDoc} |
309 |
tim |
1.1 |
*/ |
310 |
dl |
1.3 |
public E get(int index) { |
311 |
tim |
1.1 |
return entry(index).element; |
312 |
|
|
} |
313 |
|
|
|
314 |
|
|
/** |
315 |
|
|
* Replaces the element at the specified position in this list with the |
316 |
|
|
* specified element. |
317 |
|
|
* |
318 |
jsr166 |
1.28 |
* @param index index of the element to replace |
319 |
jsr166 |
1.27 |
* @param element element to be stored at the specified position |
320 |
|
|
* @return the element previously at the specified position |
321 |
jsr166 |
1.28 |
* @throws IndexOutOfBoundsException {@inheritDoc} |
322 |
tim |
1.1 |
*/ |
323 |
dl |
1.3 |
public E set(int index, E element) { |
324 |
|
|
Entry<E> e = entry(index); |
325 |
|
|
E oldVal = e.element; |
326 |
tim |
1.1 |
e.element = element; |
327 |
|
|
return oldVal; |
328 |
|
|
} |
329 |
|
|
|
330 |
|
|
/** |
331 |
|
|
* Inserts the specified element at the specified position in this list. |
332 |
|
|
* Shifts the element currently at that position (if any) and any |
333 |
|
|
* subsequent elements to the right (adds one to their indices). |
334 |
|
|
* |
335 |
jsr166 |
1.27 |
* @param index index at which the specified element is to be inserted |
336 |
|
|
* @param element element to be inserted |
337 |
jsr166 |
1.28 |
* @throws IndexOutOfBoundsException {@inheritDoc} |
338 |
tim |
1.1 |
*/ |
339 |
dl |
1.3 |
public void add(int index, E element) { |
340 |
tim |
1.1 |
addBefore(element, (index==size ? header : entry(index))); |
341 |
|
|
} |
342 |
|
|
|
343 |
|
|
/** |
344 |
|
|
* Removes the element at the specified position in this list. Shifts any |
345 |
|
|
* subsequent elements to the left (subtracts one from their indices). |
346 |
|
|
* Returns the element that was removed from the list. |
347 |
|
|
* |
348 |
jsr166 |
1.27 |
* @param index the index of the element to be removed |
349 |
|
|
* @return the element previously at the specified position |
350 |
jsr166 |
1.28 |
* @throws IndexOutOfBoundsException {@inheritDoc} |
351 |
tim |
1.1 |
*/ |
352 |
dl |
1.3 |
public E remove(int index) { |
353 |
jsr166 |
1.14 |
return remove(entry(index)); |
354 |
tim |
1.1 |
} |
355 |
|
|
|
356 |
|
|
/** |
357 |
jsr166 |
1.24 |
* Returns the indexed entry. |
358 |
tim |
1.1 |
*/ |
359 |
dl |
1.3 |
private Entry<E> entry(int index) { |
360 |
tim |
1.1 |
if (index < 0 || index >= size) |
361 |
|
|
throw new IndexOutOfBoundsException("Index: "+index+ |
362 |
|
|
", Size: "+size); |
363 |
dl |
1.3 |
Entry<E> e = header; |
364 |
tim |
1.1 |
if (index < (size >> 1)) { |
365 |
|
|
for (int i = 0; i <= index; i++) |
366 |
|
|
e = e.next; |
367 |
|
|
} else { |
368 |
|
|
for (int i = size; i > index; i--) |
369 |
|
|
e = e.previous; |
370 |
|
|
} |
371 |
|
|
return e; |
372 |
|
|
} |
373 |
|
|
|
374 |
|
|
|
375 |
|
|
// Search Operations |
376 |
|
|
|
377 |
|
|
/** |
378 |
|
|
* Returns the index in this list of the first occurrence of the |
379 |
|
|
* specified element, or -1 if the List does not contain this |
380 |
|
|
* element. More formally, returns the lowest index i such that |
381 |
|
|
* <tt>(o==null ? get(i)==null : o.equals(get(i)))</tt>, or -1 if |
382 |
|
|
* there is no such index. |
383 |
|
|
* |
384 |
jsr166 |
1.27 |
* @param o element to search for |
385 |
tim |
1.1 |
* @return the index in this list of the first occurrence of the |
386 |
jsr166 |
1.27 |
* specified element, or -1 if the list does not contain this |
387 |
|
|
* element |
388 |
tim |
1.1 |
*/ |
389 |
|
|
public int indexOf(Object o) { |
390 |
|
|
int index = 0; |
391 |
|
|
if (o==null) { |
392 |
|
|
for (Entry e = header.next; e != header; e = e.next) { |
393 |
|
|
if (e.element==null) |
394 |
|
|
return index; |
395 |
|
|
index++; |
396 |
|
|
} |
397 |
|
|
} else { |
398 |
|
|
for (Entry e = header.next; e != header; e = e.next) { |
399 |
|
|
if (o.equals(e.element)) |
400 |
|
|
return index; |
401 |
|
|
index++; |
402 |
|
|
} |
403 |
|
|
} |
404 |
|
|
return -1; |
405 |
|
|
} |
406 |
|
|
|
407 |
|
|
/** |
408 |
|
|
* Returns the index in this list of the last occurrence of the |
409 |
|
|
* specified element, or -1 if the list does not contain this |
410 |
|
|
* element. More formally, returns the highest index i such that |
411 |
|
|
* <tt>(o==null ? get(i)==null : o.equals(get(i)))</tt>, or -1 if |
412 |
|
|
* there is no such index. |
413 |
|
|
* |
414 |
jsr166 |
1.27 |
* @param o element to search for |
415 |
tim |
1.1 |
* @return the index in this list of the last occurrence of the |
416 |
jsr166 |
1.27 |
* specified element, or -1 if the list does not contain this |
417 |
|
|
* element |
418 |
tim |
1.1 |
*/ |
419 |
|
|
public int lastIndexOf(Object o) { |
420 |
|
|
int index = size; |
421 |
|
|
if (o==null) { |
422 |
|
|
for (Entry e = header.previous; e != header; e = e.previous) { |
423 |
|
|
index--; |
424 |
|
|
if (e.element==null) |
425 |
|
|
return index; |
426 |
|
|
} |
427 |
|
|
} else { |
428 |
|
|
for (Entry e = header.previous; e != header; e = e.previous) { |
429 |
|
|
index--; |
430 |
|
|
if (o.equals(e.element)) |
431 |
|
|
return index; |
432 |
|
|
} |
433 |
|
|
} |
434 |
|
|
return -1; |
435 |
|
|
} |
436 |
|
|
|
437 |
dl |
1.3 |
// Queue operations. |
438 |
|
|
|
439 |
|
|
/** |
440 |
dl |
1.7 |
* Retrieves, but does not remove, the head (first element) of this list. |
441 |
jsr166 |
1.27 |
* @return the head of this list, or <tt>null</tt> if this list is empty |
442 |
dl |
1.7 |
* @since 1.5 |
443 |
dl |
1.3 |
*/ |
444 |
|
|
public E peek() { |
445 |
|
|
if (size==0) |
446 |
|
|
return null; |
447 |
|
|
return getFirst(); |
448 |
|
|
} |
449 |
|
|
|
450 |
|
|
/** |
451 |
dl |
1.7 |
* Retrieves, but does not remove, the head (first element) of this list. |
452 |
jsr166 |
1.27 |
* @return the head of this list |
453 |
|
|
* @throws NoSuchElementException if this list is empty |
454 |
dl |
1.7 |
* @since 1.5 |
455 |
dl |
1.3 |
*/ |
456 |
|
|
public E element() { |
457 |
|
|
return getFirst(); |
458 |
|
|
} |
459 |
|
|
|
460 |
|
|
/** |
461 |
jsr166 |
1.27 |
* Retrieves and removes the head (first element) of this list |
462 |
|
|
* @return the head of this list, or <tt>null</tt> if this list is empty |
463 |
dl |
1.7 |
* @since 1.5 |
464 |
dl |
1.3 |
*/ |
465 |
|
|
public E poll() { |
466 |
|
|
if (size==0) |
467 |
|
|
return null; |
468 |
|
|
return removeFirst(); |
469 |
|
|
} |
470 |
|
|
|
471 |
|
|
/** |
472 |
dl |
1.7 |
* Retrieves and removes the head (first element) of this list. |
473 |
jsr166 |
1.27 |
* |
474 |
|
|
* @return the head of this list |
475 |
|
|
* @throws NoSuchElementException if this list is empty |
476 |
dl |
1.7 |
* @since 1.5 |
477 |
dl |
1.3 |
*/ |
478 |
|
|
public E remove() { |
479 |
|
|
return removeFirst(); |
480 |
|
|
} |
481 |
|
|
|
482 |
|
|
/** |
483 |
|
|
* Adds the specified element as the tail (last element) of this list. |
484 |
|
|
* |
485 |
jsr166 |
1.27 |
* @param e the element to add |
486 |
|
|
* @return <tt>true</tt> (as per the spec for {@link Queue#offer}) |
487 |
dl |
1.7 |
* @since 1.5 |
488 |
dl |
1.3 |
*/ |
489 |
jsr166 |
1.26 |
public boolean offer(E e) { |
490 |
|
|
return add(e); |
491 |
dl |
1.3 |
} |
492 |
|
|
|
493 |
dl |
1.17 |
// Deque operations |
494 |
|
|
/** |
495 |
dl |
1.22 |
* Inserts the specified element at the front of this list. |
496 |
dl |
1.17 |
* |
497 |
dl |
1.22 |
* @param e the element to insert |
498 |
dl |
1.17 |
* @return <tt>true</tt> (as per the spec for {@link Deque#offerFirst}) |
499 |
|
|
* @since 1.6 |
500 |
|
|
*/ |
501 |
dl |
1.22 |
public boolean offerFirst(E e) { |
502 |
|
|
addFirst(e); |
503 |
dl |
1.17 |
return true; |
504 |
|
|
} |
505 |
|
|
|
506 |
|
|
/** |
507 |
dl |
1.22 |
* Inserts the specified element at the end of this list. |
508 |
dl |
1.17 |
* |
509 |
dl |
1.22 |
* @param e the element to insert |
510 |
dl |
1.17 |
* @return <tt>true</tt> (as per the spec for {@link Deque#offerLast}) |
511 |
|
|
* @since 1.6 |
512 |
|
|
*/ |
513 |
dl |
1.22 |
public boolean offerLast(E e) { |
514 |
|
|
addLast(e); |
515 |
dl |
1.17 |
return true; |
516 |
|
|
} |
517 |
|
|
|
518 |
|
|
/** |
519 |
dl |
1.19 |
* Retrieves, but does not remove, the first element of this list, |
520 |
jsr166 |
1.27 |
* or returns <tt>null</tt> if this list is empty. |
521 |
dl |
1.17 |
* |
522 |
jsr166 |
1.27 |
* @return the first element of this list, or <tt>null</tt> |
523 |
jsr166 |
1.28 |
* if this list is empty |
524 |
dl |
1.17 |
* @since 1.6 |
525 |
|
|
*/ |
526 |
|
|
public E peekFirst() { |
527 |
|
|
if (size==0) |
528 |
|
|
return null; |
529 |
dl |
1.18 |
return getFirst(); |
530 |
dl |
1.17 |
} |
531 |
|
|
|
532 |
|
|
/** |
533 |
dl |
1.19 |
* Retrieves, but does not remove, the last element of this list, |
534 |
jsr166 |
1.27 |
* or returns <tt>null</tt> if this list is empty. |
535 |
dl |
1.17 |
* |
536 |
jsr166 |
1.27 |
* @return the last element of this list, or <tt>null</tt> |
537 |
jsr166 |
1.28 |
* if this list is empty |
538 |
dl |
1.17 |
* @since 1.6 |
539 |
|
|
*/ |
540 |
|
|
public E peekLast() { |
541 |
|
|
if (size==0) |
542 |
|
|
return null; |
543 |
dl |
1.18 |
return getLast(); |
544 |
dl |
1.17 |
} |
545 |
|
|
|
546 |
|
|
/** |
547 |
dl |
1.19 |
* Retrieves and removes the first element of this list, or |
548 |
|
|
* <tt>null</tt> if this list is empty. |
549 |
dl |
1.17 |
* |
550 |
dl |
1.19 |
* @return the first element of this list, or <tt>null</tt> if |
551 |
|
|
* this list is empty |
552 |
dl |
1.17 |
* @since 1.6 |
553 |
|
|
*/ |
554 |
|
|
public E pollFirst() { |
555 |
|
|
if (size==0) |
556 |
|
|
return null; |
557 |
|
|
return removeFirst(); |
558 |
|
|
} |
559 |
|
|
|
560 |
|
|
/** |
561 |
dl |
1.19 |
* Retrieves and removes the last element of this list, or |
562 |
|
|
* <tt>null</tt> if this list is empty. |
563 |
dl |
1.17 |
* |
564 |
dl |
1.19 |
* @return the last element of this list, or <tt>null</tt> if |
565 |
|
|
* this list is empty |
566 |
dl |
1.17 |
* @since 1.6 |
567 |
|
|
*/ |
568 |
|
|
public E pollLast() { |
569 |
|
|
if (size==0) |
570 |
|
|
return null; |
571 |
|
|
return removeLast(); |
572 |
|
|
} |
573 |
|
|
|
574 |
|
|
/** |
575 |
dl |
1.19 |
* Pushes an element onto the stack represented by this list. In other |
576 |
dl |
1.22 |
* words, inserts the element at the front of this list. |
577 |
dl |
1.17 |
* |
578 |
|
|
* <p>This method is equivalent to {@link #addFirst}. |
579 |
|
|
* |
580 |
dl |
1.22 |
* @param e the element to push |
581 |
dl |
1.17 |
* @since 1.6 |
582 |
|
|
*/ |
583 |
dl |
1.22 |
public void push(E e) { |
584 |
|
|
addFirst(e); |
585 |
dl |
1.17 |
} |
586 |
|
|
|
587 |
|
|
/** |
588 |
dl |
1.19 |
* Pops an element from the stack represented by this list. In other |
589 |
dl |
1.20 |
* words, removes and returns the first element of this list. |
590 |
dl |
1.17 |
* |
591 |
|
|
* <p>This method is equivalent to {@link #removeFirst()}. |
592 |
|
|
* |
593 |
dl |
1.19 |
* @return the element at the front of this list (which is the top |
594 |
jsr166 |
1.27 |
* of the stack represented by this list) |
595 |
|
|
* @throws NoSuchElementException if this list is empty |
596 |
dl |
1.17 |
* @since 1.6 |
597 |
|
|
*/ |
598 |
|
|
public E pop() { |
599 |
|
|
return removeFirst(); |
600 |
|
|
} |
601 |
|
|
|
602 |
|
|
/** |
603 |
|
|
* Removes the first occurrence of the specified element in this |
604 |
dl |
1.19 |
* list (when traversing the list from head to tail). If the list |
605 |
dl |
1.17 |
* does not contain the element, it is unchanged. |
606 |
|
|
* |
607 |
dl |
1.21 |
* @param o element to be removed from this list, if present |
608 |
dl |
1.19 |
* @return <tt>true</tt> if the list contained the specified element |
609 |
dl |
1.17 |
* @since 1.6 |
610 |
|
|
*/ |
611 |
dl |
1.21 |
public boolean removeFirstOccurrence(Object o) { |
612 |
|
|
return remove(o); |
613 |
dl |
1.17 |
} |
614 |
|
|
|
615 |
|
|
/** |
616 |
|
|
* Removes the last occurrence of the specified element in this |
617 |
dl |
1.19 |
* list (when traversing the list from head to tail). If the list |
618 |
dl |
1.17 |
* does not contain the element, it is unchanged. |
619 |
|
|
* |
620 |
dl |
1.19 |
* @param o element to be removed from this list, if present |
621 |
|
|
* @return <tt>true</tt> if the list contained the specified element |
622 |
dl |
1.17 |
* @since 1.6 |
623 |
|
|
*/ |
624 |
|
|
public boolean removeLastOccurrence(Object o) { |
625 |
|
|
if (o==null) { |
626 |
|
|
for (Entry e = header.previous; e != header; e = e.previous) { |
627 |
|
|
if (e.element==null) { |
628 |
|
|
remove(e); |
629 |
|
|
return true; |
630 |
|
|
} |
631 |
|
|
} |
632 |
|
|
} else { |
633 |
|
|
for (Entry e = header.previous; e != header; e = e.previous) { |
634 |
|
|
if (o.equals(e.element)) { |
635 |
|
|
remove(e); |
636 |
|
|
return true; |
637 |
|
|
} |
638 |
|
|
} |
639 |
|
|
} |
640 |
|
|
return false; |
641 |
|
|
} |
642 |
|
|
|
643 |
tim |
1.1 |
/** |
644 |
|
|
* Returns a list-iterator of the elements in this list (in proper |
645 |
|
|
* sequence), starting at the specified position in the list. |
646 |
|
|
* Obeys the general contract of <tt>List.listIterator(int)</tt>.<p> |
647 |
|
|
* |
648 |
|
|
* The list-iterator is <i>fail-fast</i>: if the list is structurally |
649 |
|
|
* modified at any time after the Iterator is created, in any way except |
650 |
|
|
* through the list-iterator's own <tt>remove</tt> or <tt>add</tt> |
651 |
|
|
* methods, the list-iterator will throw a |
652 |
|
|
* <tt>ConcurrentModificationException</tt>. Thus, in the face of |
653 |
|
|
* concurrent modification, the iterator fails quickly and cleanly, rather |
654 |
|
|
* than risking arbitrary, non-deterministic behavior at an undetermined |
655 |
|
|
* time in the future. |
656 |
|
|
* |
657 |
jsr166 |
1.27 |
* @param index index of the first element to be returned from the |
658 |
jsr166 |
1.28 |
* list-iterator (by a call to <tt>next</tt>) |
659 |
tim |
1.1 |
* @return a ListIterator of the elements in this list (in proper |
660 |
jsr166 |
1.28 |
* sequence), starting at the specified position in the list |
661 |
|
|
* @throws IndexOutOfBoundsException {@inheritDoc} |
662 |
dl |
1.3 |
* @see List#listIterator(int) |
663 |
tim |
1.1 |
*/ |
664 |
dl |
1.3 |
public ListIterator<E> listIterator(int index) { |
665 |
jsr166 |
1.14 |
return new ListItr(index); |
666 |
tim |
1.1 |
} |
667 |
|
|
|
668 |
dl |
1.3 |
private class ListItr implements ListIterator<E> { |
669 |
jsr166 |
1.14 |
private Entry<E> lastReturned = header; |
670 |
|
|
private Entry<E> next; |
671 |
|
|
private int nextIndex; |
672 |
|
|
private int expectedModCount = modCount; |
673 |
|
|
|
674 |
|
|
ListItr(int index) { |
675 |
|
|
if (index < 0 || index > size) |
676 |
|
|
throw new IndexOutOfBoundsException("Index: "+index+ |
677 |
|
|
", Size: "+size); |
678 |
|
|
if (index < (size >> 1)) { |
679 |
|
|
next = header.next; |
680 |
|
|
for (nextIndex=0; nextIndex<index; nextIndex++) |
681 |
|
|
next = next.next; |
682 |
|
|
} else { |
683 |
|
|
next = header; |
684 |
|
|
for (nextIndex=size; nextIndex>index; nextIndex--) |
685 |
|
|
next = next.previous; |
686 |
|
|
} |
687 |
|
|
} |
688 |
|
|
|
689 |
|
|
public boolean hasNext() { |
690 |
|
|
return nextIndex != size; |
691 |
|
|
} |
692 |
|
|
|
693 |
|
|
public E next() { |
694 |
|
|
checkForComodification(); |
695 |
|
|
if (nextIndex == size) |
696 |
|
|
throw new NoSuchElementException(); |
697 |
|
|
|
698 |
|
|
lastReturned = next; |
699 |
|
|
next = next.next; |
700 |
|
|
nextIndex++; |
701 |
|
|
return lastReturned.element; |
702 |
|
|
} |
703 |
|
|
|
704 |
|
|
public boolean hasPrevious() { |
705 |
|
|
return nextIndex != 0; |
706 |
|
|
} |
707 |
|
|
|
708 |
|
|
public E previous() { |
709 |
|
|
if (nextIndex == 0) |
710 |
|
|
throw new NoSuchElementException(); |
711 |
|
|
|
712 |
|
|
lastReturned = next = next.previous; |
713 |
|
|
nextIndex--; |
714 |
|
|
checkForComodification(); |
715 |
|
|
return lastReturned.element; |
716 |
|
|
} |
717 |
|
|
|
718 |
|
|
public int nextIndex() { |
719 |
|
|
return nextIndex; |
720 |
|
|
} |
721 |
|
|
|
722 |
|
|
public int previousIndex() { |
723 |
|
|
return nextIndex-1; |
724 |
|
|
} |
725 |
jozart |
1.12 |
|
726 |
jsr166 |
1.14 |
public void remove() { |
727 |
tim |
1.1 |
checkForComodification(); |
728 |
jsr166 |
1.14 |
Entry<E> lastNext = lastReturned.next; |
729 |
tim |
1.1 |
try { |
730 |
|
|
LinkedList.this.remove(lastReturned); |
731 |
|
|
} catch (NoSuchElementException e) { |
732 |
|
|
throw new IllegalStateException(); |
733 |
|
|
} |
734 |
jsr166 |
1.14 |
if (next==lastReturned) |
735 |
|
|
next = lastNext; |
736 |
tim |
1.1 |
else |
737 |
jsr166 |
1.14 |
nextIndex--; |
738 |
|
|
lastReturned = header; |
739 |
|
|
expectedModCount++; |
740 |
|
|
} |
741 |
|
|
|
742 |
jsr166 |
1.26 |
public void set(E e) { |
743 |
jsr166 |
1.14 |
if (lastReturned == header) |
744 |
|
|
throw new IllegalStateException(); |
745 |
|
|
checkForComodification(); |
746 |
jsr166 |
1.26 |
lastReturned.element = e; |
747 |
jsr166 |
1.14 |
} |
748 |
|
|
|
749 |
jsr166 |
1.26 |
public void add(E e) { |
750 |
jsr166 |
1.14 |
checkForComodification(); |
751 |
|
|
lastReturned = header; |
752 |
jsr166 |
1.26 |
addBefore(e, next); |
753 |
jsr166 |
1.14 |
nextIndex++; |
754 |
|
|
expectedModCount++; |
755 |
|
|
} |
756 |
|
|
|
757 |
|
|
final void checkForComodification() { |
758 |
|
|
if (modCount != expectedModCount) |
759 |
|
|
throw new ConcurrentModificationException(); |
760 |
|
|
} |
761 |
dl |
1.3 |
} |
762 |
|
|
|
763 |
|
|
private static class Entry<E> { |
764 |
jsr166 |
1.14 |
E element; |
765 |
|
|
Entry<E> next; |
766 |
|
|
Entry<E> previous; |
767 |
|
|
|
768 |
|
|
Entry(E element, Entry<E> next, Entry<E> previous) { |
769 |
|
|
this.element = element; |
770 |
|
|
this.next = next; |
771 |
|
|
this.previous = previous; |
772 |
|
|
} |
773 |
dl |
1.3 |
} |
774 |
|
|
|
775 |
jsr166 |
1.26 |
private Entry<E> addBefore(E e, Entry<E> entry) { |
776 |
|
|
Entry<E> newEntry = new Entry<E>(e, entry, entry.previous); |
777 |
jsr166 |
1.14 |
newEntry.previous.next = newEntry; |
778 |
|
|
newEntry.next.previous = newEntry; |
779 |
|
|
size++; |
780 |
|
|
modCount++; |
781 |
|
|
return newEntry; |
782 |
|
|
} |
783 |
|
|
|
784 |
|
|
private E remove(Entry<E> e) { |
785 |
|
|
if (e == header) |
786 |
|
|
throw new NoSuchElementException(); |
787 |
|
|
|
788 |
|
|
E result = e.element; |
789 |
|
|
e.previous.next = e.next; |
790 |
|
|
e.next.previous = e.previous; |
791 |
|
|
e.next = e.previous = null; |
792 |
|
|
e.element = null; |
793 |
|
|
size--; |
794 |
|
|
modCount++; |
795 |
|
|
return result; |
796 |
tim |
1.1 |
} |
797 |
|
|
|
798 |
|
|
/** |
799 |
|
|
* Returns a shallow copy of this <tt>LinkedList</tt>. (The elements |
800 |
|
|
* themselves are not cloned.) |
801 |
|
|
* |
802 |
jsr166 |
1.27 |
* @return a shallow copy of this <tt>LinkedList</tt> instance |
803 |
tim |
1.1 |
*/ |
804 |
|
|
public Object clone() { |
805 |
dl |
1.3 |
LinkedList<E> clone = null; |
806 |
jsr166 |
1.14 |
try { |
807 |
|
|
clone = (LinkedList<E>) super.clone(); |
808 |
|
|
} catch (CloneNotSupportedException e) { |
809 |
|
|
throw new InternalError(); |
810 |
|
|
} |
811 |
tim |
1.1 |
|
812 |
|
|
// Put clone into "virgin" state |
813 |
dl |
1.3 |
clone.header = new Entry<E>(null, null, null); |
814 |
tim |
1.1 |
clone.header.next = clone.header.previous = clone.header; |
815 |
|
|
clone.size = 0; |
816 |
|
|
clone.modCount = 0; |
817 |
|
|
|
818 |
|
|
// Initialize clone with our elements |
819 |
dl |
1.3 |
for (Entry<E> e = header.next; e != header; e = e.next) |
820 |
tim |
1.1 |
clone.add(e.element); |
821 |
|
|
|
822 |
|
|
return clone; |
823 |
|
|
} |
824 |
|
|
|
825 |
|
|
/** |
826 |
|
|
* Returns an array containing all of the elements in this list |
827 |
|
|
* in the correct order. |
828 |
|
|
* |
829 |
|
|
* @return an array containing all of the elements in this list |
830 |
jsr166 |
1.28 |
* in the correct order |
831 |
tim |
1.1 |
*/ |
832 |
|
|
public Object[] toArray() { |
833 |
jsr166 |
1.14 |
Object[] result = new Object[size]; |
834 |
tim |
1.1 |
int i = 0; |
835 |
dl |
1.3 |
for (Entry<E> e = header.next; e != header; e = e.next) |
836 |
tim |
1.1 |
result[i++] = e.element; |
837 |
jsr166 |
1.14 |
return result; |
838 |
tim |
1.1 |
} |
839 |
|
|
|
840 |
|
|
/** |
841 |
|
|
* Returns an array containing all of the elements in this list in |
842 |
|
|
* the correct order; the runtime type of the returned array is that of |
843 |
|
|
* the specified array. If the list fits in the specified array, it |
844 |
|
|
* is returned therein. Otherwise, a new array is allocated with the |
845 |
|
|
* runtime type of the specified array and the size of this list.<p> |
846 |
|
|
* |
847 |
|
|
* If the list fits in the specified array with room to spare |
848 |
|
|
* (i.e., the array has more elements than the list), |
849 |
|
|
* the element in the array immediately following the end of the |
850 |
|
|
* collection is set to null. This is useful in determining the length |
851 |
|
|
* of the list <i>only</i> if the caller knows that the list |
852 |
|
|
* does not contain any null elements. |
853 |
|
|
* |
854 |
|
|
* @param a the array into which the elements of the list are to |
855 |
jsr166 |
1.27 |
* be stored, if it is big enough; otherwise, a new array of the |
856 |
|
|
* same runtime type is allocated for this purpose. |
857 |
|
|
* @return an array containing the elements of the list |
858 |
tim |
1.1 |
* @throws ArrayStoreException if the runtime type of a is not a |
859 |
jsr166 |
1.27 |
* supertype of the runtime type of every element in this list |
860 |
|
|
* @throws NullPointerException if the specified array is null |
861 |
tim |
1.1 |
*/ |
862 |
dl |
1.3 |
public <T> T[] toArray(T[] a) { |
863 |
tim |
1.1 |
if (a.length < size) |
864 |
dl |
1.3 |
a = (T[])java.lang.reflect.Array.newInstance( |
865 |
tim |
1.1 |
a.getClass().getComponentType(), size); |
866 |
|
|
int i = 0; |
867 |
jsr166 |
1.14 |
Object[] result = a; |
868 |
dl |
1.3 |
for (Entry<E> e = header.next; e != header; e = e.next) |
869 |
|
|
result[i++] = e.element; |
870 |
tim |
1.1 |
|
871 |
|
|
if (a.length > size) |
872 |
|
|
a[size] = null; |
873 |
|
|
|
874 |
|
|
return a; |
875 |
|
|
} |
876 |
|
|
|
877 |
|
|
private static final long serialVersionUID = 876323262645176354L; |
878 |
|
|
|
879 |
|
|
/** |
880 |
|
|
* Save the state of this <tt>LinkedList</tt> instance to a stream (that |
881 |
|
|
* is, serialize it). |
882 |
|
|
* |
883 |
|
|
* @serialData The size of the list (the number of elements it |
884 |
jsr166 |
1.27 |
* contains) is emitted (int), followed by all of its |
885 |
|
|
* elements (each an Object) in the proper order. |
886 |
tim |
1.1 |
*/ |
887 |
dl |
1.3 |
private void writeObject(java.io.ObjectOutputStream s) |
888 |
tim |
1.1 |
throws java.io.IOException { |
889 |
jsr166 |
1.14 |
// Write out any hidden serialization magic |
890 |
|
|
s.defaultWriteObject(); |
891 |
tim |
1.1 |
|
892 |
|
|
// Write out size |
893 |
|
|
s.writeInt(size); |
894 |
|
|
|
895 |
jsr166 |
1.14 |
// Write out all elements in the proper order. |
896 |
tim |
1.1 |
for (Entry e = header.next; e != header; e = e.next) |
897 |
|
|
s.writeObject(e.element); |
898 |
|
|
} |
899 |
|
|
|
900 |
|
|
/** |
901 |
|
|
* Reconstitute this <tt>LinkedList</tt> instance from a stream (that is |
902 |
|
|
* deserialize it). |
903 |
|
|
*/ |
904 |
dl |
1.3 |
private void readObject(java.io.ObjectInputStream s) |
905 |
tim |
1.1 |
throws java.io.IOException, ClassNotFoundException { |
906 |
jsr166 |
1.14 |
// Read in any hidden serialization magic |
907 |
|
|
s.defaultReadObject(); |
908 |
tim |
1.1 |
|
909 |
|
|
// Read in size |
910 |
|
|
int size = s.readInt(); |
911 |
|
|
|
912 |
|
|
// Initialize header |
913 |
dl |
1.3 |
header = new Entry<E>(null, null, null); |
914 |
tim |
1.1 |
header.next = header.previous = header; |
915 |
|
|
|
916 |
jsr166 |
1.14 |
// Read in all elements in the proper order. |
917 |
|
|
for (int i=0; i<size; i++) |
918 |
dl |
1.3 |
addBefore((E)s.readObject(), header); |
919 |
tim |
1.1 |
} |
920 |
|
|
} |