Java Reference
In-Depth Information
1 package weiss.nonstandard;
2
3 // SortedLinkedList class
4 //
5 // CONSTRUCTION: with no initializer
6 // Access is via LinkedListIterator class
7 //
8 // ******************PUBLIC OPERATIONS*********************
9 // void insert( x ) --> Insert x
10 // void insert( x, p ) --> Insert x (ignore p)
11 // All other LinkedList operations
12 // ******************ERRORS********************************
13 // No special errors
14
15 public class SortedLinkedList<AnyType extends Comparable<? super AnyType>>
16 extends LinkedList<AnyType>
17 {
18 /**
19 * Insert after p.
20 * @param x the item to insert.
21 * @param p this parameter is ignored.
22 */
23 public void insert( AnyType x, LinkedListIterator<AnyType> p )
24 {
25 insert( x );
26 }
27
28 /**
29 * Insert in sorted order.
30 * @param x the item to insert.
31 */
32 public void insert( AnyType x )
33 {
34 LinkedListIterator<AnyType> prev = zeroth( );
35 LinkedListIterator<AnyType> curr = first( );
36
37 while( curr.isValid( ) && x.compareTo( curr.retrieve( ) ) > 0 )
38 {
39 prev.advance( );
40 curr.advance( );
41 }
42
43 super.insert( x, prev );
44 }
45 }
figure 17.19
The SortedLinkedList class, in which insertions are restricted to sorted order
 
Search WWH ::




Custom Search