1 |
|
/* |
2 |
< |
* @(#)LinkedList.java 1.53 03/06/22 |
2 |
> |
* %W% %E% |
3 |
|
* |
4 |
< |
* Copyright 2003 Sun Microsystems, Inc. All rights reserved. |
4 |
> |
* Copyright 2004 Sun Microsystems, Inc. All rights reserved. |
5 |
|
* SUN PROPRIETARY/CONFIDENTIAL. Use is subject to license terms. |
6 |
|
*/ |
7 |
|
|
25 |
|
* |
26 |
|
* All of the operations perform as could be expected for a doubly-linked |
27 |
|
* list. Operations that index into the list will traverse the list from |
28 |
< |
* the begining or the end, whichever is closer to the specified index.<p> |
28 |
> |
* the beginning or the end, whichever is closer to the specified index.<p> |
29 |
|
* |
30 |
|
* <b>Note that this implementation is not synchronized.</b> If multiple |
31 |
|
* threads access a list concurrently, and at least one of the threads |
62 |
|
* Java Collections Framework</a>. |
63 |
|
* |
64 |
|
* @author Josh Bloch |
65 |
< |
* @version 1.53, 06/22/03 |
65 |
> |
* @version %I%, %G% |
66 |
|
* @see List |
67 |
|
* @see ArrayList |
68 |
|
* @see Vector |
69 |
|
* @see Collections#synchronizedList(List) |
70 |
|
* @since 1.2 |
71 |
+ |
* @param <E> the type of elements held in this collection |
72 |
|
*/ |
73 |
|
|
74 |
|
public class LinkedList<E> |
131 |
|
* @throws NoSuchElementException if this list is empty. |
132 |
|
*/ |
133 |
|
public E removeFirst() { |
134 |
< |
E first = header.next.element; |
134 |
< |
remove(header.next); |
135 |
< |
return first; |
134 |
> |
return remove(header.next); |
135 |
|
} |
136 |
|
|
137 |
|
/** |
141 |
|
* @throws NoSuchElementException if this list is empty. |
142 |
|
*/ |
143 |
|
public E removeLast() { |
144 |
< |
E last = header.previous.element; |
146 |
< |
remove(header.previous); |
147 |
< |
return last; |
144 |
> |
return remove(header.previous); |
145 |
|
} |
146 |
|
|
147 |
|
/** |
285 |
|
* Removes all of the elements from this list. |
286 |
|
*/ |
287 |
|
public void clear() { |
288 |
< |
modCount++; |
288 |
> |
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 |
|
header.next = header.previous = header; |
296 |
< |
size = 0; |
296 |
> |
size = 0; |
297 |
> |
modCount++; |
298 |
|
} |
299 |
|
|
300 |
|
|
306 |
|
* @param index index of element to return. |
307 |
|
* @return the element at the specified position in this list. |
308 |
|
* |
309 |
< |
* @throws IndexOutOfBoundsException if the specified index is is out of |
309 |
> |
* @throws IndexOutOfBoundsException if the specified index is out of |
310 |
|
* range (<tt>index < 0 || index >= size()</tt>). |
311 |
|
*/ |
312 |
|
public E get(int index) { |
357 |
|
* range (<tt>index < 0 || index >= size()</tt>). |
358 |
|
*/ |
359 |
|
public E remove(int index) { |
360 |
< |
Entry<E> e = entry(index); |
357 |
< |
remove(e); |
358 |
< |
return e.element; |
360 |
> |
return remove(entry(index)); |
361 |
|
} |
362 |
|
|
363 |
|
/** |
444 |
|
// Queue operations. |
445 |
|
|
446 |
|
/** |
447 |
< |
* Retrieves, but does not remove, the head (first element) of this list.. |
447 |
> |
* Retrieves, but does not remove, the head (first element) of this list. |
448 |
|
* @return the head of this queue, or <tt>null</tt> if this queue is empty. |
449 |
+ |
* @since 1.5 |
450 |
|
*/ |
451 |
|
public E peek() { |
452 |
|
if (size==0) |
455 |
|
} |
456 |
|
|
457 |
|
/** |
458 |
< |
* Retrieves, but does not remove, the head (first element) of this list.. |
458 |
> |
* Retrieves, but does not remove, the head (first element) of this list. |
459 |
|
* @return the head of this queue. |
460 |
|
* @throws NoSuchElementException if this queue is empty. |
461 |
+ |
* @since 1.5 |
462 |
|
*/ |
463 |
|
public E element() { |
464 |
|
return getFirst(); |
465 |
|
} |
466 |
|
|
467 |
|
/** |
468 |
< |
* Retrieves and removes the head (first element) of this list.. |
468 |
> |
* Retrieves and removes the head (first element) of this list. |
469 |
|
* @return the head of this queue, or <tt>null</tt> if this queue is empty. |
470 |
+ |
* @since 1.5 |
471 |
|
*/ |
472 |
|
public E poll() { |
473 |
|
if (size==0) |
476 |
|
} |
477 |
|
|
478 |
|
/** |
479 |
< |
* Retrieves and removes the head (first element) of this list.. |
479 |
> |
* Retrieves and removes the head (first element) of this list. |
480 |
|
* @return the head of this queue. |
481 |
|
* @throws NoSuchElementException if this queue is empty. |
482 |
+ |
* @since 1.5 |
483 |
|
*/ |
484 |
|
public E remove() { |
485 |
|
return removeFirst(); |
491 |
|
* @param o the element to add. |
492 |
|
* @return <tt>true</tt> (as per the general contract of |
493 |
|
* <tt>Queue.offer</tt>) |
494 |
+ |
* @since 1.5 |
495 |
|
*/ |
496 |
< |
public boolean offer(E x) { |
497 |
< |
return add(x); |
496 |
> |
public boolean offer(E o) { |
497 |
> |
return add(o); |
498 |
|
} |
499 |
|
|
500 |
|
/** |
583 |
|
|
584 |
|
public void remove() { |
585 |
|
checkForComodification(); |
586 |
+ |
Entry<E> lastNext = lastReturned.next; |
587 |
|
try { |
588 |
|
LinkedList.this.remove(lastReturned); |
589 |
|
} catch (NoSuchElementException e) { |
590 |
|
throw new IllegalStateException(); |
591 |
|
} |
592 |
|
if (next==lastReturned) |
593 |
< |
next = lastReturned.next; |
593 |
> |
next = lastNext; |
594 |
|
else |
595 |
|
nextIndex--; |
596 |
|
lastReturned = header; |
639 |
|
return newEntry; |
640 |
|
} |
641 |
|
|
642 |
< |
private void remove(Entry<E> e) { |
642 |
> |
private E remove(Entry<E> e) { |
643 |
|
if (e == header) |
644 |
|
throw new NoSuchElementException(); |
645 |
|
|
646 |
+ |
E result = e.element; |
647 |
|
e.previous.next = e.next; |
648 |
|
e.next.previous = e.previous; |
649 |
+ |
e.next = e.previous = null; |
650 |
+ |
e.element = null; |
651 |
|
size--; |
652 |
|
modCount++; |
653 |
+ |
return result; |
654 |
|
} |
655 |
|
|
656 |
|
/** |