'Generic type error: argument lists differ in length

I'm stuck with a linked list implementation where a Node class and a list interface using generic types are required. The LinkedList class should implement the list interface and consist of Node objects. However, I have the following error: Executing command: javac -d . LinkedList.java

LinkedList.java:31: error: constructor Node in class Node cannot be applied to given types;
Node temp = new Node(data,null);
^
required: Object
found: T#1,
reason: actual and formal argument lists differ in length
where T#1,T#2 are type-variables:
T#1 extends List declared in class LinkedList
T#2 extends Object declared in class Node
LinkedList.java:41: error: constructor Node in class Node cannot be applied to given types;

I also cannot figure out why the compiler does not like: public class LinkedList<T extends Node<T> & List<T>>?

public class Node<T> {
  private T data;
  private Node<T> next;

  private Node(T data, Node<T> next) {
    this.data = data;
    this.next = next;
  }

  private Node(T data) {
    this(data,null);
  }

  public T getData() {
    return data;
  }

  public void setData(T data) {
    this.data = data;
  }

  public Node<T> getNext() {
    return next;
  }

  public void setNext(Node<T> next) {
    this.next = next;
  }
}

The List interface:

public interface List<T> {

    void addAtIndex(T data, int index);

    T getAtIndex(int index);

    T removeAtIndex(int index);

    T remove(T data);

    void clear();

    boolean isEmpty();

    int size();

}

LinkedList class:

import java.util.NoSuchElementException;

public class LinkedList<T extends List<T>> {
   private Node<T> head;
   private Node<T> tail;
   private int size;

   LinkedList() {
      this.head = null;
      this.tail = null;
      this.size = 0;
  }

  public Node<T> getHead() {
    return head;
  }

  public Node<T> getTail() {
    return tail;
  }

  public void addAtIndex(T data, int index) {
    if ((index < 0) || (index >= size)) {
      throw new IllegalArgumentException("Your index is out of the list bounds");
    }
    else if (data == null) {
      throw new IllegalArgumentException("You cannot add null data to the list");
    }
    else {
      if (index == 0) {
        Node<T> temp = new Node(data,null);
        temp.setNext(head);
        head = temp;
        size ++;
      }
      else if (index < (size-1) && index > 0){
        Node<T> current = head;
        for (int i = 0; i < index; i++) {
          current = current.getNext();
        }
        Node<T> temp = new Node(data,null);
        temp.setNext(current.getNext());
        current.setNext(temp);
        size ++;
      }
      else {
          Node<T> current = head;
          for (int i = 0; i < size; i++) {
           current = current.getNext();
        }
        Node<T> temp = new Node(data,null);
        current.setNext(temp);
        tail = temp;
        size++;
    }
  }
}

public T getAtIndex(int index) {
    if ((index < 0) || (index >= size)) {
      throw new IllegalArgumentException("Your index is out of the list bounds");
    }
    else {
      Node<T> current = head;
      for (int i = 0; i< index+1; i++) {
        current = current.getNext();
      }
      return current.getData();
    }
  }

   public T removeAtIndex(int index) {
     T removed_data = null;
     if ((index < 0) || (index >= size)) {
       throw new IllegalArgumentException("Your index is out of the list bounds");
     }
     else {
       if (size == 0) {
          removed_data = head.getData();
          head = null;
          tail = null;
          size = 0;
       }
       else if (index == 0 && size > 0) {
           Node<T> removed = head;
           removed_data = removed.getData();
           head = head.getNext();
           removed = null;
           size--;
       }
       else if  (index < (size-1) && size > 0) {
        Node<T> current = head;
        for (int i = 0; i< index; i++) {
          current = current.getNext();
        }
        Node<T> removed = current.getNext();
        removed_data = removed.getData();
        current.setNext(removed.getNext());
        removed = null;
        size--;
       }
       else if  (index == (size-1) && size > 0) {
        Node<T> current = head;
        for (int i = 0; i< index; i++) {
          current = current.getNext();
        }
        Node<T> removed = current.getNext(); //current is tail
        removed_data = removed.getData();
        current.setNext(null);
        removed = null;
        tail = current;
        size--;
       }
    } return removed_data;
    }

   public T remove(T data) {
     T toReturn = null;
     if (data == null) {
       throw new IllegalArgumentException("You cannot remove null data to the list");
     }
     else {
       Node<T> current = head;
       int counter = 0;
       while (current != null) {
         current = current.getNext();
           counter++;
         if (current.getData() == data) {
           toReturn = removeAtIndex(counter);
         }
       }
       if (counter == size) {
       throw new NoSuchElementException("The data is not present in the list.");
       }
     } return toReturn;
   }

   public void clear() {
     head = null;
     tail = null;
     size = 0;
   }

   public boolean isEmpty() {
     return (head == null);
   }

   public int size() {
     Node<T> current = head;
     int count = 0;
     while (current != null) {
       current = current.getNext();
       count++;
     }
     return count;
   }
}


Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source