Assignment 8
by Nickolas Smiley
HTML
<input type="button" value="Create Random String List" onClick="createRandomList();">
<br/>
<input type="button" value="Merge Sort" onClick="sortedList();" />
<br/>
<input type="button" value="Code Insert Sort:" onClick="insertSort();">
<input type="textbox" id="insertValue" value="" size="3"/>
<p id='output1'></p>
<p id='output2'></p>
<p id='output'></p>
JavaScript
var Node = function(content) {
this.next = null;
this.previous = null;
this.content = content;
}
var Queue = function() {
this.front = null;
this.back = null;
//call variables
this.addToFront = function(content) {
if (this.front == null) {
this.front = new Node(content);
this.back = this.front;
return this;
}
//add variables to front
var addedNode = new Node(content);
addedNode.next = this.front;
this.front.previous = addedNode;
this.front = addedNode;
return this;
}
//adding nodes
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;
//insert function value to add to the list
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";
}
//insert the nodes when the "insert value" button is pressed.
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 is a back process
this.removeBack = function()...