LinkedList add/remove node - Node.js Data Structure

Node.js examples for Data Structure:List

Description

LinkedList add/remove node

Demo Code


function LinkedList() {
  this.head = null;//from   ww w  .ja v  a2  s  .  c  o m
  this.tail = null;
}

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

// Add to head
LinkedList.prototype.addToHead = function(value) {
  var newNode = new Node(value, this.head, null);
  if (this.head) this.head.prev = newNode;
  else this.tail = newNode;
  this.head = newNode;
};
// Add to tail
LinkedList.prototype.addToTail = function(value) {
  var newNode = new Node(value, null, this.tail);
  if (this.tail) this.tail.next = newNode;
  else this.head = newNode;
  this.tail = newNode;
};
// Remove Head
LinkedList.prototype.removeHead = function() {
  if (!this.head) return null;
  var val = this.head.value;
  this.head = this.head.next;
  if(this.head) this.head.prev = null;
  else this.tail = null;
  return val;
};
// Remove Tail
LinkedList.prototype.removeTail = function() {
  if(!this.tail) return null;
  var val = this.tail.value;
  this.tail = this.tail.prev;
  if (this.tail) this.tail.next = null;
  else this.head = null;
  return val;
};
// Search
LinkedList.prototype.search = function(searchValue) {
  var currentNode = this.head;
  while(currentNode) {
    if(currentNode.value === searchValue) return currentNode.value;
    currentNode = currentNode.next;
  }
  return null;
};
// Indexing => array
LinkedList.prototype.indexOf = function(value) {
  var indexes = [];
  var currentIndex = 0;
  var currentNode = this.head;
  while(currentNode) {
    if(currentNode.value === value) indexes.push(currentIndex);
    currentNode = currentNode.next;
    currentIndex++;
  }
  return indexes;
};

var LL = new LinkedList();

LL.addToHead(5);
LL.addToHead(4);
LL.addToHead(30);
LL.addToTail(300);
LL.addToTail(33);
LL.addToTail(30);
LL.addToTail(30);
LL.addToTail(30);
LL.addToTail(30);

console.log(LL.indexOf(30));

Related Tutorials