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 |
|
|
* @param c the collection whose elements are to be placed into this list. |
93 |
|
|
* @throws NullPointerException if the specified collection is null. |
94 |
|
|
*/ |
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 |
|
|
* @return the first element in this list. |
104 |
|
|
* @throws NoSuchElementException if this list is empty. |
105 |
|
|
*/ |
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 |
|
|
* @return the last element in this list. |
117 |
|
|
* @throws NoSuchElementException if this list is empty. |
118 |
|
|
*/ |
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 |
|
|
* @return the first element from this list. |
130 |
|
|
* @throws NoSuchElementException if this list is empty. |
131 |
|
|
*/ |
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 |
|
|
* @return the last element from this list. |
140 |
|
|
* @throws NoSuchElementException if this list is empty. |
141 |
|
|
*/ |
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 |
tim |
1.1 |
* @param o the element to be inserted at the beginning of this list. |
150 |
|
|
*/ |
151 |
dl |
1.3 |
public void addFirst(E o) { |
152 |
jsr166 |
1.14 |
addBefore(o, 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 |
tim |
1.1 |
* @param o the element to be inserted at the end of this list. |
160 |
|
|
*/ |
161 |
dl |
1.3 |
public void addLast(E o) { |
162 |
jsr166 |
1.14 |
addBefore(o, 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 |
|
|
* at least one element <tt>e</tt> such that <tt>(o==null ? e==null |
169 |
|
|
* : o.equals(e))</tt>. |
170 |
|
|
* |
171 |
|
|
* @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 |
|
|
*/ |
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 |
|
|
* @return the number of elements in this list. |
182 |
|
|
*/ |
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 |
|
|
* @param o element to be appended to this list. |
191 |
|
|
* @return <tt>true</tt> (as per the general contract of |
192 |
|
|
* <tt>Collection.add</tt>). |
193 |
|
|
*/ |
194 |
dl |
1.3 |
public boolean add(E o) { |
195 |
jsr166 |
1.14 |
addBefore(o, header); |
196 |
tim |
1.1 |
return true; |
197 |
|
|
} |
198 |
|
|
|
199 |
|
|
/** |
200 |
|
|
* Removes the first occurrence of the specified element in this list. If |
201 |
|
|
* the list does not contain the element, it is unchanged. More formally, |
202 |
|
|
* removes the element with the lowest index <tt>i</tt> such that |
203 |
|
|
* <tt>(o==null ? get(i)==null : o.equals(get(i)))</tt> (if such an |
204 |
|
|
* element exists). |
205 |
|
|
* |
206 |
|
|
* @param o element to be removed from this list, if present. |
207 |
|
|
* @return <tt>true</tt> if the list contained the specified element. |
208 |
|
|
*/ |
209 |
|
|
public boolean remove(Object o) { |
210 |
|
|
if (o==null) { |
211 |
dl |
1.3 |
for (Entry<E> e = header.next; e != header; e = e.next) { |
212 |
tim |
1.1 |
if (e.element==null) { |
213 |
|
|
remove(e); |
214 |
|
|
return true; |
215 |
|
|
} |
216 |
|
|
} |
217 |
|
|
} else { |
218 |
dl |
1.3 |
for (Entry<E> e = header.next; e != header; e = e.next) { |
219 |
tim |
1.1 |
if (o.equals(e.element)) { |
220 |
|
|
remove(e); |
221 |
|
|
return true; |
222 |
|
|
} |
223 |
|
|
} |
224 |
|
|
} |
225 |
|
|
return false; |
226 |
|
|
} |
227 |
|
|
|
228 |
|
|
/** |
229 |
jsr166 |
1.24 |
* Appends all of the elements in the specified collection to the end of |
230 |
tim |
1.1 |
* this list, in the order that they are returned by the specified |
231 |
|
|
* collection's iterator. The behavior of this operation is undefined if |
232 |
|
|
* the specified collection is modified while the operation is in |
233 |
|
|
* progress. (This implies that the behavior of this call is undefined if |
234 |
|
|
* the specified Collection is this list, and this list is nonempty.) |
235 |
|
|
* |
236 |
|
|
* @param c the elements to be inserted into this list. |
237 |
|
|
* @return <tt>true</tt> if this list changed as a result of the call. |
238 |
|
|
* @throws NullPointerException if the specified collection is null. |
239 |
|
|
*/ |
240 |
dl |
1.3 |
public boolean addAll(Collection<? extends E> c) { |
241 |
tim |
1.1 |
return addAll(size, c); |
242 |
|
|
} |
243 |
|
|
|
244 |
|
|
/** |
245 |
|
|
* Inserts all of the elements in the specified collection into this |
246 |
|
|
* list, starting at the specified position. Shifts the element |
247 |
|
|
* currently at that position (if any) and any subsequent elements to |
248 |
|
|
* the right (increases their indices). The new elements will appear |
249 |
|
|
* in the list in the order that they are returned by the |
250 |
|
|
* specified collection's iterator. |
251 |
|
|
* |
252 |
|
|
* @param index index at which to insert first element |
253 |
jsr166 |
1.14 |
* from the specified collection. |
254 |
tim |
1.1 |
* @param c elements to be inserted into this list. |
255 |
|
|
* @return <tt>true</tt> if this list changed as a result of the call. |
256 |
jsr166 |
1.24 |
* @throws IndexOutOfBoundsException if the index is out of range |
257 |
|
|
* (<tt>index < 0 || index > size()</tt>). |
258 |
tim |
1.1 |
* @throws NullPointerException if the specified collection is null. |
259 |
|
|
*/ |
260 |
dl |
1.3 |
public boolean addAll(int index, Collection<? extends E> c) { |
261 |
dl |
1.4 |
if (index < 0 || index > size) |
262 |
dl |
1.3 |
throw new IndexOutOfBoundsException("Index: "+index+ |
263 |
|
|
", Size: "+size); |
264 |
|
|
Object[] a = c.toArray(); |
265 |
|
|
int numNew = a.length; |
266 |
|
|
if (numNew==0) |
267 |
|
|
return false; |
268 |
jsr166 |
1.14 |
modCount++; |
269 |
dl |
1.3 |
|
270 |
|
|
Entry<E> successor = (index==size ? header : entry(index)); |
271 |
|
|
Entry<E> predecessor = successor.previous; |
272 |
jsr166 |
1.14 |
for (int i=0; i<numNew; i++) { |
273 |
dl |
1.3 |
Entry<E> e = new Entry<E>((E)a[i], successor, predecessor); |
274 |
tim |
1.1 |
predecessor.next = e; |
275 |
|
|
predecessor = e; |
276 |
|
|
} |
277 |
|
|
successor.previous = predecessor; |
278 |
|
|
|
279 |
|
|
size += numNew; |
280 |
|
|
return true; |
281 |
|
|
} |
282 |
|
|
|
283 |
|
|
/** |
284 |
|
|
* Removes all of the elements from this list. |
285 |
|
|
*/ |
286 |
|
|
public void clear() { |
287 |
jsr166 |
1.14 |
Entry<E> e = header.next; |
288 |
|
|
while (e != header) { |
289 |
|
|
Entry<E> next = e.next; |
290 |
|
|
e.next = e.previous = null; |
291 |
|
|
e.element = null; |
292 |
|
|
e = next; |
293 |
|
|
} |
294 |
tim |
1.1 |
header.next = header.previous = header; |
295 |
jozart |
1.12 |
size = 0; |
296 |
jsr166 |
1.14 |
modCount++; |
297 |
tim |
1.1 |
} |
298 |
|
|
|
299 |
|
|
|
300 |
|
|
// Positional Access Operations |
301 |
|
|
|
302 |
|
|
/** |
303 |
|
|
* Returns the element at the specified position in this list. |
304 |
|
|
* |
305 |
|
|
* @param index index of element to return. |
306 |
|
|
* @return the element at the specified position in this list. |
307 |
dl |
1.3 |
* |
308 |
jsr166 |
1.24 |
* @throws IndexOutOfBoundsException if the index is out of range |
309 |
|
|
* (<tt>index < 0 || index > size()</tt>). |
310 |
tim |
1.1 |
*/ |
311 |
dl |
1.3 |
public E get(int index) { |
312 |
tim |
1.1 |
return entry(index).element; |
313 |
|
|
} |
314 |
|
|
|
315 |
|
|
/** |
316 |
|
|
* Replaces the element at the specified position in this list with the |
317 |
|
|
* specified element. |
318 |
|
|
* |
319 |
|
|
* @param index index of element to replace. |
320 |
|
|
* @param element element to be stored at the specified position. |
321 |
|
|
* @return the element previously at the specified position. |
322 |
jsr166 |
1.24 |
* @throws IndexOutOfBoundsException if the index is out of range |
323 |
|
|
* (<tt>index < 0 || index > size()</tt>). |
324 |
tim |
1.1 |
*/ |
325 |
dl |
1.3 |
public E set(int index, E element) { |
326 |
|
|
Entry<E> e = entry(index); |
327 |
|
|
E oldVal = e.element; |
328 |
tim |
1.1 |
e.element = element; |
329 |
|
|
return oldVal; |
330 |
|
|
} |
331 |
|
|
|
332 |
|
|
/** |
333 |
|
|
* Inserts the specified element at the specified position in this list. |
334 |
|
|
* Shifts the element currently at that position (if any) and any |
335 |
|
|
* subsequent elements to the right (adds one to their indices). |
336 |
|
|
* |
337 |
|
|
* @param index index at which the specified element is to be inserted. |
338 |
|
|
* @param element element to be inserted. |
339 |
dl |
1.3 |
* |
340 |
jsr166 |
1.24 |
* @throws IndexOutOfBoundsException if the index is out of range |
341 |
|
|
* (<tt>index < 0 || index > size()</tt>). |
342 |
tim |
1.1 |
*/ |
343 |
dl |
1.3 |
public void add(int index, E element) { |
344 |
tim |
1.1 |
addBefore(element, (index==size ? header : entry(index))); |
345 |
|
|
} |
346 |
|
|
|
347 |
|
|
/** |
348 |
|
|
* Removes the element at the specified position in this list. Shifts any |
349 |
|
|
* subsequent elements to the left (subtracts one from their indices). |
350 |
|
|
* Returns the element that was removed from the list. |
351 |
|
|
* |
352 |
|
|
* @param index the index of the element to removed. |
353 |
|
|
* @return the element previously at the specified position. |
354 |
dl |
1.3 |
* |
355 |
jsr166 |
1.24 |
* @throws IndexOutOfBoundsException if the index is out of range |
356 |
|
|
* (<tt>index < 0 || index > size()</tt>). |
357 |
tim |
1.1 |
*/ |
358 |
dl |
1.3 |
public E remove(int index) { |
359 |
jsr166 |
1.14 |
return remove(entry(index)); |
360 |
tim |
1.1 |
} |
361 |
|
|
|
362 |
|
|
/** |
363 |
jsr166 |
1.24 |
* Returns the indexed entry. |
364 |
tim |
1.1 |
*/ |
365 |
dl |
1.3 |
private Entry<E> entry(int index) { |
366 |
tim |
1.1 |
if (index < 0 || index >= size) |
367 |
|
|
throw new IndexOutOfBoundsException("Index: "+index+ |
368 |
|
|
", Size: "+size); |
369 |
dl |
1.3 |
Entry<E> e = header; |
370 |
tim |
1.1 |
if (index < (size >> 1)) { |
371 |
|
|
for (int i = 0; i <= index; i++) |
372 |
|
|
e = e.next; |
373 |
|
|
} else { |
374 |
|
|
for (int i = size; i > index; i--) |
375 |
|
|
e = e.previous; |
376 |
|
|
} |
377 |
|
|
return e; |
378 |
|
|
} |
379 |
|
|
|
380 |
|
|
|
381 |
|
|
// Search Operations |
382 |
|
|
|
383 |
|
|
/** |
384 |
|
|
* Returns the index in this list of the first occurrence of the |
385 |
|
|
* specified element, or -1 if the List does not contain this |
386 |
|
|
* element. More formally, returns the lowest index i such that |
387 |
|
|
* <tt>(o==null ? get(i)==null : o.equals(get(i)))</tt>, or -1 if |
388 |
|
|
* there is no such index. |
389 |
|
|
* |
390 |
|
|
* @param o element to search for. |
391 |
|
|
* @return the index in this list of the first occurrence of the |
392 |
jsr166 |
1.14 |
* specified element, or -1 if the list does not contain this |
393 |
|
|
* element. |
394 |
tim |
1.1 |
*/ |
395 |
|
|
public int indexOf(Object o) { |
396 |
|
|
int index = 0; |
397 |
|
|
if (o==null) { |
398 |
|
|
for (Entry e = header.next; e != header; e = e.next) { |
399 |
|
|
if (e.element==null) |
400 |
|
|
return index; |
401 |
|
|
index++; |
402 |
|
|
} |
403 |
|
|
} else { |
404 |
|
|
for (Entry e = header.next; e != header; e = e.next) { |
405 |
|
|
if (o.equals(e.element)) |
406 |
|
|
return index; |
407 |
|
|
index++; |
408 |
|
|
} |
409 |
|
|
} |
410 |
|
|
return -1; |
411 |
|
|
} |
412 |
|
|
|
413 |
|
|
/** |
414 |
|
|
* Returns the index in this list of the last occurrence of the |
415 |
|
|
* specified element, or -1 if the list does not contain this |
416 |
|
|
* element. More formally, returns the highest index i such that |
417 |
|
|
* <tt>(o==null ? get(i)==null : o.equals(get(i)))</tt>, or -1 if |
418 |
|
|
* there is no such index. |
419 |
|
|
* |
420 |
|
|
* @param o element to search for. |
421 |
|
|
* @return the index in this list of the last occurrence of the |
422 |
jsr166 |
1.14 |
* specified element, or -1 if the list does not contain this |
423 |
|
|
* element. |
424 |
tim |
1.1 |
*/ |
425 |
|
|
public int lastIndexOf(Object o) { |
426 |
|
|
int index = size; |
427 |
|
|
if (o==null) { |
428 |
|
|
for (Entry e = header.previous; e != header; e = e.previous) { |
429 |
|
|
index--; |
430 |
|
|
if (e.element==null) |
431 |
|
|
return index; |
432 |
|
|
} |
433 |
|
|
} else { |
434 |
|
|
for (Entry e = header.previous; e != header; e = e.previous) { |
435 |
|
|
index--; |
436 |
|
|
if (o.equals(e.element)) |
437 |
|
|
return index; |
438 |
|
|
} |
439 |
|
|
} |
440 |
|
|
return -1; |
441 |
|
|
} |
442 |
|
|
|
443 |
dl |
1.3 |
// Queue operations. |
444 |
|
|
|
445 |
|
|
/** |
446 |
dl |
1.7 |
* Retrieves, but does not remove, the head (first element) of this list. |
447 |
dl |
1.19 |
* @return the head of this list, or <tt>null</tt> if this list is empty. |
448 |
dl |
1.7 |
* @since 1.5 |
449 |
dl |
1.3 |
*/ |
450 |
|
|
public E peek() { |
451 |
|
|
if (size==0) |
452 |
|
|
return null; |
453 |
|
|
return getFirst(); |
454 |
|
|
} |
455 |
|
|
|
456 |
|
|
/** |
457 |
dl |
1.7 |
* Retrieves, but does not remove, the head (first element) of this list. |
458 |
dl |
1.19 |
* @return the head of this list. |
459 |
|
|
* @throws NoSuchElementException if this list is empty. |
460 |
dl |
1.7 |
* @since 1.5 |
461 |
dl |
1.3 |
*/ |
462 |
|
|
public E element() { |
463 |
|
|
return getFirst(); |
464 |
|
|
} |
465 |
|
|
|
466 |
|
|
/** |
467 |
dl |
1.7 |
* Retrieves and removes the head (first element) of this list. |
468 |
dl |
1.19 |
* @return the head of this list, or <tt>null</tt> if this list is empty. |
469 |
dl |
1.7 |
* @since 1.5 |
470 |
dl |
1.3 |
*/ |
471 |
|
|
public E poll() { |
472 |
|
|
if (size==0) |
473 |
|
|
return null; |
474 |
|
|
return removeFirst(); |
475 |
|
|
} |
476 |
|
|
|
477 |
|
|
/** |
478 |
dl |
1.7 |
* Retrieves and removes the head (first element) of this list. |
479 |
dl |
1.19 |
* @return the head of this list. |
480 |
|
|
* @throws NoSuchElementException if this list is empty. |
481 |
dl |
1.7 |
* @since 1.5 |
482 |
dl |
1.3 |
*/ |
483 |
|
|
public E remove() { |
484 |
|
|
return removeFirst(); |
485 |
|
|
} |
486 |
|
|
|
487 |
|
|
/** |
488 |
|
|
* Adds the specified element as the tail (last element) of this list. |
489 |
|
|
* |
490 |
jsr166 |
1.11 |
* @param o the element to add. |
491 |
dl |
1.3 |
* @return <tt>true</tt> (as per the general contract of |
492 |
|
|
* <tt>Queue.offer</tt>) |
493 |
dl |
1.7 |
* @since 1.5 |
494 |
dl |
1.3 |
*/ |
495 |
jsr166 |
1.11 |
public boolean offer(E o) { |
496 |
|
|
return add(o); |
497 |
dl |
1.3 |
} |
498 |
|
|
|
499 |
dl |
1.17 |
// Deque operations |
500 |
|
|
/** |
501 |
dl |
1.22 |
* Inserts the specified element at the front of this list. |
502 |
dl |
1.17 |
* |
503 |
dl |
1.22 |
* @param e the element to insert |
504 |
dl |
1.17 |
* @return <tt>true</tt> (as per the spec for {@link Deque#offerFirst}) |
505 |
|
|
* @since 1.6 |
506 |
|
|
*/ |
507 |
dl |
1.22 |
public boolean offerFirst(E e) { |
508 |
|
|
addFirst(e); |
509 |
dl |
1.17 |
return true; |
510 |
|
|
} |
511 |
|
|
|
512 |
|
|
/** |
513 |
dl |
1.22 |
* Inserts the specified element at the end of this list. |
514 |
dl |
1.17 |
* |
515 |
dl |
1.22 |
* @param e the element to insert |
516 |
dl |
1.17 |
* @return <tt>true</tt> (as per the spec for {@link Deque#offerLast}) |
517 |
|
|
* @since 1.6 |
518 |
|
|
*/ |
519 |
dl |
1.22 |
public boolean offerLast(E e) { |
520 |
|
|
addLast(e); |
521 |
dl |
1.17 |
return true; |
522 |
|
|
} |
523 |
|
|
|
524 |
|
|
/** |
525 |
dl |
1.19 |
* Retrieves, but does not remove, the first element of this list, |
526 |
|
|
* returning <tt>null</tt> if this list is empty. |
527 |
dl |
1.17 |
* |
528 |
dl |
1.19 |
* @return the first element of this list, or <tt>null</tt> if |
529 |
|
|
* this list is empty |
530 |
dl |
1.17 |
* @since 1.6 |
531 |
|
|
*/ |
532 |
|
|
public E peekFirst() { |
533 |
|
|
if (size==0) |
534 |
|
|
return null; |
535 |
dl |
1.18 |
return getFirst(); |
536 |
dl |
1.17 |
} |
537 |
|
|
|
538 |
|
|
/** |
539 |
dl |
1.19 |
* Retrieves, but does not remove, the last element of this list, |
540 |
|
|
* returning <tt>null</tt> if this list is empty. |
541 |
dl |
1.17 |
* |
542 |
dl |
1.19 |
* @return the last element of this list, or <tt>null</tt> if this list |
543 |
dl |
1.17 |
* is empty |
544 |
|
|
* @since 1.6 |
545 |
|
|
*/ |
546 |
|
|
public E peekLast() { |
547 |
|
|
if (size==0) |
548 |
|
|
return null; |
549 |
dl |
1.18 |
return getLast(); |
550 |
dl |
1.17 |
} |
551 |
|
|
|
552 |
|
|
/** |
553 |
dl |
1.19 |
* Retrieves and removes the first element of this list, or |
554 |
|
|
* <tt>null</tt> if this list is empty. |
555 |
dl |
1.17 |
* |
556 |
dl |
1.19 |
* @return the first element of this list, or <tt>null</tt> if |
557 |
|
|
* this list is empty |
558 |
dl |
1.17 |
* @since 1.6 |
559 |
|
|
*/ |
560 |
|
|
public E pollFirst() { |
561 |
|
|
if (size==0) |
562 |
|
|
return null; |
563 |
|
|
return removeFirst(); |
564 |
|
|
} |
565 |
|
|
|
566 |
|
|
/** |
567 |
dl |
1.19 |
* Retrieves and removes the last element of this list, or |
568 |
|
|
* <tt>null</tt> if this list is empty. |
569 |
dl |
1.17 |
* |
570 |
dl |
1.19 |
* @return the last element of this list, or <tt>null</tt> if |
571 |
|
|
* this list is empty |
572 |
dl |
1.17 |
* @since 1.6 |
573 |
|
|
*/ |
574 |
|
|
public E pollLast() { |
575 |
|
|
if (size==0) |
576 |
|
|
return null; |
577 |
|
|
return removeLast(); |
578 |
|
|
} |
579 |
|
|
|
580 |
|
|
/** |
581 |
dl |
1.19 |
* Pushes an element onto the stack represented by this list. In other |
582 |
dl |
1.22 |
* words, inserts the element at the front of this list. |
583 |
dl |
1.17 |
* |
584 |
|
|
* <p>This method is equivalent to {@link #addFirst}. |
585 |
|
|
* |
586 |
dl |
1.22 |
* @param e the element to push |
587 |
dl |
1.17 |
* @since 1.6 |
588 |
|
|
*/ |
589 |
dl |
1.22 |
public void push(E e) { |
590 |
|
|
addFirst(e); |
591 |
dl |
1.17 |
} |
592 |
|
|
|
593 |
|
|
/** |
594 |
dl |
1.19 |
* Pops an element from the stack represented by this list. In other |
595 |
dl |
1.20 |
* words, removes and returns the first element of this list. |
596 |
dl |
1.17 |
* |
597 |
|
|
* <p>This method is equivalent to {@link #removeFirst()}. |
598 |
|
|
* |
599 |
dl |
1.19 |
* @return the element at the front of this list (which is the top |
600 |
|
|
* of the stack represented by this list) |
601 |
jsr166 |
1.24 |
* @throws NoSuchElementException if this list is empty. |
602 |
dl |
1.17 |
* @since 1.6 |
603 |
|
|
*/ |
604 |
|
|
public E pop() { |
605 |
|
|
return removeFirst(); |
606 |
|
|
} |
607 |
|
|
|
608 |
|
|
/** |
609 |
|
|
* Removes the first occurrence of the specified element in this |
610 |
dl |
1.19 |
* list (when traversing the list from head to tail). If the list |
611 |
dl |
1.17 |
* does not contain the element, it is unchanged. |
612 |
|
|
* |
613 |
dl |
1.21 |
* @param o element to be removed from this list, if present |
614 |
dl |
1.19 |
* @return <tt>true</tt> if the list contained the specified element |
615 |
dl |
1.17 |
* @since 1.6 |
616 |
|
|
*/ |
617 |
dl |
1.21 |
public boolean removeFirstOccurrence(Object o) { |
618 |
|
|
return remove(o); |
619 |
dl |
1.17 |
} |
620 |
|
|
|
621 |
|
|
/** |
622 |
|
|
* Removes the last occurrence of the specified element in this |
623 |
dl |
1.19 |
* list (when traversing the list from head to tail). If the list |
624 |
dl |
1.17 |
* does not contain the element, it is unchanged. |
625 |
|
|
* |
626 |
dl |
1.19 |
* @param o element to be removed from this list, if present |
627 |
|
|
* @return <tt>true</tt> if the list contained the specified element |
628 |
dl |
1.17 |
* @since 1.6 |
629 |
|
|
*/ |
630 |
|
|
public boolean removeLastOccurrence(Object o) { |
631 |
|
|
if (o==null) { |
632 |
|
|
for (Entry e = header.previous; e != header; e = e.previous) { |
633 |
|
|
if (e.element==null) { |
634 |
|
|
remove(e); |
635 |
|
|
return true; |
636 |
|
|
} |
637 |
|
|
} |
638 |
|
|
} else { |
639 |
|
|
for (Entry e = header.previous; e != header; e = e.previous) { |
640 |
|
|
if (o.equals(e.element)) { |
641 |
|
|
remove(e); |
642 |
|
|
return true; |
643 |
|
|
} |
644 |
|
|
} |
645 |
|
|
} |
646 |
|
|
return false; |
647 |
|
|
} |
648 |
|
|
|
649 |
tim |
1.1 |
/** |
650 |
|
|
* Returns a list-iterator of the elements in this list (in proper |
651 |
|
|
* sequence), starting at the specified position in the list. |
652 |
|
|
* Obeys the general contract of <tt>List.listIterator(int)</tt>.<p> |
653 |
|
|
* |
654 |
|
|
* The list-iterator is <i>fail-fast</i>: if the list is structurally |
655 |
|
|
* modified at any time after the Iterator is created, in any way except |
656 |
|
|
* through the list-iterator's own <tt>remove</tt> or <tt>add</tt> |
657 |
|
|
* methods, the list-iterator will throw a |
658 |
|
|
* <tt>ConcurrentModificationException</tt>. Thus, in the face of |
659 |
|
|
* concurrent modification, the iterator fails quickly and cleanly, rather |
660 |
|
|
* than risking arbitrary, non-deterministic behavior at an undetermined |
661 |
|
|
* time in the future. |
662 |
|
|
* |
663 |
|
|
* @param index index of first element to be returned from the |
664 |
jsr166 |
1.14 |
* list-iterator (by a call to <tt>next</tt>). |
665 |
tim |
1.1 |
* @return a ListIterator of the elements in this list (in proper |
666 |
jsr166 |
1.14 |
* sequence), starting at the specified position in the list. |
667 |
jsr166 |
1.24 |
* @throws IndexOutOfBoundsException if the index is out of range |
668 |
|
|
* (<tt>index < 0 || index > size()</tt>). |
669 |
dl |
1.3 |
* @see List#listIterator(int) |
670 |
tim |
1.1 |
*/ |
671 |
dl |
1.3 |
public ListIterator<E> listIterator(int index) { |
672 |
jsr166 |
1.14 |
return new ListItr(index); |
673 |
tim |
1.1 |
} |
674 |
|
|
|
675 |
dl |
1.3 |
private class ListItr implements ListIterator<E> { |
676 |
jsr166 |
1.14 |
private Entry<E> lastReturned = header; |
677 |
|
|
private Entry<E> next; |
678 |
|
|
private int nextIndex; |
679 |
|
|
private int expectedModCount = modCount; |
680 |
|
|
|
681 |
|
|
ListItr(int index) { |
682 |
|
|
if (index < 0 || index > size) |
683 |
|
|
throw new IndexOutOfBoundsException("Index: "+index+ |
684 |
|
|
", Size: "+size); |
685 |
|
|
if (index < (size >> 1)) { |
686 |
|
|
next = header.next; |
687 |
|
|
for (nextIndex=0; nextIndex<index; nextIndex++) |
688 |
|
|
next = next.next; |
689 |
|
|
} else { |
690 |
|
|
next = header; |
691 |
|
|
for (nextIndex=size; nextIndex>index; nextIndex--) |
692 |
|
|
next = next.previous; |
693 |
|
|
} |
694 |
|
|
} |
695 |
|
|
|
696 |
|
|
public boolean hasNext() { |
697 |
|
|
return nextIndex != size; |
698 |
|
|
} |
699 |
|
|
|
700 |
|
|
public E next() { |
701 |
|
|
checkForComodification(); |
702 |
|
|
if (nextIndex == size) |
703 |
|
|
throw new NoSuchElementException(); |
704 |
|
|
|
705 |
|
|
lastReturned = next; |
706 |
|
|
next = next.next; |
707 |
|
|
nextIndex++; |
708 |
|
|
return lastReturned.element; |
709 |
|
|
} |
710 |
|
|
|
711 |
|
|
public boolean hasPrevious() { |
712 |
|
|
return nextIndex != 0; |
713 |
|
|
} |
714 |
|
|
|
715 |
|
|
public E previous() { |
716 |
|
|
if (nextIndex == 0) |
717 |
|
|
throw new NoSuchElementException(); |
718 |
|
|
|
719 |
|
|
lastReturned = next = next.previous; |
720 |
|
|
nextIndex--; |
721 |
|
|
checkForComodification(); |
722 |
|
|
return lastReturned.element; |
723 |
|
|
} |
724 |
|
|
|
725 |
|
|
public int nextIndex() { |
726 |
|
|
return nextIndex; |
727 |
|
|
} |
728 |
|
|
|
729 |
|
|
public int previousIndex() { |
730 |
|
|
return nextIndex-1; |
731 |
|
|
} |
732 |
jozart |
1.12 |
|
733 |
jsr166 |
1.14 |
public void remove() { |
734 |
tim |
1.1 |
checkForComodification(); |
735 |
jsr166 |
1.14 |
Entry<E> lastNext = lastReturned.next; |
736 |
tim |
1.1 |
try { |
737 |
|
|
LinkedList.this.remove(lastReturned); |
738 |
|
|
} catch (NoSuchElementException e) { |
739 |
|
|
throw new IllegalStateException(); |
740 |
|
|
} |
741 |
jsr166 |
1.14 |
if (next==lastReturned) |
742 |
|
|
next = lastNext; |
743 |
tim |
1.1 |
else |
744 |
jsr166 |
1.14 |
nextIndex--; |
745 |
|
|
lastReturned = header; |
746 |
|
|
expectedModCount++; |
747 |
|
|
} |
748 |
|
|
|
749 |
|
|
public void set(E o) { |
750 |
|
|
if (lastReturned == header) |
751 |
|
|
throw new IllegalStateException(); |
752 |
|
|
checkForComodification(); |
753 |
|
|
lastReturned.element = o; |
754 |
|
|
} |
755 |
|
|
|
756 |
|
|
public void add(E o) { |
757 |
|
|
checkForComodification(); |
758 |
|
|
lastReturned = header; |
759 |
|
|
addBefore(o, next); |
760 |
|
|
nextIndex++; |
761 |
|
|
expectedModCount++; |
762 |
|
|
} |
763 |
|
|
|
764 |
|
|
final void checkForComodification() { |
765 |
|
|
if (modCount != expectedModCount) |
766 |
|
|
throw new ConcurrentModificationException(); |
767 |
|
|
} |
768 |
dl |
1.3 |
} |
769 |
|
|
|
770 |
|
|
private static class Entry<E> { |
771 |
jsr166 |
1.14 |
E element; |
772 |
|
|
Entry<E> next; |
773 |
|
|
Entry<E> previous; |
774 |
|
|
|
775 |
|
|
Entry(E element, Entry<E> next, Entry<E> previous) { |
776 |
|
|
this.element = element; |
777 |
|
|
this.next = next; |
778 |
|
|
this.previous = previous; |
779 |
|
|
} |
780 |
dl |
1.3 |
} |
781 |
|
|
|
782 |
|
|
private Entry<E> addBefore(E o, Entry<E> e) { |
783 |
jsr166 |
1.14 |
Entry<E> newEntry = new Entry<E>(o, e, e.previous); |
784 |
|
|
newEntry.previous.next = newEntry; |
785 |
|
|
newEntry.next.previous = newEntry; |
786 |
|
|
size++; |
787 |
|
|
modCount++; |
788 |
|
|
return newEntry; |
789 |
|
|
} |
790 |
|
|
|
791 |
|
|
private E remove(Entry<E> e) { |
792 |
|
|
if (e == header) |
793 |
|
|
throw new NoSuchElementException(); |
794 |
|
|
|
795 |
|
|
E result = e.element; |
796 |
|
|
e.previous.next = e.next; |
797 |
|
|
e.next.previous = e.previous; |
798 |
|
|
e.next = e.previous = null; |
799 |
|
|
e.element = null; |
800 |
|
|
size--; |
801 |
|
|
modCount++; |
802 |
|
|
return result; |
803 |
tim |
1.1 |
} |
804 |
|
|
|
805 |
|
|
/** |
806 |
|
|
* Returns a shallow copy of this <tt>LinkedList</tt>. (The elements |
807 |
|
|
* themselves are not cloned.) |
808 |
|
|
* |
809 |
|
|
* @return a shallow copy of this <tt>LinkedList</tt> instance. |
810 |
|
|
*/ |
811 |
|
|
public Object clone() { |
812 |
dl |
1.3 |
LinkedList<E> clone = null; |
813 |
jsr166 |
1.14 |
try { |
814 |
|
|
clone = (LinkedList<E>) super.clone(); |
815 |
|
|
} catch (CloneNotSupportedException e) { |
816 |
|
|
throw new InternalError(); |
817 |
|
|
} |
818 |
tim |
1.1 |
|
819 |
|
|
// Put clone into "virgin" state |
820 |
dl |
1.3 |
clone.header = new Entry<E>(null, null, null); |
821 |
tim |
1.1 |
clone.header.next = clone.header.previous = clone.header; |
822 |
|
|
clone.size = 0; |
823 |
|
|
clone.modCount = 0; |
824 |
|
|
|
825 |
|
|
// Initialize clone with our elements |
826 |
dl |
1.3 |
for (Entry<E> e = header.next; e != header; e = e.next) |
827 |
tim |
1.1 |
clone.add(e.element); |
828 |
|
|
|
829 |
|
|
return clone; |
830 |
|
|
} |
831 |
|
|
|
832 |
|
|
/** |
833 |
|
|
* Returns an array containing all of the elements in this list |
834 |
|
|
* in the correct order. |
835 |
|
|
* |
836 |
|
|
* @return an array containing all of the elements in this list |
837 |
jsr166 |
1.14 |
* in the correct order. |
838 |
tim |
1.1 |
*/ |
839 |
|
|
public Object[] toArray() { |
840 |
jsr166 |
1.14 |
Object[] result = new Object[size]; |
841 |
tim |
1.1 |
int i = 0; |
842 |
dl |
1.3 |
for (Entry<E> e = header.next; e != header; e = e.next) |
843 |
tim |
1.1 |
result[i++] = e.element; |
844 |
jsr166 |
1.14 |
return result; |
845 |
tim |
1.1 |
} |
846 |
|
|
|
847 |
|
|
/** |
848 |
|
|
* Returns an array containing all of the elements in this list in |
849 |
|
|
* the correct order; the runtime type of the returned array is that of |
850 |
|
|
* the specified array. If the list fits in the specified array, it |
851 |
|
|
* is returned therein. Otherwise, a new array is allocated with the |
852 |
|
|
* runtime type of the specified array and the size of this list.<p> |
853 |
|
|
* |
854 |
|
|
* If the list fits in the specified array with room to spare |
855 |
|
|
* (i.e., the array has more elements than the list), |
856 |
|
|
* the element in the array immediately following the end of the |
857 |
|
|
* collection is set to null. This is useful in determining the length |
858 |
|
|
* of the list <i>only</i> if the caller knows that the list |
859 |
|
|
* does not contain any null elements. |
860 |
|
|
* |
861 |
|
|
* @param a the array into which the elements of the list are to |
862 |
jsr166 |
1.14 |
* be stored, if it is big enough; otherwise, a new array of the |
863 |
|
|
* same runtime type is allocated for this purpose. |
864 |
tim |
1.1 |
* @return an array containing the elements of the list. |
865 |
|
|
* @throws ArrayStoreException if the runtime type of a is not a |
866 |
|
|
* supertype of the runtime type of every element in this list. |
867 |
|
|
* @throws NullPointerException if the specified array is null. |
868 |
|
|
*/ |
869 |
dl |
1.3 |
public <T> T[] toArray(T[] a) { |
870 |
tim |
1.1 |
if (a.length < size) |
871 |
dl |
1.3 |
a = (T[])java.lang.reflect.Array.newInstance( |
872 |
tim |
1.1 |
a.getClass().getComponentType(), size); |
873 |
|
|
int i = 0; |
874 |
jsr166 |
1.14 |
Object[] result = a; |
875 |
dl |
1.3 |
for (Entry<E> e = header.next; e != header; e = e.next) |
876 |
|
|
result[i++] = e.element; |
877 |
tim |
1.1 |
|
878 |
|
|
if (a.length > size) |
879 |
|
|
a[size] = null; |
880 |
|
|
|
881 |
|
|
return a; |
882 |
|
|
} |
883 |
|
|
|
884 |
|
|
private static final long serialVersionUID = 876323262645176354L; |
885 |
|
|
|
886 |
|
|
/** |
887 |
|
|
* Save the state of this <tt>LinkedList</tt> instance to a stream (that |
888 |
|
|
* is, serialize it). |
889 |
|
|
* |
890 |
|
|
* @serialData The size of the list (the number of elements it |
891 |
jsr166 |
1.14 |
* contains) is emitted (int), followed by all of its |
892 |
dl |
1.3 |
* elements (each an Object) in the proper order. |
893 |
tim |
1.1 |
*/ |
894 |
dl |
1.3 |
private void writeObject(java.io.ObjectOutputStream s) |
895 |
tim |
1.1 |
throws java.io.IOException { |
896 |
jsr166 |
1.14 |
// Write out any hidden serialization magic |
897 |
|
|
s.defaultWriteObject(); |
898 |
tim |
1.1 |
|
899 |
|
|
// Write out size |
900 |
|
|
s.writeInt(size); |
901 |
|
|
|
902 |
jsr166 |
1.14 |
// Write out all elements in the proper order. |
903 |
tim |
1.1 |
for (Entry e = header.next; e != header; e = e.next) |
904 |
|
|
s.writeObject(e.element); |
905 |
|
|
} |
906 |
|
|
|
907 |
|
|
/** |
908 |
|
|
* Reconstitute this <tt>LinkedList</tt> instance from a stream (that is |
909 |
|
|
* deserialize it). |
910 |
|
|
*/ |
911 |
dl |
1.3 |
private void readObject(java.io.ObjectInputStream s) |
912 |
tim |
1.1 |
throws java.io.IOException, ClassNotFoundException { |
913 |
jsr166 |
1.14 |
// Read in any hidden serialization magic |
914 |
|
|
s.defaultReadObject(); |
915 |
tim |
1.1 |
|
916 |
|
|
// Read in size |
917 |
|
|
int size = s.readInt(); |
918 |
|
|
|
919 |
|
|
// Initialize header |
920 |
dl |
1.3 |
header = new Entry<E>(null, null, null); |
921 |
tim |
1.1 |
header.next = header.previous = header; |
922 |
|
|
|
923 |
jsr166 |
1.14 |
// Read in all elements in the proper order. |
924 |
|
|
for (int i=0; i<size; i++) |
925 |
dl |
1.3 |
addBefore((E)s.readObject(), header); |
926 |
tim |
1.1 |
} |
927 |
|
|
} |