My Linked List utilizes variables that are not predefined, but I only learned to use these today. I have set up the list to utilize simple integers but I am realizing that I cannot compare my variables the same way because they are not defined as integers, thus I am receiving a syntax error on my if statements which are trying to compare the integer values from the linked list nodes to find the largest and smallest in the list.
I've marked the if statements as errors. Please tell me if any more code or information is needed.
package lab01Pkg;
public class LinkedList <T> //<T> is defining the type of node we are creating
{
private int size;
private Node<T> head;
private Node<T> tail;
public LinkedList()
{
size = 0;
head = null;
tail = null;
}
public void addToFront( T theData ) //Must pass in the type of variable the list knows how to hold.
{
Node<T> tempNode = head;
head = new Node<T>( theData, tempNode );
size++;
}
public void addLast( T theData )
{
if ( isEmpty() )
{
addToFront(theData);
}
else
{
Node<T> tempNode = head;
while ( tempNode.getLink() != null )
{
tempNode = tempNode.getLink();
}
Node<T> newNode = new Node<T>( theData, null );
tempNode.setLink(newNode);
size++;
}
}
public T min()
{
if (size == 0)
{
return null;
}
Node<T> tempNode = head;
T valueHold = tempNode.getData();
while ( tempNode.getLink() != null )
{
**if (tempNode.getData() < valueHold)** //Error Here
{
valueHold = tempNode.getData();
}
tempNode = tempNode.getLink();
}
return valueHold;
}
public T max()
{
if (size == 0)
{
return null;
}
Node<T> tempNode = head;
T valueHold = tempNode.getData();
while ( tempNode.getLink() != null )
{
**if (tempNode.getData() > valueHold)** //Error here
{
valueHold = tempNode.getData();
}
tempNode = tempNode.getLink();
}
return valueHold;
}
public void removeLast()
{
Node<T> tempNode = head;
while (tempNode.getLink() != null)
{
tempNode = tempNode.getLink();
}
tempNode = null;
}
public boolean isEmpty() //Returns true or false based on if list is empty or not.
{
return size == 0;
}
public String toString()
{
Node<T> tempNode = head;
String theResult = "";
while ( tempNode != null)
{
theResult = theResult + tempNode.getData().toString();
tempNode = tempNode.getLink();
}
return theResult;
}
}