I am trying to implement the add method for the linkedlist , it should take any data type, but I am kind of lost and is not working, any help will be appreciated.
public class LinkedList <T>extends AbstractList {
private class Node {
T data;
Node next;
Node(T data, Node next) {
this.data = data;
this.next = next;
}
Node(T data) {
this(data, null);
}
}
Node first;
Node last;
public LinkedList() {
first = null;
last = null;
}
@Override
public boolean add(T item) {
Node newNode = new Node((T)item);
if (isEmpty()) {
first = newNode;
last = first;
return true;
}
last.next = newNode;
last = null;
return true;
}
}
last = null;looks to be incorrect.last = newNode;maybe? Also you should genericise Nodelast = newNodeandlast.next = nulljava.util.LinkedListimplementation for hints.