java - 在双向链表末尾插入节点
问题描述
我试图在双向链表的末尾插入一个节点,但是当我运行 add 方法时,代码永远不会完成运行。下面是代码:
public class DoublyLinkedList<T> {
static class Node<T> {
T data;
Node<T> next;
Node<T> previous;
Node() {
data = null;
next = null;
previous = null;
}
Node(T value) {
data = value;
next = null;
previous = null;
}
}
private Node<T> head;
private int size;
public DoublyLinkedList() {
head = null;
}
public DoublyLinkedList(T value) {
head = new Node<T>(value);
size ++;
}
public void add(T value) {
Node<T> append = new Node<T>(value);
append.next = null;
if(head == null) {
append.previous = null;
head = append;
size ++;
return;
}
Node current = head;
while(current.next != null) {
current = current.next;
}
current.next = append;
append.previous = current;
size ++;
}
我很确定 current.next = append 这行是问题所在,但我不知道如何解决它。我究竟做错了什么?
解决方案
推荐阅读
- regex - 如何在飞镖中使用正则表达式从字符串中提取日期时间对象?
- sql - 正确的连接顺序 - 建立一个新的查询
- typo3 - Typo3 9: Second Language 使用后备模式显示两种翻译的内容
- android - 当我打开 android studio 项目时,它在文件中显示不同的字符
- java - 不要将 Android 上下文类放在静态字段中。这是内存泄漏。为什么?
- javascript - 如何检查元素是否可以托管 Shadow DOM?
- javascript - 如何将css分配给具有两个或多个选择器的javascript
- forms - opencart 表单操作网址在哪里
- c# - 如何从复杂的 xml 嵌套结构中选择 xml 值?
- powerbi - 将运行总计拆分为正 DAX Power BI 和负 DAX Power BI