plastikaweb
12/4/2017 - 7:12 PM

data structure linked list - addToHead() addToTail() - https://repl.it/@plastikaweb/LightblueTomatoRaven

data structure linked list - addToHead() addToTail() - https://repl.it/@plastikaweb/LightblueTomatoRaven

const es6 = require('es6');
function LinkedList() {
  this.head = null;
  this.tail = null;
}

LinkedList.prototype.addToHead = function(value) {
  const newNode = new Node(value, this.head, null);
  if (this.head) {
    this.head.prev = newNode;
  } else {
    this.tail = newNode;
  }
  this.head = newNode;
};

LinkedList.prototype.addToTail = function(value) {
  const newNode = new Node(value, null, this.tail);
  if (this.tail) {
    this.tail.next = newNode;
  } else {
    this.head = newNode;
  }
  this.tail = newNode;
};

function Node(value, next, prev) {
  this.value = value;
  this.next = next;
  this.prev = prev;
}

var LL = new LinkedList();

LL.addToHead(100);
LL.addToHead(200);
LL.addToHead(300);

console.log(LL);