首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >链表-在索引处查找第N个元素

链表-在索引处查找第N个元素
EN

Stack Overflow用户
提问于 2018-08-24 10:03:23
回答 2查看 513关注 0票数 0

在查找链表的特定索引处的元素时,我的代码出现了问题。

代码语言:javascript
复制
  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函数出了什么问题。

代码语言:javascript
复制
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++;
  }
EN

回答 2

Stack Overflow用户

回答已采纳

发布于 2018-08-24 10:47:31

您的findElement函数中的逻辑存在一些问题。主要问题是count永远不会从0开始更改,因此该函数仅在head是查找的元素(例如index === 0)并在任何其他输入上返回-1时才起作用(此"fail“返回应完全移出while循环)。

下面是一个将count++currentNode = currentNode.nextif移到隐式else中的版本

代码语言:javascript
复制
  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。递减应该是有条件的,也应该是删除的。这可能会在将来的实现中造成危害,但由于您从不使用列表长度,因此可以将其完全删除。

把这些放在一起,下面是一个测试程序:

代码语言:javascript
复制
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) {
    let 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) {
    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++;
  }
}


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}`);

票数 1
EN

Stack Overflow用户

发布于 2018-08-24 10:07:37

您有一条return语句作为if (count === index)条件的第一行,这将阻止进一步的代码执行(这意味着永远不会到达currentNode = currentNode.next )。

您希望将return currentNode向下移动两行,以便在函数返回之前引用后续节点。

代码语言:javascript
复制
findElement(index) {
  let currentNode = this.head;
  let count = 0;

  while (currentNode != null) {
    if (count === index) {
      count++;
      currentNode = currentNode.next;
      return currentNode;
    }
    return -1;
  }
}
票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/51996509

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档