将最后一个节点添加到链接列表

发布于 2025-01-26 14:53:35 字数 1471 浏览 3 评论 0原文

我正在学习如何将节点添加到链接列表中。以下是正确的代码:

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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

汹涌人海 2025-02-02 14:53:35

也许您可以从此处读取linked-list源代码 https://github.com/samavati/samavati/tsds

Maybe you can read the linked-list source code from here https://github.com/samavati/tsds

任性一次 2025-02-02 14:53:35

区别在于:

curr是一个变量。 this.head是对象的属性。

如果您分配给Curr,则在查看变量时会看到效果。如果您分配给this.head,则修改属于对象的属性。因此,此突变一个对象。对curr 的分配不能突变对象。它只是将某些内容分配给变量。

如果分配到变量会突变对象(在这种情况下, list ),然后想象一下语句curr = curr.next会带来(在else块中)...但是,您似乎知道该语句不会更改列表,它仅允许使用变量沿该列表行走。然后没有理由认为curr = node会更改列表。

它可能有助于在第一个代码块中可视化过程。假设我们在空名单上调用addlast(1)。当我们到达时,如果阻止,我们可以像这样想象状态:

 this                  node
  │                     │
  ▼                     ▼
┌────────────┐    ┌────────────┐
│ head: null │    │ val: 1     │
└────────────┘    │ next: None │
                  └────────────┘

因此,此时我们有两个变量:thisnode。然后执行curr = this.head

 this curr             node
  │    │                │
  ▼    ▼                ▼
┌────────────┐    ┌────────────┐
│ head: null │    │ val: 1     │
└────────────┘    │ next: None │
                  └────────────┘

然后您的代码建议curr = node。看看会发生什么:

 this curr             node
  │    └────────────┐   │
  ▼                 ▼   ▼
┌────────────┐    ┌────────────┐
│ head: null │    │ val: 1     │
└────────────┘    │ next: None │
                  └────────────┘

相反,如果您要做curr.next = node,则会得到突变您需要:

 this curr             node
  │    │                │
  ▼    ▼                ▼
┌────────────┐    ┌────────────┐
│ head: ────────► │ val: 1     │
└────────────┘    │ next: None │
                  └────────────┘

希望这会澄清它。

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 to this.head you modify the property that belongs to an object. So this mutates an object. The assignment to curr 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 the else 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 that curr = 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 the if block we could picture the state like this:

 this                  node
  │                     │
  ▼                     ▼
┌────────────┐    ┌────────────┐
│ head: null │    │ val: 1     │
└────────────┘    │ next: None │
                  └────────────┘

So we essentially have two variables at this point: this and node. Then curr = this.head is executed:

 this curr             node
  │    │                │
  ▼    ▼                ▼
┌────────────┐    ┌────────────┐
│ head: null │    │ val: 1     │
└────────────┘    │ next: None │
                  └────────────┘

Then your code suggest doing curr = node. Look what happens:

 this curr             node
  │    └────────────┐   │
  ▼                 ▼   ▼
┌────────────┐    ┌────────────┐
│ head: null │    │ val: 1     │
└────────────┘    │ next: None │
                  └────────────┘

If instead you'd do curr.next = node, you get the mutation you need:

 this curr             node
  │    │                │
  ▼    ▼                ▼
┌────────────┐    ┌────────────┐
│ head: ────────► │ val: 1     │
└────────────┘    │ next: None │
                  └────────────┘

Hope this clarifies it.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文