我对列入共同名单的内容有问题。
public class LinkedList {
public Node first;
public Node last;
public LinkedList() {
first = null;
last = null;
}
public void addFirst(Student student) {
Node f = first;
Node newNode = new Node(student);
first = newNode;
if (f == null) last = newNode;
else f.previous = newNode;
}
public void addLast(Student student) {
Node l = last;
Node newNode = new Node(student);
last = newNode;
if (l == null) first = newNode;
else {
l.next = newNode;
}
}
public void display() {
Node current = first;
while (current != null) {
//print...
current = current.next;
}
}
我的问题是,在我执政的时候:
list.addLast(1);
list.addFirst(2);
list.display();
It displays just "2" display method just can t see last added element.
But if I run:
list.addFirst(2);
list.addLast(1);
It will display both. What is wrong with it? Thanks.