理解Java中删除双链表节点
澄清:主要问题是哪个节点类似于current.previous.next=current.next实际指向哪个节点 我正在使用我在YouTube视频上找到的一些示例代码,但我确实试图将其分解并理解。一切按原样进行,我在每个部分都添加了注释以帮助我理解。当代码开始在同一行中使用“上一行”和“下一行”时,我在用通俗易懂的英语解释正在发生的事情时遇到了一些问题,比如节点删除。我现在没有完全理解它所指的内容。我已经在调试器中完成了代码,但是如果有人不介意的话,我需要一些简单的英语解释 比如说,我有一个DLL,它有3,4,5,6,7。所以我要删除5,这是第三个索引。以下是移除的方法理解Java中删除双链表节点,java,nodes,doubly-linked-list,Java,Nodes,Doubly Linked List,澄清:主要问题是哪个节点类似于current.previous.next=current.next实际指向哪个节点 我正在使用我在YouTube视频上找到的一些示例代码,但我确实试图将其分解并理解。一切按原样进行,我在每个部分都添加了注释以帮助我理解。当代码开始在同一行中使用“上一行”和“下一行”时,我在用通俗易懂的英语解释正在发生的事情时遇到了一些问题,比如节点删除。我现在没有完全理解它所指的内容。我已经在调试器中完成了代码,但是如果有人不介意的话,我需要一些简单的英语解释 比如说,我有一个D
//Method to remove node at a specified position
public void removeAt(int index) {
//If the head doesn't exist, break out of the logic
if(head == null) return;
//If the requested index is smaller than 1 or greater
//than the size of the list, break out.
if(index < 1 || index > size) return;
//Declares the currently used link as the head
Link current = head;
//Declare int i counter for use in while loop
//While i is less than the index set current to the next node
//and add to the counter i
int i = 1;
while(i < index) {
current = current.next;
i++;
}
//If the next node doesn't exist, set current...previous next?
if(current.next == null) {
current.previous.next = null;
}
//Else if the node before the current is null, set current to
//the next node and then set the previous to null (I thought it
//already was null??) Set the head to the current node
else if(current.previous == null) {
current = current.next;
current.previous = null;
head = current;
}
//If none of the above conditions, set current previous next?? to
//the next node and current next previous to the previous node??
else {
current.previous.next = current.next;
current.next.previous = current.previous;
}
//Subtract from the size of the list
size--;
}
请给我解释一下。谢谢 如果node.next为null,则表示该节点是列表的最后一个元素。因此,如果试图删除列表中的最后一个元素,则最后一个元素之前的元素将成为最后一个元素。这就是代码的作用
if(current.next == null) {
current.previous.next = null;
}
if(current.previous == null) {
current = current.next;
current.previous = null;
head = current;
}
如果node.previous为null,则表示它是列表的第一个元素。首先,我们必须保留对以下元素的引用,定义以下元素不能有前一个元素,然后将其定义为列表的标题。这就是代码的作用
if(current.next == null) {
current.previous.next = null;
}
if(current.previous == null) {
current = current.next;
current.previous = null;
head = current;
}
然后继续你的例子
如果current为4,则current.previous为3,current.next为5
所以
定义3.next现在是5
及
定义5.5。上一个现在是3
那么4就不再是列表中的引用了(DLL)是一个可以在两个方向上遍历的列表,正如您已经知道的那样。每个节点都有指向其兄弟节点的上一个和下一个指针,允许导航到两个方向。您已经有了指向链接列表标题的head
变量
在remove
方法中,您传入了要删除的节点的索引
while(i < index) {
current = current.next;
i++;
}
上述代码部分将是上述第二点<代码>当前指向最后一个节点。要删除最后一个节点,您必须返回到current.previous
,并将其链接设置为null
,以便从previous
节点不再访问current
else if(current.previous == null) {
current = current.next;
current.previous = null;
head = current;
}
检查当前节点是否为节点的头部。如果current
指向链接列表的头部,则它没有任何以前的同级,因此current.previous
指向null
。因此current.next
必须成为列表的新标题。使用current.previous=null
我们将previous
链接设置为null,并将此current
head设置为head
变量。现在,上一个节点已消失。没有提到它
第三种情况是上述要点3。这件事由我来处理
else {
current.previous.next = current.next;
current.next.previous = current.previous;
}
我们要删除当前节点的位置。这意味着我们需要将current.previous
和current.next
链接为同级
最后,您的
链接
类使用公共字段。它们应该是私有的,通过getter和setter进行访问。这些写访问是对字段的访问,而不是对字段中包含的值的访问。要删除当前节点,必须更改上一个和下一个节点的指针以跳过当前节点…@fabian我没有完全遵循。我将更新帖子,将其他类包含在这些字段中。我不确定它们是否完全相关。
else if(current.previous == null) {
current = current.next;
current.previous = null;
head = current;
}
else {
current.previous.next = current.next;
current.next.previous = current.previous;
}