我正在嘗試使用工廠函式創建一個鏈表。當我在串列 console.log 上運行方法附加時,顯示正確的值。另一方面,當我 console.log 結果串列時,前置方法似乎不起作用。但是,當我運行一個方法來回傳串列的值并記錄我得到我正在尋找的值時。代碼如下:
//linked list factory function
const LinkedListFactory = (initialNode) => {
//declare initial value of the list
let head = { value: initialNode.value, nextNode: initialNode.nextNode };
//method to return list
const giveList = () => {
return head;
};
//method to return last node in list
const getLast = () => {
let lastNode = head;
if (lastNode) {
while (lastNode.nextNode) {
lastNode = lastNode.nextNode;
}
}
return lastNode;
};
//method to append node to the end of the list
const append = (node) => {
getLast().nextNode = node;
};
//method to add node to the front of the list (not working)
const prepend = (node) => {
let temp = { ...head };
node.nextNode = temp;
head = node;
//gives the correct updated value of the list
console.log(head);
};
return { head, append, prepend, giveList };
};
//node factory function to create data nodes
const NodeFactory = (value = null, nextNode = null) => {
return { value, nextNode };
};
//creating test nodes
let node1 = NodeFactory("This is node 1");
let node2 = NodeFactory("This is node 2");
let node3 = NodeFactory("This is node 3");
let node4 = NodeFactory("This is node 4");
let list = LinkedListFactory(node1);
console.log(list.head);
list.append(node2);
console.log(list.head);
list.prepend(node3);
//These two statements give different values in the console
console.log(list.head);
console.log(list.giveList());
我做了一些谷歌搜索以試圖確定問題,但到目前為止我沒有運氣。我希望將 node3 添加到鏈表的開頭并指向 node1,然后再指向 node2。當我 console.log 串列時,它只顯示 node1 和 node2 但不顯示 node3。當我運行一個方法來回傳鏈接串列并記錄它時,我得到了我正在尋找的值。我不知道為什么會這樣,我很困惑。
uj5u.com熱心網友回復:
問題是它head = node實際上并沒有按照你認為的那樣去做。
首先讓我們看一下LinkedListFactory,它在內部宣告一個變數head并使用傳遞給 的 Node 的內容對其進行初始化LinkedListFactory。宣告方法后,您將回傳一個包含它們和其中的head節點的新物件。(從現在開始我將其稱為“串列”)
JavaScript 物件始終是對其基礎值的參考。這意味著在LinkedListFactory回傳后“串列”物件的head屬性將指向與head在LinkedListFactory.
當您在prepend中將head變數設定為等于新temp節點時,“串列”物件的 head 屬性仍將指向原始值。
要實作您想要的效果,您需要將 prepend 更改為如下所示:
function prepend(node) {
let temp = this.head; // temp points to the old head
this.head = node; // overwrite pointer to head on list
this.head.nextNode = temp; // point to old head
head = this.head; // keep head and this.head in sync
}
在這種情況下this,將參考prepend函式被呼叫的物件,因此“串列”,我們可以修改它。
在您的實作中,我們只是更改內部變數head指向的內容。在此實作中,我們更改了head“串列”的屬性。
我可以推薦你看看這些 MDN 頁面:
- https://developer.mozilla.org/en-US/docs/Web/JavaScript/Guide/Working_with_Objects
- https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Operators/this
我稍微考慮了一下。您的實施有效,您只需在沒有的地方賦予“串列”的 head 屬性一個含義。您可以簡單地洗掉它,因為它只會令人困惑:
//linked list factory function
const LinkedListFactory = (initialNode) => {
//declare initial value of the list
let head = { value: initialNode.value, nextNode: initialNode.nextNode };
//method to return list
const giveList = () => {
return head;
};
//method to return last node in list
const getLast = () => {
let lastNode = head;
if (lastNode) {
while (lastNode.nextNode) {
lastNode = lastNode.nextNode;
}
}
return lastNode;
};
//method to append node to the end of the list
const append = (node) => {
getLast().nextNode = node;
};
//method to add node to the front of the list (not working)
const prepend = (node) => {
let temp = { ...head };
node.nextNode = temp;
head = node;
//gives the correct updated value of the list
console.log(head);
};
return { append, prepend, get head() { return giveList(); } }; // I aliased giveList to a `head` setter
};
//node factory function to create data nodes
const NodeFactory = (value = null, nextNode = null) => {
return { value, nextNode };
};
//creating test nodes
let node1 = NodeFactory("This is node 1");
let node2 = NodeFactory("This is node 2");
let node3 = NodeFactory("This is node 3");
let node4 = NodeFactory("This is node 4");
let list = LinkedListFactory(node1);
console.log(list.head);
list.append(node2);
console.log(list.head);
list.prepend(node3);
console.log(list.head);
轉載請註明出處,本文鏈接:https://www.uj5u.com/shujuku/537314.html
