Here's the code that I currently have. Only contains the addFirst method.
public void addFirst(E e) {
Node<E> newNode = new Node<E>(e);
if (tail == null)
head = tail = newNode;
else {
newNode.next = head;
head = newNode;
}
size++;
}
private static class Node<E> {
E element;
Node<E> next;
public Node(E e) {
element = e;
}
}
Trying to set up the 'previous' field
public void addFirst(E e) {
Node<E> newNode = new Node<E>(e);
if (tail == null)
head = tail = newNode;
else {
newNode.next = head;
firstElement.previous = newNode; //*
head = newNode;
newNode.previous = null;
}
size++;
}
private static class Node<E> {
E element;
Node<E> next, previous; //adding previous field
public Node(E e) {
element = e;
}
}
*I want firstElement.previous to point to newNode. How do I do that?
change:
firstElement.previous = newNode
to
head.previous = newNode;
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.