JSFiddle - React, Tailwind, and code Playground

by Eugen Sunic

JavaScript

class DoublyLinkedListNode {
  constructor(data) {
    this.data = data;
    this.next = null;
    this.previous = null;
  }
}



class DoublyLinkedList {

  constructor() {
    this.head = null;
    this.tail = null;
  }

  add(value) {
    const node = new DoublyLinkedListNode(value);
    if (!this.head) {
      this.head = node;
    } else {
      this.tail.next = node;
      node.previous = this.tail
    }
    this.tail = node;
  }

  get(index) {
    var i = 0;
    var current = this.head;
    while (i !== index) {
      current = current.next;
      i++
    }
    return current;
  }


  remove(index) {
    // pointer use to traverse the list
    let current = this.head
    let i = 0;

    while (i !== index) {
      current = current.next;
			i++
    }
		console.log(current)
    current.previous.next = current.next;


  }

}

const list = new DoublyLinkedList();
list.add(11);
list.add(22);
list.add(33);
list.add(44);
list.add(55);

list.remove(2)
console.log(list)