如何解决链接列表指针Javascript,append方法
我认为这段代码可以很好地用于创建链表并附加到链表中, 但是我不明白下面注释的那小段代码,请帮忙。
class Node {
constructor(value) {
this.value = value;
this.next = null;
}
}
class LinkedList {
constructor(value) {
this.head = new Node(value);
this.tail = this.head;
this.length = 1;
}
append(value) {
const newNode = new Node(value);
// how come that we did not even mention this.head and it keeps adding to it??
this.tail.next = newNode; //<<<<< can not figure this one
this.tail = newNode;
this.length++;
return this;
}
}
//test code
let myLinkedList = new LinkedList(10);
myLinkedList.append(5);
myLinkedList.append(16);
myLinkedList.append(14);
console.log(myLinkedList);
我想了解的>>>
// instantiation
p = { a: 1,b: null };
c = p;
// what happens when i call append
node = { a: 2,b: null };
c.b = node;
console.log(p);
c = node;
node1 = { a: 3,b: null };
c.b = node1;
console.log(p);
c = node1;
node2 = { a: 4,b: null };
c.b = node2;
console.log(p);
c = node2;
我想知道c
如何精确地访问b
对象中的最后一个p
属性,
每个b都有它自己的内存地址,还是因为c
包含a: 4
,所以当我说c.b
时,它访问带有b
的{{1}}?
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。