Hello,
I'm having trouble translating an algorithm. I'm not sure I coded bits of this correctly, but I'm certain that I have no idea how to compare an item with a node identifier (cannot apply > operator with T,T) but since this is an insert sorted method rather than insert from back or front some sort of compare (compareTo()?) is necessary to establish a sorting procedure, yes? I guess what I'm asking is the proper syntax for said code. Thanks, any help is appreciated.
package ListPkg;
import ExceptionPkg.InvalidListOperationException;
public class SortedLinkedList <T extends Comparable<T>> extends LinkedList<T>
{
public SortedLinkedList()
{
super();
}
public SortedLinkedList(String listName)
{
super(listName);
}
public void insertAtFront(T item) throws InvalidListOperationException
{
throw new InvalidListOperationException("Insert at front of list is not allowed");
}
public void insertAtBack(T item) throws InvalidListOperationException
{
throw new InvalidListOperationException("Insert at back of list is not allowed");
}
public T insertSorted(T item)
{
ListNode<T> newNode;
if(isEmpty())
{
// data = item;
// nextNode = null;
newNode = new ListNode<T>(item,null);
firstNode = lastNode = newNode;
}
else
{
ListNode<T> current = firstNode;
if(item < firstNode.data) //here's a problem
{
super.insertAtFront(item);
}
else
{
current = firstNode;
boolean found = false;
while(current.nextNode != null && found == false)
{
if(item > current.nextNode) //here's another
{
current = current.nextNode;
}
else
{
found = true;
}
}
if(found == true)
{
item = current.nextNode;
}
else
{
super.insertAtBack(item);
}
}
}
}
}