将最后一个节点添加到链接列表
我正在学习如何将节点添加到链接列表中。以下是正确的代码:
class NodeList {
constructor(head = null){
this.head = head
this.addLast = function (data) {
let node = new Node(data)
console.log(curr)
if (this.head === null) {
return this.head = node
} else{
let curr = this.head
while(curr.next){
curr = curr.next
}
curr.next = node
}
}
}
}
最初,
我编写了代码:
class NodeList {
constructor(head = null){
this.head = head
this.addLast = function (data) {
let node = new Node(data)
console.log(curr)
if (this.head === null) {
return this.head = node
} else{
let curr = this.head
while(curr.next){
curr = curr.next
}
curr.next = node
}
}
}
}
主要区别在于if语句。代替
if(this.head === null) { return this.head = node}
我写的
let curr = this.head
if(curr === null) { return curr = node}
,代码不起作用。
的事情
如果我做一些让Nodelist = New Nodelist Nodelist.Addlast(2)
我希望head:node {data:2,next:null}。第一个代码实现了该购买,但第二个代码未达到。我很困惑为什么这样。这两个代码看起来都一样。有人可以启发我吗?
I am learning how to add node to the linked list. The following is the correct code:
class NodeList {
constructor(head = null){
this.head = head
this.addLast = function (data) {
let node = new Node(data)
console.log(curr)
if (this.head === null) {
return this.head = node
} else{
let curr = this.head
while(curr.next){
curr = curr.next
}
curr.next = node
}
}
}
}
Initially,
i wrote the code as such:
class NodeList {
constructor(head = null){
this.head = head
this.addLast = function (data) {
let node = new Node(data)
console.log(curr)
if (this.head === null) {
return this.head = node
} else{
let curr = this.head
while(curr.next){
curr = curr.next
}
curr.next = node
}
}
}
}
The main difference is at the if statement. Instead of
if(this.head === null) { return this.head = node}
I wrote
let curr = this.head
if(curr === null) { return curr = node}
However, the code doesn't work.
If I do something like
let nodeList = new Nodelist
nodeList.addLast(2)
I expect to get head: Node {data: 2, next: null}. The first code achieved that purchase but the second code does not. I am confused why this is so. Both codes look the same. Can someone enlighten me?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
也许您可以从此处读取linked-list源代码 https://github.com/samavati/samavati/tsds
Maybe you can read the linked-list source code from here https://github.com/samavati/tsds
区别在于:
curr
是一个变量。this.head
是对象的属性。如果您分配给
Curr
,则在查看变量时会看到效果。如果您分配给this.head
,则修改属于对象的属性。因此,此突变一个对象。对curr
的分配不能突变对象。它只是将某些内容分配给变量。如果分配到变量会突变对象(在这种情况下, list ),然后想象一下语句
curr = curr.next
会带来(在else
块中)...但是,您似乎知道该语句不会更改列表,它仅允许使用变量沿该列表行走。然后没有理由认为curr = node
会更改列表。它可能有助于在第一个代码块中可视化过程。假设我们在空名单上调用
addlast(1)
。当我们到达时,如果
阻止,我们可以像这样想象状态:因此,此时我们有两个变量:
this
和node
。然后执行curr = this.head
:然后您的代码建议
curr = node
。看看会发生什么:相反,如果您要做
curr.next = node
,则会得到突变您需要:希望这会澄清它。
The difference is this:
curr
is a variable.this.head
is a property of an object.If you assign to
curr
you will see the effect when looking at the variable, nowhere else. If you assign tothis.head
you modify the property that belongs to an object. So this mutates an object. The assignment tocurr
cannot mutate an object. It just assigns something to a variable.If assigning to a variable would mutate an object (and in this case the list), then imagine what havoc the statement
curr = curr.next
would bring (in theelse
block)... Yet, you do seem to understand that this statement does not change the list, it merely allows to walk along that list with a variable. There is no reason to then think thatcurr = node
would alter the list.It may help to visualise the procedure in the first code block. Let's say we call
addLast(1)
on a list that is empty. When we arrive in theif
block we could picture the state like this:So we essentially have two variables at this point:
this
andnode
. Thencurr = this.head
is executed:Then your code suggest doing
curr = node
. Look what happens:If instead you'd do
curr.next = node
, you get the mutation you need:Hope this clarifies it.