gpt4 book ai didi

javascript - 5 个 Node 的双链表

转载 作者:太空宇宙 更新时间:2023-11-04 00:38:49 24 4
gpt4 key购买 nike

我无法指向最后一个 Node 。

输出应该如下所示:

具有 5 个 Node 的链表

  1. Node 值:头 Node/下一个 Node 值:第二个 Node/最后一个 Node 值:null

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

  3. Node 值:第三个 Node/下一个 Node 值:第四个 Node/最后一个 Node 值:第二个 Node

  4. Node 值:第四个 Node/下一个 Node 值:尾 Node/最后一个 Node 值:第三个 Node

  5. Node 值:尾 Node/下一个 Node 值:未定义/最后一个 Node 值:第四个 Node

但是,我不断得到这个:

具有 5 个 Node 的链表

  1. Node 值:头 Node/下一个 Node 值:第二个 Node/最后一个 Node 值:未定义

  2. Node 值:第二个 Node/下一个 Node 值:第三个 Node/最后一个 Node 值:未定义

  3. Node 值:第三个 Node/下一个 Node 值:第四个 Node/最后一个 Node 值:未定义

  4. Node 值:第四个 Node/下一个 Node 值:尾 Node/最后一个 Node 值:未定义

  5. Node 值:尾 Node/下一个 Node 值:未定义/最后一个 Node 值: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>

您没有在 add 方法中设置 last 值,并且在 toString 方法中犯了一些错误。

关于javascript - 5 个 Node 的双链表,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/37755613/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com