如何在双循环链表的末尾插入一个项?为什么运行此程序时要使用NullPointerException
?
public void addLast( String title, double length ){
if( isEmpty()){
head = new Episode(title, length, head, head);
}else{
Episode last = head.prev;
Episode new_episode = new Episode(title, length, head, last);
}
}
代码设置新节点的引用,但不更新列表中的现有引用。修正了通信中的注释:
if( isEmpty()){ // assuming if empty, head == null
head = new Episode(title, length, head, head);
head.next = head; // fix
head.prev = head; // fix
} else {
Episode last = head.prev;
Episode new_episode = new Episode(title, length, head, last);
last.next = new_episode; // fix
head.prev = new_episode; // fix
}