0

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;
    }

}
3
  • 1
    To compare generic types you need to enforce them to implement comparable or alternatively enforce the data structure to receive a comparison method to be used. Commented Sep 14, 2018 at 4:53
  • Since you are using Generics, the List<T> is unable to determine the type. You will need to use Comparator<T> and use compareTo() method to do the comparisons. Commented Sep 14, 2018 at 4:56
  • Use the Integer wrapper class rather than the simple int type. Integer implements the comparable interface. Then in your min() function you can use the .compareTo method. Check out this q/a stackoverflow.com/questions/21544716/… Commented Sep 14, 2018 at 4:57

1 Answer 1

2

To achieve what you want, you need to use the Comparable interface. Read this article to get an idea on Comparable interface.

Then you need make your class definition to have a generic type which implements Comparable interface.

public class LinkedList <T extends Comparable<T>> { ... 

And you need to make sure the objects you are planning to add to this list are implementing Comparable interface. Usually java inbuilt classes like Integer, Long have implemented this. If you are using custom objects, make sure you implement that interface in those classes.

Then, instead of < and > marks, use the compareTo method within if conditions. For example:

if (tempNode.getData().compareTo(valueHold) > 0) // tempNode is larger

I have just outlined what you need to do. Hope you got the idea. Read this for further readings on Comparable usage with Generics.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.