- android - 多次调用 OnPrimaryClipChangedListener
- android - 无法更新 RecyclerView 中的 TextView 字段
- android.database.CursorIndexOutOfBoundsException : Index 0 requested, 光标大小为 0
- android - 使用 AppCompat 时,我们是否需要明确指定其 UI 组件(Spinner、EditText)颜色
我无法指向最后一个 Node 。
输出应该如下所示:
具有 5 个 Node 的链表
Node 值:头 Node/下一个 Node 值:第二个 Node/最后一个 Node 值:null
Node 值:第二个 Node/下一个 Node 值:第三个 Node/最后一个 Node 值:头 Node
Node 值:第三个 Node/下一个 Node 值:第四个 Node/最后一个 Node 值:第二个 Node
Node 值:第四个 Node/下一个 Node 值:尾 Node/最后一个 Node 值:第三个 Node
Node 值:尾 Node/下一个 Node 值:未定义/最后一个 Node 值:第四个 Node
但是,我不断得到这个:
具有 5 个 Node 的链表
Node 值:头 Node/下一个 Node 值:第二个 Node/最后一个 Node 值:未定义
Node 值:第二个 Node/下一个 Node 值:第三个 Node/最后一个 Node 值:未定义
Node 值:第三个 Node/下一个 Node 值:第四个 Node/最后一个 Node 值:未定义
Node 值:第四个 Node/下一个 Node 值:尾 Node/最后一个 Node 值:未定义
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/
好吧,我的教授(数据结构课)布置了这个:你的任务是编写一个程序来更新双向链表中的字符访问频率。该程序应一次从包含许多字符的文本文件中读取一个字符。为了方便起见,不要计算空格。每次访问一个字符时,将其访
嗨我想知道如何将我的对象从 arrayList 复制到双向链表?我的 DNode 构造函数也是: public DNode(Object element, DNode prev, DNode
我想实现 Split 函数来学习 C 的困难方式双链表,但要做到这一点,我需要每个节点都有其索引号,就像常规列表、数组一样。当我执行“Push”功能(将新节点添加到列表末尾)时,一切都很好,但是当我执
struct Node { int data; Node *next; Node *prev; }; class DoublyLinkedList { ofstream cout3; Node *he
我刚刚开始学习 C,并且(似乎)到目前为止,大多数东西都在点击。但是,我在尝试使用双链表 时遇到了一些问题。当我尝试构建/运行此代码时,我不断收到 seg-fault。我正在通过 NetBeans 使
我试图分两部分实现双链表:第一个是创建列表的实际功能;第二个是模拟器 - 包含一些线程、读取器和写入器(每个线程都在 while 循环中弹出和插入双链表),以及一个垃圾收集器线程,如果列表太大(根据
我正在分析我要在其上构建双向链表的这段代码: struct dplist_node { dplist_node_t * prev, * next; element_t element; };
我正在尝试制作一个简单的双链表,我首先使用了 (switch): int choice, data; switch(choice) { case 1:
我是 C 编程的初学者。我的任务是使用双链表创建学生列表。该应用程序应具有三个功能:显示列表、添加新学生和通过 ID 号删除学生。我做到了,它运行得很好。我想请教几个问题: 是否使用不当? 如果有缩短
我需要在 C 中的双链表中,但它必须用于不同的类型。在 C++ 中,我们为它使用模板。我在哪里可以找到 C 中带有抽象类型项的双链表的示例。 谢谢 最佳答案 您可以采用几种方法,其中一种涉及在您的 A
这个问题不太可能帮助任何 future 的访问者;它只与一个小的地理区域、一个特定的时间点或一个非常狭窄的情况有关,这些情况并不普遍适用于互联网的全局受众。为了帮助使这个问题更广泛地适用,visit
最近我开始学习 C++,并且开始玩简单的结构。我在双链表上苦苦挣扎,我被困在“prev”指针上;/“Next”指针工作正常但“prev”不工作,我不知道为什么。 #ifndef _DOUBLELIST
在此处提问之前,我会尽力查找问题的解决方案,但我遇到了一些困难。虽然有一个用于 Java 的,但它并没有帮助我理解我的实现哪里出了问题。因此,事不宜迟,这里是一些背景,然后是问题。 尝试此操作的背景/
双链表是一种重要的线性存储结构,对于双链表中的每个节点,不仅仅存储自己的信息,还要保存前驱和后继节点的地址。 PHP SPL中的SplDoublyLinkedList类提供了对双链表的操作。
我的 DLL 插入函数有问题。附加到列表上没有问题,但是当我有一个包含 5 个对象的列表并且我想插入节点之间时,它什么也不做。我一直在四处寻找数小时来解决这个问题,但没有任何改变。 这是我的代码:在
我有一个具有这些属性的 class Node: int value; Node* next; Node* prev; 当我初始化列表以了解哪个是第一个节点时,我有一个带有属性 Node* first
我已经编写了这段代码,一般来说效果很好,但是当我们到达 i == 74 列表元素为 4 - 11 - 18 - 4 - 10 - 18 - 17 - 22 - 14 - 29 和 swapNodes(
你能帮我弄清楚为什么我会收到这些 2019 错误吗?我很确定所有文件都保存在正确的位置,而且我认为我对头文件使用了正确的约定?这是我的系统编程类(class)的实验室。 错误如下: 1>main.ob
我是一名优秀的程序员,十分优秀!