I have problems in doubly LinkedList and I am not using a tail I instead use a current pointer. When I use the tail I don't find problem but when I use a current pointer I get an error and I can not solve it. It occurs when I remove a node. My program is working but I can not remove the next node. This is the error message:
This the class DNode
public class DNode<T> {
T data;
DNode<T> next;
DNode<T> prev;
public DNode(T e){
data = e;
next = prev = null;
}
This class DoubleLinkedList
public class DoubleLinkedList<T> {
DNode<T> head;
DNode<T> current;
int size = 0;
public DoubleLinkedList() {
head = current = null;
}
public void Insert(T e) {
DNode<T> tmp = new DNode(e);
if (size == 0) {
head = current = tmp;
} else {
tmp.next = current.next;
tmp.prev = current;
current.next = tmp;
current = tmp;
}
size++;
}
public void remove() {
if (head == current) {
if (current.next == null) {
head = current = null;
} else {
current.next.prev = null;
head = current.next;
current.next = null;
current = head;
}
} else {
DNode<T> tmp = current.next;
current.prev.next = tmp;
if (tmp != null) {
tmp.prev = current;
}
current.next = current.prev = null;
current = tmp;
}
size--;
}
The main calss
public static void main(String[] args) {
DoubleLinkedList<String> d = new DoubleLinkedList();
d.Insert("jon");
d.Insert("jack");
d.Insert("mohammed");
d.remove();
d.remove();// here my problem
}
The line with the comment is where I get an error.