// Clear the list public void clear() { headPtr = null; }
All you need to do is unlink the first node of the chain. This makes all nodes orphans. The garbage collector recycles all the orphan nodes.
get()
The get(int index)
method returns the value of a node at a particular index.
This method is the same as with the unordered linked list.
// get the value at index of the list // int get( int index ) throws IndexOutOfBoundsException { if ( isEmpty() ) throw new IndexOutOfBoundsException(); Node p = headPtr; int counter = 0; while ( counter<index && p != null ) { p = p.getNext(); counter++ ; } if ( index==counter ) return p.getValue(); else throw new IndexOutOfBoundsException(); }
(Design Question: ) Consider a delete( int value )
method, that unlinks the node (if any)
that contains value
.
value
is not in the list?value
?current
and next
) needed?