I try to implement Generic linked list but I get an error:
required type T, Provided Object in the line T val = head.val;
DDLinkedList class:
public class DDLinkedList <T>{
private ListElement head;
private ListElement tail;
protected <T> void addToHead(T val){
if (this.isEmpty()){
head = new ListElement(val,head);
tail = head;
}else {
head = new ListElement(val, head);
head.next.prev = head;
}
}
protected <T> T removeFromHead(){
if(this.isEmpty()){
return null;
}
T val = head.val;
head = head.next;
return val;
}
}
the List Element class:
public static class ListElement<T> {
private ListElement next;
private ListElement prev;
private T val;
public ListElement(){
this(null, null, null);
}
public ListElement(T val){
this(val, null, null);
}
public ListElement(T val, ListElement next, ListElement prev){
this.val = val;
this.next = next;
this.prev = prev;
}
}
What can be the problem?