Javascript 为什么将新节点添加到链接列表后,上一个节点会被设置为循环节点而不是_节点?

Javascript 为什么将新节点添加到链接列表后,上一个节点会被设置为循环节点而不是_节点?,javascript,linked-list,singly-linked-list,Javascript,Linked List,Singly Linked List,有人知道为什么将上一个节点设置为循环而不是\u节点 我正在尝试将新节点添加到链接列表的末尾。我希望prev成为\u节点。相反,它被设置为循环。在本练习之前,在我看到prev被设置为Circular之前,我不知道存在循环链接列表 控制台日志 LinkedList { head: _Node { value: 'Apollo', next: _Node { value: 'Boomer', next: [_Node], prev: [Circular] }, prev:

有人知道为什么将上一个节点设置为
循环
而不是
\u节点

我正在尝试将新节点添加到链接列表的末尾。我希望
prev
成为
\u节点
。相反,它被设置为
循环
。在本练习之前,在我看到
prev
被设置为
Circular
之前,我不知道存在循环链接列表

控制台日志

LinkedList {
  head: _Node {
    value: 'Apollo',
    next: _Node { value: 'Boomer', next: [_Node], prev: [Circular] },
    prev: null
  },
  size: 6
}
LinkedList.js

const _Node = require("./Node");

class LinkedList {
  constructor() {
    this.head = null;
    this.size = 0;
  }

  insertFirst(item) {
    if (this.head !== null) {
      const newHead = new _Node(item);
      let oldHead = this.head;

      oldHead.prev = newHead;
      newHead.next = oldHead;
      this.head = newHead;
    } else {
      this.head = new _Node(item, this.head);
    }

    this.size++;
  }

  insertLast(item) {
    if (!this.head) {
      this.insertFirst(item);
    } else {
      let tempNode = this.head;
      while (tempNode.next !== null) {
        tempNode = tempNode.next;
      }
      // *** I have no idea why prev becomes [Circular] ***
      tempNode.next = new _Node(item, null, tempNode);
    }
    this.size++
  }

  insertAt(item, index) {
    if (index > 0 && index > this.size) {
      return;
    }

    if (index === 0) {
      this.insertFirst(item);
      return;
    }

    const newNode = new _Node(item);
    let currentNode = this.head;
    let previousNode = this.head;

    currentNode = this.head;
    let count = 0;

    while (count < index) {
      previousNode = currentNode;
      currentNode = currentNode.next;
      count++;
    }
    previousNode.next = newNode;
    newNode.next = currentNode;
    this.size++;
  }
const LinkedList = require("./LinkedLists");

function main() {
  let SLL = new LinkedList();

  SLL.insertFirst("Apollo");
  SLL.insertLast("Boomer");
  SLL.insertLast("Helo");
  SLL.insertLast("Husker");
  SLL.insertLast("Starbuck");
  SLL.insertLast("Tauhida");

  return SLL;
}

console.log(main());

module.exports = main
class _Node {
  constructor(value, next, prev) {
    this.value = value;
    this.next = next || null;
    this.prev = prev || null;
  }
}

module.exports = _Node
Node.js

const _Node = require("./Node");

class LinkedList {
  constructor() {
    this.head = null;
    this.size = 0;
  }

  insertFirst(item) {
    if (this.head !== null) {
      const newHead = new _Node(item);
      let oldHead = this.head;

      oldHead.prev = newHead;
      newHead.next = oldHead;
      this.head = newHead;
    } else {
      this.head = new _Node(item, this.head);
    }

    this.size++;
  }

  insertLast(item) {
    if (!this.head) {
      this.insertFirst(item);
    } else {
      let tempNode = this.head;
      while (tempNode.next !== null) {
        tempNode = tempNode.next;
      }
      // *** I have no idea why prev becomes [Circular] ***
      tempNode.next = new _Node(item, null, tempNode);
    }
    this.size++
  }

  insertAt(item, index) {
    if (index > 0 && index > this.size) {
      return;
    }

    if (index === 0) {
      this.insertFirst(item);
      return;
    }

    const newNode = new _Node(item);
    let currentNode = this.head;
    let previousNode = this.head;

    currentNode = this.head;
    let count = 0;

    while (count < index) {
      previousNode = currentNode;
      currentNode = currentNode.next;
      count++;
    }
    previousNode.next = newNode;
    newNode.next = currentNode;
    this.size++;
  }
const LinkedList = require("./LinkedLists");

function main() {
  let SLL = new LinkedList();

  SLL.insertFirst("Apollo");
  SLL.insertLast("Boomer");
  SLL.insertLast("Helo");
  SLL.insertLast("Husker");
  SLL.insertLast("Starbuck");
  SLL.insertLast("Tauhida");

  return SLL;
}

console.log(main());

module.exports = main
class _Node {
  constructor(value, next, prev) {
    this.value = value;
    this.next = next || null;
    this.prev = prev || null;
  }
}

module.exports = _Node

此处,
Circular
不是对象类型,这意味着
console.log
找到了它正在打印的对象的引用,因此它停止了循环中的进一步操作。
head.next.prev
仍然是
\u Node
类型,但它是我们已经显示的
\u Node
对象

console.log(main())
尝试向您显示
头是什么时,接下来的
将尽其所能。它发现的是
head。下一个
是“Boomer”项,它的
prev
值指向
head
。因此,当它试图向您显示
head.next.prev
时,它会看到它指向它试图向您显示的对象(head)。这是一个循环条件,因为如果它试图继续,它会再次显示“阿波罗”,所以它会停止并输出“[循环]”,让你知道它是因为这个原因停止的。我试着把它画出来:

_Node: Apollo  <----------+  // this is the circular part
       next: Boomer  -+   |
       prev: null     |   |
_Node: Boomer  <------+   |
       next: Helo         |
       prev: Apollo  -----+

\u Node:Apollo此处
循环
不是对象类型,这意味着
console.log
找到了它正在打印的对象的引用,因此它停止了循环中的进一步操作。
head.next.prev
仍然是
\u Node
类型,但它是我们已经显示的
\u Node
对象

console.log(main())
尝试向您显示
头是什么时,接下来的
将尽其所能。它发现的是
head。下一个
是“Boomer”项,它的
prev
值指向
head
。因此,当它试图向您显示
head.next.prev
时,它会看到它指向它试图向您显示的对象(head)。这是一个循环条件,因为如果它试图继续,它会再次显示“阿波罗”,所以它会停止并输出“[循环]”,让你知道它是因为这个原因停止的。我试着把它画出来:

_Node: Apollo  <----------+  // this is the circular part
       next: Boomer  -+   |
       prev: null     |   |
_Node: Boomer  <------+   |
       next: Helo         |
       prev: Apollo  -----+
\u节点:阿波罗