javascript - 如何从链表显示到控制台节点
问题描述
我有一个创建链接列表的类,还有一个将节点添加到该列表的函数。我正在尝试对列表实现更多功能,但我想通过显示整个列表来查看这些功能所做的更改。
这是代码:
function LinkedList() {
var length = 0;
var head = null;
var Node = function(element) {
this.element = element;
this.next = null;
};
this.size = function() {
return length;
};
this.head = function() {
return head;
};
this.add = function(element) {
var node = new Node(element);
if (head === null) {
head = node;
} else {
var currentNode = head;
while (currentNode.next) {
currentNode = currentNode.next;
}
currentNode.next = node;
}
length++;
};
声明 LinkedList 类并使用 class.add(element) 函数添加元素后,如何使用 console.log() 显示整个列表?
解决方案
您需要编写toString
LinkedList 类的方法。请参阅:https ://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Object/toString
推荐阅读
- reactjs - React & Chartjs:如何在重新渲染时最好地破坏我的图表?
- python - 将带有嵌套标题的 CSV 转换为 JSON
- spring - 在自动装配中使用类型化集合时,这是 Intellij Idea 中的错误吗?
- rust - 如何在 actix-web 中接收多个同名的查询参数?
- reactjs - React - 删除按钮删除正确的条目,但在视觉上删除了底行
- shopify - Shopify 订单电子邮件模板有时会打印两次交易
- java - Axis2 客户端可以使用非 Axis SOAP 服务器(CXF 服务器)吗?
- android - 在 Android 11 (R) 上获取专辑封面
- timezone - 自动建议并详细获取时区
- python-3.x - python pdfplumber错误将pdf转换为jpg FailedToExecuteCommand`“gswin64c.exe”