RB_JSFIDDLE_A8
Sorting
by Ryan Brown
HTML
<input type="button" value="Create Radonomized List" onClick="createRandom();" />
<br/>
<input type="button" value="Merge Sort" onClick="numericalSort();" />
<br/> Enter a value to insert below:
<br>
<input type="textbox" id="userInsert" value="20" />
<input type="button" value="Insert & Sort" onClick="sortInsert();" />
<p id='inSortOutput'></p>
JavaScript
var Node = function(_content)
{
this.next = null;
this.previous = null;
this.content = _content;
}
var Queue = function()
{
this.front = null;
this.back = null;
this.addToFront = function(_content)
{
if (this.front == null)
{
this.front = new Node(_content);
this.back = this.front;
return this;
}
var createdNode = new Node(_content);
createdNode.next = this.front;
this.front.previous = createdNode;
this.front = createdNode;
return this;
}
this.addToBack = function(_content)
{
if (this.front == null)
{
this.front = new Node(_content);
this.back = this.front;
return this;
}
var createdNode = new Node(_content);
createdNode.previous = this.back;
this.back.next = createdNode;
this.back = createdNode;
return this;
}
this.insertAfter = function(valueToAdd)
{
var listX = "";
var node = this.back;
if (valueToAdd < this.front.content)
{
return "gFront";
}
if (valueToAdd > this.back.content)
{
return "gBack";
}
while (node != null)
{
if (valueToAdd > node.content)
{
var nodeToInsert = new Node(valueToAdd);
node.next.previous = nodeToInsert;
nodeToInsert.previous = node;
nodeToInsert.next = node.next;
node.next = nodeToInsert;
return "inserted";
}
node = node.previous;
}
return;
}
this.removeFront = function()
{
if (this.front == null) {
return null;
}
var popContent = this.front.content;
if (this.back == this.front)
{
this.front = null;
this.back = null;
return popContent;
}
else
{
this.front = this.front.next;
this.front.previous = null;
return popContent;
}
}
this.removeBack = function()
{
if...