I wigged and I wagged....here's the finished code, so when someone asks How do I compare a generic node with a generic object you can refer them to this post.
public class ListNode<T> implements Comparable<T> {
T data;
ListNode< T > nextNode;
ListNode(T object){
this(object, null);
}
ListNode(T object, ListNode<T> node){
data = object;
nextNode = node;
}
T getData(){
return data;
}
ListNode<T> getNext(){
return nextNode;
}
public int compareTo(T arg0) {
return ((Comparable<T>) data).compareTo(arg0);
}
}
public static void main(String[] args){
List<Integer> list = new List<Integer>();
list.insertAtFront(0);
list.print();
list.insertAtFront(-1);
list.print();
list.insertAtBack(1);
list.print();
list.insertAtBack(4);
list.insertAtBack(5);
list.print();
list.insertAfter(3);
list.print();
try{
int removedItem = list.removeFromFront();
System.out.printf("\n%d removed \n", removedItem);
list.print();
removedItem = list.removeFromFront();
System.out.printf("\n%d removed \n", removedItem);
list.print();
removedItem = list.removeFromBack();
System.out.printf("\n%d removed \n", removedItem);
list.print();
removedItem = list.removeFromBack();
System.out.printf("\n%d removed \n", removedItem);
list.print();
}catch(EmptyListException e){
e.printStackTrace();
}
}
}
@SuppressWarnings("serial")
public class EmptyListException extends RuntimeException {
public EmptyListException(){
this("list");
}
public EmptyListException( String name){
super(name+ " is empty");
}
}
public class List<T> {
private ListNode<T> firstNode;
private ListNode<T> lastNode;
private String name;
public List(){
this("list");
}
public List(String listName){
name = listName;
firstNode = lastNode = null;
}
public void insertAfter(T insertItem) {
ListNode<T> lastTemp = firstNode;
ListNode<T> current = firstNode;
if(isEmpty()){
firstNode = lastNode = new ListNode<T>(insertItem); //firstnode = lastnode because the list is empty.
}else{
while(current != null){
System.out.println("loop through list, comparison int return = : " + current.compareTo(insertItem) + " current item: " + current.data + " item to insert: " + insertItem);
if(current.compareTo(insertItem) > 0){
System.out.println("Compared object was greater and will now insert");
if(current != firstNode){
if(current.nextNode != null)
lastTemp.nextNode = new ListNode<T>(insertItem, current);
else
current.nextNode = lastNode = new ListNode<T>(insertItem);
}else
firstNode = new ListNode<T>(insertItem, firstNode);
break;
}
lastTemp = current;
current = current.nextNode;
}
}
}
public void insertAtFront(T insertItem){
if(isEmpty()){
firstNode = lastNode = new ListNode<T>(insertItem); //firstnode = lastnode because the list is empty.
}else
firstNode = new ListNode<T>(insertItem, firstNode);
}
public void insertAtBack(T insertItem){
if(isEmpty()){
firstNode = lastNode = new ListNode<T>(insertItem); //firstnode = lastnode because the list is empty.
}else
lastNode = lastNode.nextNode = new ListNode<T>(insertItem);
}
public T removeFromFront() throws EmptyListException{
if(isEmpty())
throw new EmptyListException(name);
T removedItem = firstNode.data;
if(firstNode == lastNode)
firstNode = lastNode = null;
else
firstNode = firstNode.nextNode;
return removedItem;
}
public T removeFromBack() throws EmptyListException{
if(isEmpty())
throw new EmptyListException(name);
//if no exception is thrown
T removedItem = lastNode.data;
if(firstNode == lastNode)
firstNode = lastNode = null;
else{
ListNode<T> current = firstNode;
while(current.nextNode != lastNode)
current = current.nextNode;
lastNode = current;
current.nextNode = null;
}
return removedItem;
}
public boolean isEmpty(){
return firstNode == null;
}
public void print(){
if(isEmpty()){
System.out.printf("Empty %s\n", name);
return;
}
System.out.printf("The %s is: ", name);
ListNode<T> current = firstNode;
while(current!= null){
System.out.printf("%s ", current.data);
current = current.nextNode;
}
System.out.printf("\n");
}
}