微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

Javascript 中的双链表节点 [Circular]

如何解决Javascript 中的双链表节点 [Circular]

class LinkedList{
  constructor(value){
    this.head = {
      data: value,next: null,prev: null
    }
    this.tail = this.head
    this.length = 1

  }
  append(val){
    const node = {
      data: val,prev: this.tail
    }
    this.tail.next = node
    this.tail = node
    this.length++ 
    return this
  }
  prepend(val){
    const node = {
      data: val,prev: null
    }
    node.next = this.head
    this.head.prev = node
    this.head = node
    this.length++
    return this
  }
  insert(value,pos){
    let i = 1
    let target = this.head
    while (i!=pos){
      target = target.next
      i++
    }
    const node = {
      data: value,prev: null
    }
    node.next = target.next
    let nextNode = target.next
    nextNode.prev = node
    node.prev = target
    target.next = node 
    this.length++
  }
  remove(i){
    let counter = 1
    let currentNode = this.head
    while (counter!= i){
      currentNode = currentNode.next
      counter++
    }

    let temp = currentNode.next 
    currentNode.next = temp.next
    let nextNode = temp.next 
    nextNode.prev = currentNode
    temp.next = null 
    temp.prev = null
    this.length--
  }
  printNodes(){
    const nodeArray = []
    let currentNode = this.head;
    while (currentNode){
      nodeArray.push(currentNode.data.toString())
      currentNode = currentNode.next
    }
    console.log(nodeArray.join("-->"))
  }
}
const lili = new LinkedList(5)
lili.append(12)
lili.append(18)
lili.append(15)
lili.insert(14,2)
lili.printNodes()
lili.remove(2)
console.log(lili)

我在 Javascript 中完成了双重 LinkedList 的不完整实现。第二个节点的 prev 属性和倒数第二个节点的 next 属性显示 [Circular] 而不是 [Object]。有人可以解释一下什么是 Circular 以及为什么会发生这种情况吗?谢谢。

解决方法

你不应该打印底层数据结构,因为所有其他节点都指向它们的邻居。我还翻转了 insert 方法的参数,因为位置通常是许多标准库中的第一个参数。

相反,您需要设计一个迭代器,允许您从头到尾遍历列表一次。在下面的示例中,我修改了 LinkedList 类中的以下 class iterator。您可以在此处找到有关迭代器的更多信息:MDN: Iterators and generators

您可以通过简单的展开快速遍历列表。

printNodes() {
  console.log(`List nodes: ${[...this].join("-->")}`);
}

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

class LinkedList {
  constructor(value) {
    this.head = this.tail = null;
    this.length = 1
  }
  append(data) {
    const node = new Node(data);  
    if (this.head == null) {
      this.head = this.tail = node;  
      this.head.prev = null;  
      this.tail.next = null;  
    } else {
      this.tail.next = node;  
      node.prev = this.tail;  
      this.tail = node;  
      this.tail.next = null;  
    }
    this.length++;
    return this;
  }
  prepend(data) {
    const node = new Node(data);  
    if (this.head == null) {
      this.head = this.tail = node;  
      this.head.prev = null;  
      this.tail.next = null;  
    } else {
      this.tail.prev = node;  
      node.next = this.head;  
      this.head = node;  
      this.tail.prev = null;  
    }
    this.length++;
    return this;
  }
  get(pos) {
    let i = 0,currentNode = this.head;
    while (currentNode) {
      if (i === pos) return currentNode;
      currentNode = currentNode.next;
      i++;
    }
    return null;
  }
  insert(pos,value) {
    const target = this.get(pos),node = new Node(value);
    node.next = target.next;
    const nextNode = target.next;
    nextNode.prev = node;
    node.prev = target;
    target.next = node;
    this.length++;
    return this;
  }
  remove(pos) {
    let counter = 1,currentNode = this.head;
    while (counter != pos) {
      currentNode = currentNode.next;
      counter++;
    }
    const temp = currentNode.next;
    currentNode.next = temp.next;
    const nextNode = temp.next;
    nextNode.prev = currentNode;
    temp.next = null;
    temp.prev = null;
    this.length--;
  }
  // See: https://stackoverflow.com/a/45849918/1762224
  *iterator() {
    let currentNode = this.head;
    while (currentNode) {
      const { data } = currentNode;
      currentNode = currentNode.next;
      yield data;
    }
  }
  [Symbol.iterator]() {
    return this.iterator();
  }
  printNodes() {
    if (this.head == null) {
      console.log('List is empty');
      return;
    }
    console.log(`List nodes: ${[...this].join("-->")}`);
  }
}

const list = new LinkedList()
  .append(5)
  .prepend(12)
  .append(18)
  .append(15)
  .insert(2,14);
list.printNodes();

list.remove(2);
list.printNodes();
.as-console-wrapper { top: 0; max-height: 100% !important; }

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。