【问题标题】:Removing a reference to a deleted node in a Doubly Linked Linked List在双向链接列表中删除对已删除节点的引用
【发布时间】:2020-04-10 16:40:32
【问题描述】:

我可以成功删除双向链表中的一个节点,但我的 node.next 仍然“指向”已删除的节点,而旧节点的 prev 仍“指向”已删除的节点。当我打印列表时节点不存在,并且在调用 findNode() 时,返回符合预期('没有具有该值的节点')

这是我的课:

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

class LinkedList {
  constructor() {
      this.head = null
      this.tail = this.head
      this.size = 0
    }

  findNode(value) {
    let node = this.head
        while(node !== null && node.value !== null) {
       node = node.next
    }
        return node !== null
  }

  remove(value) {
    let node = this.head
    while(node.next !== null) {
      if(node.value === value) {
        const newCurrentNode = node.prev
        const newNextNode = node.next
        newCurrentNode.next = newNextNode
        newNextNode.prev = newCurrentNode
      }
      node = node.next
      if(node.next === null) {
        return 'node node with that value'
      }
    }
    this.size--
    return this.print()
  }

  removeAtIndex(position) {
    let node = this.head
    if(position < 1) this.removeHead(value)
    if(position > this.size) return 'no node at that position'
    let count = 0
    while(count !== position){
      console.log(`${count} is not at position`)
      node = node.next
      count++
    }
    // console.log(`${count} is at position`)
    const prevNode = node.prev
    const newNextNode = node.next
    prevNode.next = newNextNode
    newNextNode.prev = prevNode
    node.next = null
    node.prev = null
    this.size--
    return this.print() // change to return this for LL
}

 insertNewHead(value) {
    const newHead = new Node(value)
    const oldHead = this.head
    if(!this.head) {
      this.head = newHead
      this.size++
    } else {
      newHead.next = oldHead
      oldHead.prev = newHead
      this.head = newHead
      this.size++
    }
    return this.head
  }

  insert(value) {
    const newNode = new Node(value)
    const prevNode = this.tail
    if(!this.head) {
      this.head = newNode
    } else {
      this.tail.next = newNode
      newNode.prev = prevNode
    }
    this.tail = newNode
    this.size++
    return this
  }

insertAtIndex(value, position) {
    const node = new Node(value)
    if(position < 1) this.insertNewHead(value)
    if(position > this.size) {
      this.tail.next = node
      node.prev = this.tail
      this.tail = node
      this.size++
      return this
    }
    let count = 0
    let previous = this.head 
    let current = previous.next
    while(count !== position -1) {
      previous = current
      current = current.next
      count++
    }
    node.next = current
    previous.next = node
    node.prev = previous
    this.size++
    return node
  }

  insertBefore(value, position) {
    return this.insertAtIndex(value, position - 1)
  }

  insertAfter(value, position) {
    return insertAtIndex(value, position + 1)
  }

  forEach(cb) {
    let node = this.head;
    while (node) {
      cb(node.value);
      node = node.next;
    }
  }

  print() {
    let result = [];
    this.forEach(function(value) {
      result.push(value);
      });
    return result.join(', ');
  }
removeHead(){
  let oldHead = this.head;
  let newHead = oldHead.next;
  this.head = newhead;
  oldHead.next = null;
  this.size--;
  return oldHead;
}

}

在使用 print() 之前 7、5、1、2、3、8、6

ll.removeAtIndex(4) 7、5、1、2、8、6

但是,我仍然看到这个,我仍然看到 6 prev 指向 3

LinkedList {
  head:
   Node {
     value: 7,
     next: Node { value: 5, next: [Node], prev: [Circular] },
     prev: null },
  tail:
   Node {
     value: 6,
     next: null,
     prev: Node { value: 3, next: null, prev: null } },
  size: 6 }

我是否正确重置了节点指针?我这是一个预期的结果?这个节点会被垃圾回收吗? 6.prev 不应该指向 8 吗?

向列表中添加元素的代码:

const ll = new LinkedList()
ll.insert(1)
ll.insert(2)
ll.insert(3)
ll.findNode(1)
ll.insertNewHead(7)
ll.insertAtIndex(5,1)
ll.tail
ll.insertBefore(6, 10)
ll.insertAtIndex(8, 5)
ll.print()
ll.removeAtIndex(4)
console.log(ll)
ll.print() 

【问题讨论】:

  • 您的函数无法正确处理 tail 和 size。此外,您忘记发布您如何构建列表、print 方法和removeHead 方法。
  • 如果调用之前的列表是7, 5, 1, 2, 3, 8, 6,那么值为 6 的节点应该永远指向值为 3 的节点。还有其他问题。跨度>
  • 添加了插入方法,print 和 removeHead 不相关。你是对的,我没有注意到 6 指向 3 预删除。
  • 是的,那么删除方法确实是相关的。但是您仍然没有显示用于构建损坏列表的代码!
  • 进行了修改,添加了 removeHead()、print()、forEach()

标签: javascript data-structures linked-list


【解决方案1】:

问题不在于removeAtIndex,而在于insertAtIndex,它确实分配了node.next = current,但没有分配current.prev = node。这使得节点 6 仍然引用节点 3,而它确实应该引用节点 8。

【讨论】:

  • 所以后面的代码块应该是:`node.next = current; current.prev = 节点;上一个.下一个 = 节点; node.prev = 上一个; `
  • @KevinT。正是
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2011-03-12
  • 2015-04-15
  • 1970-01-01
相关资源
最近更新 更多