首页 > 解决方案 > 如何从链表显示到控制台节点

问题描述

我有一个创建链接列表的类,还有一个将节点添加到该列表的函数。我正在尝试对列表实现更多功能,但我想通过显示整个列表来查看这些功能所做的更改。

这是代码:

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() 显示整个列表?

标签: javascriptdebugginglinked-listnodesconsole.log

解决方案



推荐阅读