Java Reference
In-Depth Information
The remove methods are shown in Figures 17.27 and 17.28, and those fun-
nel through a private remove method, shown at lines 40-48 (in Figure 17.27),
that mimics the algorithm in Section 17.3.
1 /**
2 * Removes the first item in the list.
3 * @return the item was removed from the collection.
4 * @throws NoSuchElementException if the list is empty.
5 */
6 public AnyType removeFirst( )
7 {
8 if( isEmpty( ) )
9 throw new NoSuchElementException( );
10 return remove( getNode( 0 ) );
11 }
12
13 /**
14 * Removes the last item in the list.
15 * @return the item was removed from the collection.
16 * @throws NoSuchElementException if the list is empty.
17 */
18 public AnyType removeLast( )
19 {
20 if( isEmpty( ) )
21 throw new NoSuchElementException( );
22 return remove( getNode( size( ) - 1 ) );
23 }
24
25 /**
26 * Removes an item from this collection.
27 * @param idx the index of the object.
28 * @return the item that was removed from the collection.
29 */
30 public AnyType remove( int idx )
31 {
32 return remove( getNode( idx ) );
33 }
34
35 /**
36 * Removes the object contained in Node p.
37 * @param p the Node containing the object.
38 * @return the item that was removed from the collection.
39 */
40 private AnyType remove( Node<AnyType> p )
41 {
42 p.next.prev = p.prev;
43 p.prev.next = p.next;
44 theSize--;
45 modCount++;
46
47 return p.data;
48 }
figure 17.27
remove methods for
standard LinkedList
class
Search WWH ::




Custom Search