将元素插入链接列表中的特定索引(Javascript)

发布于 2025-01-17 14:43:38 字数 1530 浏览 2 评论 0原文

我无法弄清楚为什么我的代码不起作用,而我一直在盯着它。当我尝试调用该函数时,我会收到错误“无法读取null的属性(读取'next')”,并且由于我正在重新分配Curr并通过列表迭代,因此不会期望发生这种错误。

// insert an item at the position specified
LinkedList.prototype.insert = function(data, position) {
  let newNode = new Node(data); //instantiate a new node
  if (position < 0 || position > this.size) { // if the position is less than zero or greater than the size of the linked list itself
    return 'Please enter a valid position.' // return error statement
  } else {
    if (this.head === null) { // if we do not have any nodes in the list)
      this.head = newNode; // point newNode's pointer to the head
      this.tail = newNode; // point the head pointer to the newNode;
    } else {
      let prev = null; // set up a variable to hold the prev value
      let curr = this.head; // set a current variable to this.head (the start)
      let currIndex = 0;
      while (currIndex < position) { // while the current index we're at is less than the position, we need to keep iterating
        prev = curr; // set prev variable equal to curr
        curr = curr.next; // skip to next node until we find the right position
      }
      newNode.next = curr; // after we find the position, set the new node's next to equal the current
      prev.next = newNode; // also set the previous node's next to equal the new node
    }
    this.size++;
  }
}

console.log(myList)
myList.insert(4,1)
console.log(myList)

I cannot figure out why my code isn't working and I've been staring at it forever. When I try to call the function, I get the error "Cannot read properties of null (reading 'next')" and wouldn't have expected this to happen since I am re-assigning both curr and prev to iterate through the list.

// insert an item at the position specified
LinkedList.prototype.insert = function(data, position) {
  let newNode = new Node(data); //instantiate a new node
  if (position < 0 || position > this.size) { // if the position is less than zero or greater than the size of the linked list itself
    return 'Please enter a valid position.' // return error statement
  } else {
    if (this.head === null) { // if we do not have any nodes in the list)
      this.head = newNode; // point newNode's pointer to the head
      this.tail = newNode; // point the head pointer to the newNode;
    } else {
      let prev = null; // set up a variable to hold the prev value
      let curr = this.head; // set a current variable to this.head (the start)
      let currIndex = 0;
      while (currIndex < position) { // while the current index we're at is less than the position, we need to keep iterating
        prev = curr; // set prev variable equal to curr
        curr = curr.next; // skip to next node until we find the right position
      }
      newNode.next = curr; // after we find the position, set the new node's next to equal the current
      prev.next = newNode; // also set the previous node's next to equal the new node
    }
    this.size++;
  }
}

console.log(myList)
myList.insert(4,1)
console.log(myList)

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

浅忆流年 2025-01-24 14:43:38

当我弄清楚出了什么问题时就结束了。我忘记在 while 循环中增加 currIndex ,哈哈。

Closing this out as I figured out what was wrong. I was forgetting to increment currIndex within the while loop, haha.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文