【问题标题】:javascript - simple linked list traversal issuejavascript - 简单的链表遍历问题
【发布时间】:2021-04-12 06:54:50
【问题描述】:

我已经使用 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();

但是 traverse 方法从不打印最后一个元素。我在这里想念什么?虽然 insertAtTail 方法看起来是正确的。谁能告诉我。

谢谢

【问题讨论】:

    标签: javascript data-structures linked-list traversal


    【解决方案1】:

    traversecurrent.nextElementnull 时停止循环——但此时current 仍然是data 的节点,只是它没有next 节点。

    相反,继续直到节点本身为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) {
    

    可能

    while (current !== null) {
    

    如果你愿意的话。)

    现场示例:

    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) {
          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();

    【讨论】:

      【解决方案2】:

      在遍历中,需要检查所有节点,直到下一个为空。

      所以我刚刚从遍历中删除了 .nextElement,它工作正常

      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) { // Here
            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();

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 2013-07-28
        • 2021-01-05
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2011-08-29
        • 2019-02-26
        • 1970-01-01
        相关资源
        最近更新 更多