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

时间:2019-03-06 02:03:27

标签: javascript doubly-linked-list

我想从单链接列表构建双链接列表

,输出为[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]

1 个答案:

答案 0 :(得分: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]