Doubly Linked List Merge Sort

A simple demonstration of a Doubly Linked List with an interface to add nodes to the list. This can be used to Fork to applications that require a Linked List. Includes push and pop functions. Also has Merge Sort

by austinmillett

HTML

<input type="button" value="Create Random List" onclick="createList(5)" />
<input type="textbox" id="v" value="Node 1" />
<input type="button" value="Add Node" onclick="addNode()" /><input type="button" value="Pop Node" onclick="popNode()" /><input type="button" value="Dequeue Node" onclick="dequeueNode()" />
<br/><br/>
<div id="output">

</div>
<br/>

<br/><br/>
<div id="output2">

</div>
<br/>
<input type="button" value="Do Merge Sort" onclick="doMergeSort()" />
<br/>
<div id="output3">

</div>

JavaScript

function LinkedList() {
  this.head = null;
  this.tail = null;
  this.length = 0;
}

// Nodes define the Doubly Linked
function Node() {
  this.next = null;  //next node in list
  this.prev = null;  // previous node in list
  this.content = null;
}

LinkedList.prototype.add = function(_content) {
  var node = new Node();
  node.content = _content;

  if (this.head == null) {
    this.head = node;
    this.tail = node;
    this.length = 1;
    return node;
  }

  if (this.tail == null) {
    this.tail = node;
    this.tail.prev = this.head;
    this.head.next = this.tail;
    this.length = 2;
    return node;
  }

  this.tail.next = node;
  node.prev = this.tail;
  this.tail = node;
  this.length++;
  return node;
}

LinkedList.prototype.push = function(_content) {
  this.add(_content);
}

// Pop will pop of the entire node
LinkedList.prototype.pop = function() {

if (this.head == null) return null;

if (this.head == this.tail) {
     var temp = this.head;
     this.head = null;
     this.tail = null;
     this.length = 0;
     return temp;
  }
  
  var oldtail = this.tail;
  var newtail = this.tail.prev;

  newtail.next = null;
  this.tail = newtail;
  this.length--;
  return oldtail;
}

// dequeue will remove node from front (head) of list.
LinkedList.prototype.dequeue = function() {
if (this.head == null) return null;

if (this.head == this.tail) {
     var temp = this.head;
     this.head = null;
     this.tail = null;
     this.length = 0;
     return temp;
  }
  
  var oldhead = this.head;
  this.head = this.head.next;

  this.length--;
  return oldhead;
}

LinkedList.prototype.print = function() {
  if (this.length == 0) return "Empty List";
  var s = "Linked List (length " + this.length + ")   ";
  var node = this.head;
  while (node != null) {
    s += node.content + "   ";
    node = node.next;
  }
  return s;
}

// Functions to respond to mouse clicks
var i = 1;
var aList = new LinkedList();
function addNode() {
  var c =...