javascript中的双链表

我在javascript中构建链表。 我不明白一部分。

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
*/

问题是插入功能!
假设我已经拥有ABC节点了。
我想在B之后插入K.

目前,B的下一个和前一个是C和A。
C的前一个元素是B.


一旦我把K放在B之后,
ABKC
(1)K的下一个元素是C
(2)K的前一个元素是B.
(3)B的下一个元素是K.
(4)C的前一个元素是K.

在我在Insert函数中编写的代码中,下面代码的每一行都应该处理上层语句。
(1)newNode.next = current.next;
(2)newNode.previous = current;
(3)current.next = newNode;
(4)current.next.previous = newNode;

但是,当我运行包括(4)的整个代码时,发生了错误。
我不明白为什么......
如果没有(4)代码行,它就可以工作。

有谁能帮我理解这个?

采纳答案:

您需要在第3步之前执行第4步:

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

实际上,在查找“旧” current.nextprevious属性( current.next.previous ,指向B时)之前, current.next (C)的引用被设置为newNode (K)。 只要为其分配新值,就会更改对当前节点的引用。 这就是current.next.previous实际上返回newNode.previous而不是您期望的节点引用的原因。

author: jmlane

参考更多解答: Doubly Linked List in javascript ,转载请保留出处javascript中的双链表及作者信息

Statement: We respect knowledge and authors. Since the content comes from the Internet and is intended for scientific research, any reprinters should retain the author's signature and origin. If you are the author of the content and feel in dispute, please contact email: 1076545519@qq.com. We will find out the situation and deal with it in time. We sincerely thank the author for his hard work.


更多:doubly-linked-list