我已經創建了這個方法來將一個數字添加到一個鏈表中,但我真的不明白它為什么會起作用。
如果您看一下下面的代碼,您會注意到我創建了一個名為“current”的變數,該變數使用 this.head 的內容進行設定,直到那時一切正常,但我不明白為什么 this.head 使用值進行更新如果我沒有告訴 Javascript 這樣做,則為當前變數。
這是我的代碼,我真的很感謝你們能給我的幫助
class Node {
constructor(value, next_node = null) {
this.value = value;
this.next_node = next_node;
}
}
class LinkedList {
// setup head and tail
constructor() {
this.head = null;
this.length = 0;
}
add(number) {
let node = new Node(number)
if(this.head === null){
this.head = node;
} else {
let current = this.head;
while(current.next_node !== null){
current = current.next_node
}
current.next_node = node;
console.log(this.head)
}
this.length
}
get(index) {
}
}
const ll = new LinkedList();
ll.add(2)
ll.add(3)
ll.add(5)
uj5u.com熱心網友回復:
可視化該程序可能會有所幫助:
一旦您創建了串列const ll = new LinkedList(),我們可以將情況表示為:
ll
↓
┌────────────┐
│ head: null │
│ length: 0 │
└────────────┘
現在我們執行ll.add(2),這等同this于ll我們執行let node = new Node(number)。這可以描述為:
ll/this node
↓ ↓
┌────────────┐ ┌─────────────────┐
│ head: null │ │ value: 2 │
│ length: 0 │ │ next_node: null │
└────────────┘ └─────────────────┘
if條件為真,所以被this.head = node執行,最后this.length 。呼叫add結束,因此node變數超出范圍:
ll/this
↓
┌────────────┐ ┌─────────────────┐
│ head: ────────?? │ value: 2 │
│ length: 1 │ │ next_node: null │
└────────────┘ └─────────────────┘
何時ll.add(3)執行,我們將進入您問題的核心。再次let node = new Node(number)執行:
ll/this node
↓ ↓
┌────────────┐ ┌─────────────────┐ ┌─────────────────┐
│ head: ────────?? │ value: 2 │ │ value: 3 │
│ length: 1 │ │ next_node: null │ │ next_node: null │
└────────────┘ └─────────────────┘ └─────────────────┘
這次if條件為假,我們執行let current = this.head:
ll/this node
↓ ↓
┌────────────┐ ┌─────────────────┐ ┌─────────────────┐
│ head: ────────?? │ value: 2 │ │ value: 3 │
│ length: 1 │ │ next_node: null │ │ next_node: null │
└────────────┘ └─────────────────┘ └─────────────────┘
↑
current
由于while條件為假,回圈不會迭代,我們執行current.next_node = nodeand this.length :
ll/this node
↓ ↓
┌────────────┐ ┌─────────────────┐ ┌─────────────────┐
│ head: ────────?? │ value: 2 │ │ value: 3 │
│ length: 2 │ │ next_node: ────────?? │ next_node: null │
└────────────┘ └─────────────────┘ └─────────────────┘
↑
current
這是至關重要的:因為current參考與 相同的節點,無論您是通過還是通過this.head查看節點,節點的突變都是可見的:它是同一個節點。this.headcurrent
這應該說清楚。只是為了完成示例腳本,讓我們也執行ll.add(5). while在塊中回圈的開始,else我們有這個:
ll/this node
↓ ↓
┌────────────┐ ┌─────────────────┐ ┌─────────────────┐ ┌─────────────────┐
│ head: ────────?? │ value: 2 │ │ value: 3 │ │ value: 5 │
│ length: 2 │ │ next_node: ────────?? │ next_node: null │ │ next_node: null │
└────────────┘ └─────────────────┘ └─────────────────┘ └─────────────────┘
↑
current
現在回圈進行一次迭代,current指向第二個節點實體:
ll/this node
↓ ↓
┌────────────┐ ┌─────────────────┐ ┌─────────────────┐ ┌─────────────────┐
│ head: ────────?? │ value: 2 │ │ value: 3 │ │ value: 5 │
│ length: 2 │ │ next_node: ────────?? │ next_node: null │ │ next_node: null │
└────────────┘ └─────────────────┘ └─────────────────┘ └─────────────────┘
↑
current
最后,current.next_node = nodeandthis.length 被執行,之后變數nodeandcurrent結束它們的生命:
ll
↓
┌────────────┐ ┌─────────────────┐ ┌─────────────────┐ ┌─────────────────┐
│ head: ────────?? │ value: 2 │ │ value: 3 │ │ value: 5 │
│ length: 3 │ │ next_node: ────────?? │ next_node: ────────?? │ next_node: null │
└────────────┘ └─────────────────┘ └─────────────────┘ └─────────────────┘
這完成了串列并間接改變了ll.head參考的內容。
轉載請註明出處,本文鏈接:https://www.uj5u.com/qukuanlian/469299.html
標籤:javascript 变量 链表
