Java Reference
In-Depth Information
if (l== null )
return null ;
else
return new ListString( l .name,copyRec( l .next)) ;
}
7.5 Creating linked lists from arrays
So far we have handled large (static) data-sets into arrays that are contiguously
stored in the global memory. It is useful to start to build linked lists from arrays
that will support editing operations. Therefore, we create the helper function,
which creates a linked list from a String array as follows:
static ListString Build(String [] array)
{ ListString result= null ;
// To ensure that list head is the first array element
decrement
// Iterate from largest to smallest index
for ( int i=array . length 1;i > =0;i −− )
result= new ListString(array [ i ] , result ) ;
return result ;
}
Consider, for example, the following code snippet that shows how to use the
function Build :
String [] colors= { "green" , "red" , "blue" , "purple" , "orange" ,
"yellow" } ;
ListString lColors=ListString . Build( colors) ;
ListString . Display(lColors) ;
The result displayed on the output console by executing the above instructions
is:
green-->red-->blue-->purple-->orange-->yellow-->null
7.6 Sorting linked lists
Given an arbitrarily ordered linked list, we wish to sort the list elements, say
in increasing order. We must first consider a simpler problem that consists of
 
Search WWH ::




Custom Search