Assignment 8-1
by Taylor Zimmerman
HTML
<html>
<Body>
<fieldset>
<legend>Create List</legend>
<input type="button" value="Create List" onclick="CreateList()">
</fieldset>
<fieldset>
<legend>Quick Sort</legend>
<input type="button" value="Quick Sort" onclick="QuickSort()">
</fieldset>
<fieldset>
<legend>Add to the list</legend>
<input type="textbox" id="tbInput"><br>
<input type="button" value="Add Node" onclick="AddToList()">
<input type="button" value="Merge Sort" onclick="MergeSort()">
</fieldset>
<fieldset>
<legend>List</legend>
<div id="output">
</div>
</fieldset>
</Body>
</html>
JavaScript
var List = new LinkedList();
var d = "";
function Node(){
this.content = null;
this.next = null;
this.last = null;
return this;
}
function LinkedList(){
this.head = null;
this.tail = null;
this.length = 0;
}
LinkedList.prototype.add = function(pass){
var node = new Node();
node.content = pass;
if (this.head === null) {
this.head = node;
this.length = 1;
return node;
}
if (this.tail == null) {
this.tail = node;
this.tail.last = this.head;
this.head.next = this.tail;
this.length = 2;
return node;
}
this.tail.next = node;
node.last = this.tail;
this.tail = node;
this.length++;
return node;
}
LinkedList.prototype.print = function(){
if (this.head == null) return "Empty List";
var node = this.head;
var d = "";
var count = 1;
while(node != null){
d += "#" + count + " - " + node.content + "</div><br> ";
count = count + 1;
node = node.next;
}
return d;
}
function CreateList(){
for (var i = 1; i <= 20; i++) {
var rnum = (Math.random().toString(20).substring(2, 15));
List.add(rnum);
document.getElementById('output').innerHTML = List.print();
}
}
function AddToList(){
var addNode = document.getElementById("tbInput").value;
List.add(addNode);
document.getElementById('output').innerHTML = List.print();
}