【发布时间】:2021-04-06 09:16:39
【问题描述】:
考虑一个模仿 Linkedlist 数据结构的 LinkedList 类,如下所示:
class LinkedList {
constructor(value) {
this.head = {
value: value,
next: null
};
this.tail = this.head;
this.length = 1;
}
append(value) {
const newNode = {
value: value,
next: null
}
this.tail.next = newNode; // why does this change head.next ?
this.tail = newNode;
this.length++;
return this;
}
}
let myLinkedList = new LinkedList(10);
myLinkedList.append(5);
日志输出
LinkedList {
head: { value: 10, next: { value: 5, next: null } },
tail: { value: 5, next: null },
length: 2
}
我看到this.tail.next 也会改变tail 的下一个属性(然后this.tail = newNode 会将tail 重新分配给newNode)。我在这里不明白的是为什么this.tail.next 也会改变this.head 的下一个属性?
另外,当将另一个数字附加到列表myLinkedList.append(16) 时,它会不断更新head 的下一个属性,如下所示:
LinkedList {
head: { value: 10, next: { value: 5, next: [Object] } },
tail: { value: 16, next: null },
length: 3
}
也许一个可能的原因与我定义this.tail = this.head 的构造函数有关?但我不太确定,因为这个只分配头到尾的值。
总结一下,我的问题是为什么this.tail.next = newNode会改变head的next属性?另外,在追加其他值时,为什么会改变 head.next.next 等等?
【问题讨论】:
-
"也许一个可能的原因与我定义
this.tail = this.head的构造函数有关?" - 是的,当然这就是原因:两个属性现在都拥有相同的对象引用,然后您将更改从这两个地方引用的该对象的.next属性。 -
第一个追加是预期更新头部(因为开头头部是尾部)。第二个追加不会更新
head,它只是console.log以这种方式表示它。也就是说,你的代码没问题。 -
(你的代码唯一奇怪的是链表总是至少包含一个元素并且不能为空)
-
这不是我的代码(是的,它应该以这种方式工作,但我不明白它是如何工作的,所以我问:D(经过一番思考后))所以实际上它只是对象引用。 head 的引用对象被分配给 tail。当我执行
tail.next时,它还会更新它引用的对象,即head.next。对吗?
标签: javascript class oop linked-list this