javascript - 链表 - 查找索引处的第 N 个元素

标签 javascript linked-list

我的代码在查找链接列表的特定索引处的元素时遇到问题。

  findElement(index) {
    let currentNode = this.head;
    let count = 0;

    while (currentNode != null) {
      if (count === index) {
        return currentNode;
        count++;
        currentNode = currentNode.next;
      }
      return -1;
    }
  }

当我这样做时,我得到的是整个链表而不是一个特定的节点。因此,如果我 console.log(list.findElement(0)),我会得到整个链接列表。但是如果我控制台日志console.log(list.findElement(1)),我得到-1。但我想要的是第二个节点。下面是我的其余代码。不完全确定我的 findElement 函数出了什么问题。

class Node {
  constructor(value) {
    this.value = value;
    this.next = null;
  }
}

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

    //Length
    this.length = 0;
  }

  //Push-front
  pushFront(value) {
    let node = new Node(value);

    node.next = this.head;

    this.head = node;

    this.length++;
  }

  //Pop-front
  popFront() {
    if (this.head != null) {
      this.head = this.head.next;
    }
    this.length--;
  }

  //Push-back
  pushBack(value) {
    let node = new Node(value);

    if (this.head === null) {
      this.head = node;
    } else {
      let currentNode = this.head;

      while (currentNode.next) {
        currentNode = currentNode.next;
      }
      currentNode.next = node;
    }
    this.length++;
  }

最佳答案

findElement 函数中的逻辑存在一些问题。主要问题是 count 永远不会从 0 开始变化,因此该函数仅在 head 是查找元素时才有效(例如 index === 0)并返回 -1 在任何其他输入上(此“失败”返回应完全移至 while 循环之外)。

这是一个将 count++currentNode = currentNode.next 移出 if 并移至隐式 else 的版本>:

  findElement(index) {
    let currentNode = this.head;
    let count = 0;

    while (currentNode) {
      if (count === index) {  // found the element
        return currentNode;
      }
      
      count++;  // increment counter
      currentNode = currentNode.next;  // move to next node
    }
    
    return -1;
  }

另一个问题是,如果在空列表上调用,您的 popFront 会将列表的长度减少到 -1。减少和删除都应该是有条件的。这可能会在将来的实现中造成损害,但由于您从不使用列表长度,因此可以将其完全删除。

把它们放在一起,这是一个测试程序:

class Node {
  constructor(value) {
    this.value = value;
    this.next = null;
  }
}

class LinkedList {
  constructor() {
    this.head = null;
    this.length = 0;
  }
  
  findElement(index) {
    let currentNode = this.head;
    let count = 0;

    while (currentNode) {
      if (count === index) {
        return currentNode;
      }
      
      count++;
      currentNode = currentNode.next;
    }
    
    return -1;
  }

  pushFront(value) {
    const node = new Node(value);
    node.next = this.head;
    this.head = node;
    this.length++;
  }

  popFront() {
    if (this.head != null) {
      this.head = this.head.next;
      this.length--;
    }
  }

  pushBack(value) {
    const node = new Node(value);

    if (this.head === null) {
      this.head = node;
    } 
    else {
      let currentNode = this.head;

      while (currentNode.next) {
        currentNode = currentNode.next;
      }
      
      currentNode.next = node;
    }
    
    this.length++;
  }
}


const ll = new LinkedList();
ll.pushBack(1);
ll.pushBack(2);
ll.pushBack(3);
console.log(ll);
console.log(`First node: ${ll.findElement(0).value}`);
console.log(`Second node: ${ll.findElement(1).value}`);
console.log(`Third node: ${ll.findElement(2).value}`);
console.log(`Invalid index: ${ll.findElement(22).value}`);

关于javascript - 链表 - 查找索引处的第 N 个元素,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/51996509/

相关文章:

javascript - contenteditable 中的空格导致换行

javascript - 嵌入式谷歌地图 : stop the callout from automatically appearing?

java - Java冒泡排序双向链表

javascript - 来自 R 的字符串作为 JS() 中的 HTML

javascript - SOAPUI:SIMple Groovy 脚本 - 导入语句存在语法错误?

javascript - 在 RegExp 回溯断言中转义字符

使用具有已定义结构的方法进行 C 编程

c - 以这种方式分配对象非法吗?

c++ - System.AccessViolationException 试图读取或写入 protected 内存

c++ - 无法使 strstr() 工作