Assignment 8
by Nickolas Smiley
HTML
<label>Type in something: </label><br>
<input type="text" id="nodeinput">
<button onclick="insertSort();">
Insert String
</button><br><br>
<button onclick="createRandomList();">
Put random strings into list
</button><br>
<button onclick="sortedList();">
Sort with algorithm 1: MergeSort
</button><br>
<button onclick="QuickSort();">
Sort with algorithm 2: QuickSort
</button><br>
<p id='output1'></p>
<p id='output2'></p>
<p id='output'></p>
CSS
body {
background-color: grey;
}
p {
color: white;
}
label {
color: white;
}
button:focus {
border: 1px solid black;
padding: 4px 4px;
}
button {
background-color: grey;
border: 1px solid black;
color: white;
padding: 4px 8px;
text-decoration: none;
margin: 4px 2px;
}
button:hover {
background-color: black;
}
#print {
background-color: grey;
color: white;
}
#sort {
background-color: #656565;
color: white;
}
input[type=text] {
background-color: grey;
color: white;
padding: 4px 10px;
margin: 1px 1;
box-sizing: border-box;
border: 1px solid #303030;
}
input[type=text]:focus {
background-color: #303030;
}
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;
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;
if (this.back == this.front) {
...