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

问题描述 投票:1回答:1

我想从单链表中建立双链表

输出为[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]
javascript doubly-linked-list
1个回答
0
投票

我更新了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]
© www.soinside.com 2019 - 2024. All rights reserved.