سؤال

I am new to Java and trying to implement MyLinkedList which extends the GeneralList interface, I want to use the comparable interface for my Node to keep my list sorted, When I try to create a head node it is giving me errors

Please find the error message below the following code

//List interface

public interface GeneralList<T> 
{
    public boolean addNode(T elem);
    public boolean deleteNode(T elem);
    public T containsNode(T elem);
    public void printSll();

} 

//ListImplementation

public class SLL2<T> implements GeneralList<T> 
{   
    private static class Node<T extends Comparable<T>> 
    {
        public T data;
        public Node<T> next;

        public Node() 
        {
            data = null;
            next = null;
        }
    }   

    public SLL2()
    {
        head = null;        
    }

    /* 1. Error while creating a head referance*/
    private Node<T> head;  

    @Override
    public boolean addNode(T elem) 
    {
        Node<T> tmp = new Node<T>();
        tmp.data = elem;

        if(head == null)
        {
            head = temp;
                        return true;
        }
        else
        {
              for(Node<T> cur = head; cur.next != null ; cur= cur.next) 
          {
                    /* iterate and add the node */
                    if(temp.elem.comparTo(cur.elem)) 
                    {
                    }

                }
           }
}

1. I am not able to create the head node with the declaration private Node<T> head;

It is giving error "Bound mismatch: The type T is not a valid substitute for the bounded parameter <T extends Comparable<T>> of the type SLL2<T>.Node<T>"

Please help me to resolve this error...
هل كانت مفيدة؟

المحلول

Your class SLL2<T> should also have a constraint about comparability of T. Like that:

public class SLL2<T extends Comparable<T>> implements GeneralList<T> {
    // ...
مرخصة بموجب: CC-BY-SA مع الإسناد
لا تنتمي إلى StackOverflow
scroll top