LinkedList, add to head, add to tail - Node.js Data Structure

Node.js examples for Data Structure:List

Description

LinkedList, add to head, add to tail

Demo Code


function LinkedList() {
  this.head = null;//w ww. j a v a  2  s  . c  om
  this.tail = null;
}
function Node(value, next, prev) {
  this.value = value;
  this.next = next;
  this.prev = prev;
}

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

LinkedList.prototype.addTotail = function(value) {
  var node = new Node(value, null, this.tail);
  if (this.tail) this.tail.next = node;
  else this.head = node;
  this.tail = node;
}



var ll = new LinkedList();
ll.addToHead(2);
ll.addToHead(4);
ll.addToHead(3);
console.log('ll is: ', ll);

Related Tutorials