带有5个节点的双链表

我无法指向最后一个节点。

输出结果是这样的:

链接列表与5个节点

  1. 节点值:头节点/下节点值:第二节点/最后节点值: null

  2. 节点值:第二个节点/下一个节点值:第三个节点/最后一个节点值:头节点

  3. 节点值:第三节点/下一节点值:第四节点/最后节点值:第二节点

  4. 节点值:第四节点/下一节点值:尾节点/最后节点值:第三节点

  5. 节点值:尾节点/下节点值:未定义/最后节点值:第四节点

但是,我一直得到这个:

链接列表与5个节点

  1. 节点值:头节点/下节点值:第二节点/最后节点值: undefined

  2. 节点值:第二个节点/下一个节点值:第三个节点/最后一个节点值: undefined

  3. 节点值:第三节点/下一节点值:第四节点/最后节点值: undefined

  4. 节点值:第四节点/下一节点值:尾节点/最后节点值: undefined

  5. 节点值:尾节点/下节点值:未定义/最后节点值: null

 var DoubleLinkedList = function() { this.head = 0; this.tail = 0; this.length = 5; var LinkedListNode = function(content) { this.next = 0; this.last = []; this.content = content; }; this.add = function(content) { if (this.head == 0) { this.head = new LinkedListNode(content); return this.head; } if (this.tail == 0) { this.tail = new LinkedListNode(content); this.head.next = this.tail; return this.tail; }; this.tail.next = new LinkedListNode(content); this.tail = this.tail.next; this.tail.next = 0; return this.tail; }; } DoubleLinkedList.prototype.length = function() { var i = 0; var node = this.head; while (node != 0) { i++; node = node.next; } return i; }; DoubleLinkedList.prototype.toString = function() { var i = 1; var str = 'Linked List with ' + this.length + ' nodes <br/>'; var node = this.head; while (node != 0) { str += i + ': Node Value: ' + node.content; str += ' / Next Node Value: ' + node.next.content; str += " / Last Node Value: " + node.last; if (node.next == 0) str += ' null'; if (node.next != 0) str += node.last.content; i++; str += "<br>"; node = node.next; } return str; }; var lln = new DoubleLinkedList(); lln.add(' Head Node'); lln.add(' Second Node'); lln.add(' Third Node'); lln.add(' Fourth Node') lln.add(' Tail Node'); document.getElementById('output').innerHTML = lln.toString(); 
 <p id='output'></p> 

 var DoubleLinkedList = function() { this.head = 0; this.tail = 0; this.length = 5; var LinkedListNode = function(content) { this.next = 0; this.last = 0; this.content = content; }; this.add = function(content) { if (this.head == 0) { this.head = new LinkedListNode(content); return this.head; } if (this.tail == 0) { this.tail = new LinkedListNode(content); this.head.next = this.tail; this.tail.last = this.head; return this.tail; }; this.tail.next = new LinkedListNode(content); this.tail.next.last = this.tail; this.tail = this.tail.next; this.tail.next = 0; return this.tail; }; } DoubleLinkedList.prototype.length = function() { var i = 0; var node = this.head; while (node != 0) { i++; node = node.next; } return i; }; DoubleLinkedList.prototype.toString = function() { var i = 1; var str = 'Linked List with ' + this.length + ' nodes <br/>'; var node = this.head; while (node != 0) { str += i + ': Node Value: ' + node.content; str += ' / Next Node Value: ' + node.next.content; str += " / Last Node Value: "; if (node.last == 0) str += ' null'; else str += node.last.content; i++; str += "<br>"; node = node.next; } return str; }; var lln = new DoubleLinkedList(); lln.add(' Head Node'); lln.add(' Second Node'); lln.add(' Third Node'); lln.add(' Fourth Node') lln.add(' Tail Node'); document.getElementById('output').innerHTML = lln.toString(); 
 <p id='output'></p>