简体   繁体   English

JavaScript中的双链表

[英]Doubly Linked List in javascript

I was building linked list in javascript. 我正在用javascript构建链接列表。 I dont understand one part. 我不明白一部分。

 function Node(element) { this.element = element; this.next = null; this.previous = null; } function LList() { this.head = new Node("head"); this.find = find; this.findLast = findLast; this.remove = remove; this.insert = insert; this.display = display; this.dispReverse = dispReverse; } function find(item) { var currNode = this.head; while(currNode.element != item) { currNode = currNode.next; } return currNode; } function display(list) { var currNode = this.head.next; while (currNode != null) { console.log(currNode.element); currNode = currNode.next; } } function insert(newElement, item) { var newNode = new Node(newElement); var current = this.find(item); newNode.next = current.next; newNode.previous = current; current.next = newNode; // Why I dont need this part? // Since new node got inserted, on my thoughts, // the next node of the current node should point the new node as a previous one // current.next.previous = newNode; } function remove(item) { var currNode = this.find(item); if (currNode.next != null) { currNode.previous.next = currNode.next; currNode.next.previous = currNode.previous; currNode.next = null; currNode.previous = null; } } function findLast() { var currNode = this.head; while (currNode.next != null) { currNode = currNode.next; } return currNode; } function dispReverse() { var currNode = this.head; currNode = this.findLast(); while(currNode.previous != null) { console.log(currNode.element); currNode = currNode.previous; } } var cities = new LList(); cities.insert("Conway", "head"); cities.insert("Russellville", "Conway"); cities.insert("Carlisle", "Russellville"); cities.insert("Alma", "Carlisle"); cities.display(); cities.remove("Carlisle"); cities.display(); cities.dispReverse(); /* Output should look like this: Conway Russellville Carlisle Alma Conway Russellville Alma Alma Russellville Conway */ 

Problem is the insert function! 问题是插入功能!
Let's say if I have ABC node already. 假设我已经有ABC节点。
And I want to insert K after B. 我想在B之后插入K。

Currently, the next and previous of B are C and A for each. 当前,B的下一个和前一个分别是C和A。
The previous element of C is B. C的前一个元素是B。


Once I put K after B, 我把K放在B之后
ABKC ABKC
(1) the next element of K will be C (1)K的下一个元素将是C
(2) the previous element of K will be B. (2)K的前一个元素将是B。
(3) The next element of B is K (3)B的下一个元素是K
(4) previous element of C is K. (4)C的前一个元素是K。

On the code I wrote in Insert function, each line of the codes below should process the upper statements. 在我在Insert函数中编写的代码上,以下代码的每一行都应处理上面的语句。
(1) newNode.next = current.next; (1)newNode.next = current.next;
(2) newNode.previous = current; (2)newNode.previous =当前;
(3) current.next = newNode; (3)current.next = newNode;
(4) current.next.previous = newNode; (4)current.next.previous = newNode;

But when I run this whole code including (4), error has occurred. 但是当我运行包括(4)的整个代码时,发生了错误。
I don understand why... 我不明白为什么...
Without the (4) line of codes, it works. 没有代码的(4)行,它将起作用。

Is there anyone who can help me understand this? 有谁能帮助我理解这一点?

Your insert logic seems wrong in the last line : 您的插入逻辑在最后一行似乎是错误的:

current.next = newNode; current.next = newNode;

current.next.previous = newNode; current.next.previous = newNode;

This actually means 这实际上意味着

newNode.previous=newNode; newNode.previous = newNode;

Since you are setting the value of current.next to newNode in the 3rd statement 由于您要在第三条语句中将current.next的值设置为newNode

It should be : 它应该是 :

newNode.next.previous = newNode. newNode.next.previous = newNode。

You need to do step 4 before step 3: 您需要在步骤3之前执行步骤4:

current.next.previous = newNode
current.next = newNode

As it is, the reference of current.next (C) is being set to newNode (K) before you lookup the "old" current.next 's previous property ( current.next.previous when points to B). newNode在查找“旧” current.nextprevious属性(指向B时为current.next.previous )之前, current.next (C)的引用将设置为newNode (K)。 Your reference to the current node is changed as soon as you assign it a new value. 分配新值后,对当前节点的引用即会更改。 This is why current.next.previous is actually returning newNode.previous instead of the node reference you expect. 这就是为什么current.next.previous实际上返回newNode.previous而不是您期望的节点引用的原因。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM