public void insert(int item)
{
if (head == null)
head = new Node(item,null);
else if (item < head. info)
head = new Node(item,head);
else
{
// Locate insertion point
Node current = head;
boolean located = false;
while (!located && current.link != null)
if (item < current.link.info)
located = true;
else
current = current.link;
// Perform insertion
if (located) // Normal insertion
{
Node temp = new Node(current.info,current.link);
current.info = item;
current.link = temp;
}
else // Insert at end of list
current.link = new Node(item,null);
}
}
|