Olá, boa tarde. Tenho esta classe:
public static void main( String[] args )
{
List< Integer > list = new List< Integer >(); // create a List
// insert integers in list
list.insertAtFront(8); // inclui um número na frente
list.print();
e esta:
class ListNode< T >
{
T data; // data for this node
ListNode< T > nextNode; // reference to the next node in the list
// constructor creates a ListNode that refers to object
ListNode( T object )
{
this( object, null );
} // end ListNode one-argument constructor
// constructor creates ListNode that refers to the specified
// object and to the next ListNode
ListNode( T object, ListNode< T > node )
{
data = object;
nextNode = node;
} // end ListNode two-argument constructor
// return reference to data in node
T getData()
{
return data; // return item in this node
} // end method getData
// return reference to next node in list
ListNode< T > getNext()
{
return nextNode; // get next node
} // end method getNext
} // end class ListNode< T >
// class List definition
public class List< T >
{
private ListNode< T > firstNode;
private ListNode< T > lastNode;
private String name; // string like "list" used in printing
// constructor creates empty List with "list" as the name
public List()
{
this( "list" );
} // end List no-argument constructor
// constructor creates an empty List with a name
public List( String listName )
{
name = listName;
firstNode = lastNode = null;
} // end List one-argument constructor
// insert item at front of List
public void insertAtFront( T insertItem )
{
if ( isEmpty() ) // firstNode and lastNode refer to same object
firstNode = lastNode = new ListNode< T >( insertItem );
else // firstNode refers to new node
firstNode = new ListNode< T >( insertItem, firstNode );
} // end method insertAtFront
} // end method isEmpty
// output list contents
public void print()
{
if ( isEmpty() )
{
System.out.printf( "Empty %s\n", name );
return;
} // end if
System.out.printf( "The %s is: ", name );
ListNode< T > current = firstNode;
// while not at end of list, output current node's data
while ( current != null )
{
System.out.printf( "%s ", current.data );
current = current.nextNode;
} // end while
System.out.println();
} // end method print
} // end class List< T >
Eu preciso criar um método de inserção de dados ordenados nesta última classe sem usar o Collections.sort (tipo o insertAtFront). Ele tem ainda que usar um array pq serão vários dados. Alguma ajuda?