Assignment 8 - Sorting Final
by Jenni Meiklejohn
April 29, 2017
HTML
<input type = "button" id = "button" value = "Create List" onClick = "createList();"/>
<input type = "button" id = "button" value = "Merge Sort" onClick = "CallMergeSort();"/>
<input type = "textbox" id = "input" value = "Insert Value"/>
<input type = "button" id = "button" value = "Insertion Sort" onClick = "CallInsertSort();"/>
<p id = "output"></p>
CSS
#button
{
background-color: grey;
color: white;
}
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 addedNode = new Node(_content);
addedNode.next = this.front;
this.front.previous = addedNode;
this.front = addedNode;
return this;
}
this.addToBack = function(_content)
{
if (this.front == null)
{
this.front = new Node(_content);
this.back = this.front;
return this;
}
var addedNode = new Node(_content);
addedNode.previous = this.back;
this.back.next = addedNode;
this.back = addedNode;
return this;
}
this.insertAfter = function(valueToAdd)
{
var listX = "";
var node = this.back;
if(valueToAdd < this.front.content)
{
return "goesInFront";
}
if(valueToAdd > this.back.content)
{
return "goesInBack";
}
while (node != null)
{
if(valueToAdd > node.content)
{
var newNode = new Node(valueToAdd);
node.next.previous = newNode;
newNode.previous = node;
newNode.next = node.next;
node.next = newNode;
return "inserted";
}
node = node.previous;
}
return;
}
this.removeFront = function()
{
if (this.front == null)
{
return null;
}
var contentRemoved = this.front.content;
if(this.back == this.front)
{
this.front = null;
this.back = null;
return contentRemoved;
}
else
{
this.front = this.front.next;
this.front.previous = null;
return contentRemoved;
}
}
this.removeBack = function()
{
if (this.front == null)
{
return null;
}
var contentRemoved = this.back.content;
...