javascript-简单链表遍历问题

javascript-简单链表遍历问题,javascript,data-structures,linked-list,traversal,Javascript,Data Structures,Linked List,Traversal,我已经使用javascript实现了一个单一的链表。请查找以下代码: class Node { constructor(data) { this.data = data; this.nextElement = null; } } class LinkedList { constructor() { this.head = null; } isEmpty() { return this.head === null; } insert

我已经使用javascript实现了一个单一的链表。请查找以下代码:

class Node {
  constructor(data) {
    this.data = data;
    this.nextElement = null;
  }
}

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

  isEmpty() {
    return this.head === null;
  }

  insertAtHead(data) {
    const tempNode = new Node(data);
    tempNode.nextElement = this.head;
    this.head = tempNode;
  }

  traverse() {
    let current = this.head;
    while (current.nextElement != null) {
      console.log("node data", current.data);
      current = current.nextElement;
    }
  }

  insertAtTail(data) {
    const tempNode = new Node(data);
    if (this.head === null) {
      this.head = tempNode;
      return;
    }

    let currentNode = this.head;
    while (currentNode.nextElement != null) {
      currentNode = currentNode.nextElement;
    }

    currentNode.nextElement = tempNode;
  }
}

const linkedList = new LinkedList();
linkedList.insertAtTail(12);
linkedList.insertAtTail(23);
linkedList.insertAtTail(25);

linkedList.traverse();
但是遍历方法从不打印最后一个元素。我错过了什么?不过insertAtTail方法看起来是正确的。有人能告诉我吗


感谢
当前时,遍历
停止其循环。nextElement
null
-但在这一点上,
当前
仍然是一个包含
数据的节点,只是它后面没有下一个节点

相反,继续运行,直到节点本身为
null

traverse() {
  let current = this.head;
  while (current) { // *** Only change is on this line
    console.log("node data", current.data);
    current = current.nextElement;
  }
}
(那

可能是

while (current !== null) {
如果你愿意的话。)

实例:

类节点{
建造师(数据){
这个数据=数据;
this.nextElement=null;
}
}
类链接列表{
构造函数(){
this.head=null;
}
isEmpty(){
返回this.head==null;
}
插入日期(数据){
const tempNode=新节点(数据);
tempNode.nextElement=this.head;
this.head=tempNode;
}
导线测量(){
让电流=这个头;
while(当前){
日志(“节点数据”,当前数据);
current=current.nextElement;
}
}
插入附件(数据){
const tempNode=新节点(数据);
if(this.head==null){
this.head=tempNode;
返回;
}
让currentNode=this.head;
while(currentNode.nextElement!=null){
currentNode=currentNode.nextElement;
}
currentNode.nextElement=tempNode;
}
}
const linkedList=new linkedList();
linkedList.insertAtTail(12);
linkedList.insertAtTail(23);
linkedList.insertAtTail(25);

linkedList.traverse()在遍历中,需要检查所有节点,直到下一个节点为空

所以我刚从遍历中删除了.nextElement,它工作得很好

类节点{
建造师(数据){
这个数据=数据;
this.nextElement=null;
}
}
类链接列表{
构造函数(){
this.head=null;
}
isEmpty(){
返回this.head==null;
}
插入日期(数据){
const tempNode=新节点(数据);
tempNode.nextElement=this.head;
this.head=tempNode;
}
导线测量(){
让电流=这个头;
while(当前){//Here
日志(“节点数据”,当前数据);
current=current.nextElement;
}
}
插入附件(数据){
const tempNode=新节点(数据);
if(this.head==null){
this.head=tempNode;
返回;
}
让currentNode=this.head;
while(currentNode.nextElement!=null){
currentNode=currentNode.nextElement;
}
currentNode.nextElement=tempNode;
}
}
const linkedList=new linkedList();
linkedList.insertAtTail(12);
linkedList.insertAtTail(23);
linkedList.insertAtTail(25);
linkedList.traverse()
while (current !== null) {