英文:
Doubly linked list: adding a node at front. From geeksforgeeks (java code)
问题
这是一个在双向链表前面添加节点的代码。我在这里不理解的是步骤4。在这里,我看到它似乎在将new_Node的地址存储到变量head.prev中。变量head.prev现在将持有new_node。这甚至没有意义,因为变量'head'也将持有new_node。所以现在我们有两个变量指向相同的地址。
即使在任何情况下,这段代码的意思是new_node = head.prev,这也是没有意义的,因为此时head.prev将为null,然后new_node将指向null。
// 双向链表的类
public class DLL {
Node head; // 链表的头
/* 双向链表的节点 */
class Node {
int data;
Node prev;
Node next;
// 构造函数用于创建一个新节点
// next和prev默认初始化为null
Node(int d) { data = d; }
}
// 在链表前面添加一个节点
public void push(int new_data)
{
/* 1. 分配节点
* 2. 放入数据 */
Node new_Node = new Node(new_data);
/* 3. 将新节点的下一个设置为头部,上一个设置为null */
new_Node.next = head;
new_Node.prev = null;
/* 4. 将头部节点的prev更改为新节点 */
if (head != null)
head.prev = new_Node;
/* 5. 将头部指向新节点 */
head = new_Node;
}
}
英文:
This is a code that adds a node at the front of the doubly linked list. What I don't understand here is step 4. Right here, it appears to me that it's storing the address of the new_Node into the variable head.prev. The variable head.prev will now hold new-node. This doesn't even make sense because the variable 'head' will also hold new_node. So now we have two variables pointing to the same address.
Even if, in any case, this code was meant to say, new_node = head.prev, that also does not make sense, because the head.prev will be null at this point, and new_node will then point to a null.
// Class for Doubly Linked List
public class DLL {
Node head; // head of list
/* Doubly Linked list Node*/
class Node {
int data;
Node prev;
Node next;
// Constructor to create a new node
// next and prev is by default initialized as null
Node(int d) { data = d; }
}
// Adding a node at the front of the list
public void push(int new_data)
{
/* 1. allocate node
* 2. put in the data */
Node new_Node = new Node(new_data);
/* 3. Make next of new node as head and previous as NULL */
new_Node.next = head;
new_Node.prev = null;
/* 4. change prev of head node to new node */
if (head != null)
head.prev = new_Node;
/* 5. move the head to point to the new node */
head = new_Node;
}
}
答案1
得分: 3
第四步需要将旧头部的prev
连接到新头部。
这是第三步之后的情况:
然后在第四步之后,旧头部的prev
(原本为null)被设置为指向新头部:
然后第五步将头部指向新节点(新头部):
英文:
The step 4 is needed to connect the prev
of the old head to the new head.
This is the situation after step 3:
Then after step 4 the prev
of the old head (which was null) is set to point to the new head:
And then step 5 makes head point to the new node (the new head):
答案2
得分: 0
如果 head.prev != null
那么 head
不是该列表的第一个元素。这应该作为前提条件进行检查,并且应该抛出 IllegalStateException
。由于必须恢复到第一个位置的指针,插入的进一步处理是无意义的。
在第3步完成后,new_node
的设置就完成了,并且 new_node
通过 new_node.next
在单向上与先前的第一个元素 head
相链接。为了使双重链接完整,必须将 head.prev
设置为新的前任 head
。如果省略 if
语句,这就是第4步所做的事情。
英文:
If head.prev != null
then head
is not the first element of the list. This should be checked as a pre-condition, and an IllegalStateException
should be thrown. Further processing of the insertion is senseless as the pointer to the first position must be restored.
After step 3, the new_node
setup is complete, and the new_node
is linked unidirectional by new_node.next
to the former first, now second element head
. To make the double-link complete, head.prev
must be set to the new predecessor head
. That is what step 4 does if you omit the if
.
答案3
得分: 0
public class DLL {
private Node head;
private Node tail;
public void addFirst(int val) {
Node node = new Node(val);
if (head == null)
tail = node;
else {
node.next = head;
head.prev = node;
}
head = node;
}
public void addLast(int val) {
Node node = new Node(val);
if (tail == null)
head = node;
else {
tail.next = node;
node.prev = tail;
}
tail = node;
}
private static final class Node {
private final int val;
private Node prev;
private Node next;
public Node(int val) {
this.val = val;
}
}
}
英文:
public class DLL {
private Node head;
private Node tail;
public void addFirst(int val) {
Node node = new Node(val);
if (head == null)
tail = node;
else {
node.next = head;
head.prev = node;
}
head = node;
}
public void addLast(int val) {
Node node = new Node(val);
if (tail == null)
head = node;
else {
tail.next = node;
node.prev = tail;
}
tail = node;
}
private static final class Node {
private final int val;
private Node prev;
private Node next;
public Node(int val) {
this.val = val;
}
}
}
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论