将单向链表更改为双向链表

Changed singly linkedList to doubly linkedList

我想从单链表构建双链表

输出为[1,10,5,16]。

我认为问题出在附加、插入和删除函数中

谁能帮我改一下单链表?

我对双向链表的了解是有两个节点

一点到下一个值

秒指向上一个值

这是我的 JS:

class DoublyLinkedList {
  constructor(value) {
    this.head = {
     value: value,
     previous:null,
     next: null
   };
   this.tail = this.head;
   this.length = 1;
  }
  append(value) {
    const newNode = {
     value: value,
     previous:null,
      next: null

   }
   this.tail.next = newNode;
  newNode.previous=this.head
  this.tail = newNode;
   this.length++;
   return this;
   }
   prepend(value) {
    const newNode = {
     value: value,
     next: null
    }
   newNode.next = this.head;
   this.head = newNode;
    this.length++;
     return this;
   }
   printList() {
     const array = [];
     let currentNode = this.head;
     while(currentNode !== null){
      array.push(currentNode.value)
       currentNode = currentNode.next
     }
    return array;
     }
     insert(index, value){
     //Check for proper parameters;
      if(index >= this.length) {
      console.log('yes')
       return this.append(value);
       }

      const newNode = {
      value: value,
      next: null
      previous:null
      }
      const leader = this.traverseToIndex(index-1);
      const holdingPointer = leader.next;
      leader.next = newNode;
      newNode.previous = this.head
      newNode.next = holdingPointer;
      this.length++;
      return this.printList();
      }
      traverseToIndex(index) {
       //Check parameters
      let counter = 0;
      let currentNode = this.head;
      while(counter !== index){
        currentNode = currentNode.next;
        counter++;
       }
      return currentNode;
      }
      remove(index) {
       // Check Parameters      
      const leader = this.traverseToIndex(index-1);
      const unwantedNode = leader.next;
      unwantedNode.previous=leader.head;
      leader.next = unwantedNode.next;
       this.length--;
      return this.printList();
       }
      }

     let myDoublyLinkedList = new DoublyLinkedList(10);
     myDoublyLinkedList.append(5);
     myDoublyLinkedList.append(16);
     myDoublyLinkedList.prepend(1);
     myDoublyLinkedList.insert(2, 6);
     myDoublyLinkedList.remove(2);//Should return [1,10,5,16]

我更新了 appendprepend 函数。现在应该可以正确显示了

class DoublyLinkedList {
  constructor(value) {
    this.head = {
      value: value,
      previous:null,
      next: null
    };
    this.tail = this.head;
    this.length = 1;
  }
  append(value) {
    const newNode = {
      value: value,
      previous:null,
      next: null
    }
    this.tail.next = newNode;
    newNode.previous=this.tail;
    this.tail = newNode;
    this.length++;
    return this;
  }
  prepend(value) {
    const newNode = {
      value: value,
      previous:null,
      next: null

    }
    this.head.previous = newNode;
    newNode.next = this.head;
    this.head = newNode;
    this.length++;
    return this;
  }
  printList() {
    const array = [];
    let currentNode = this.head;
    while(currentNode !== null){
      array.push(currentNode.value)
      currentNode = currentNode.next
    }
    return array;
  }
  insert(index, value){
    //Check for proper parameters;
    if(index >= this.length) {
      console.log('yes')
      return this.append(value);
    }

    const newNode = {
      value: value,
      next: null,
      previous:null
    }
    const leader = this.traverseToIndex(index-1);
    const holdingPointer = leader.next;
    leader.next = newNode;
    newNode.previous = leader;
    newNode.next = holdingPointer;
    this.length++;
    return this.printList();
  }
  traverseToIndex(index) {
    //Check parameters
    let counter = 0;
    let currentNode = this.head;
    while(counter !== index){
      currentNode = currentNode.next;
      counter++;
    }
    return currentNode;
  }
  remove(index) {
    // Check Parameters      
    const leader = this.traverseToIndex(index-1);
    const unwantedNode = leader.next;
    leader.next = unwantedNode.next;
    unwantedNode.next.previous=leader;
    this.length--;
    return this.printList();
  }
}

let myDoublyLinkedList = new DoublyLinkedList(10);
myDoublyLinkedList.append(5);
myDoublyLinkedList.append(16);
myDoublyLinkedList.prepend(1);
myDoublyLinkedList.insert(2, 6);
myDoublyLinkedList.remove(2);//Should return [1,10,5,16]