Assignment 8
by leiperte
HTML
<input type="button" id="create" value="Create Random List" onClick="create()"> <br/><br/><input type="button" id="sort1" value="Quick Sort" onClick="quick_Sort()"> <br/><br/>
<input type="button" id="sort2" value="Merge Sort" onClick="mergeSort()"> <br/><br/>
<input type="textbox" id="v" value="aaa" /> <input type="button" id="AddLink" value="Insert to List" onClick="addNode();" /> <br/><br/>
<div id="output">
</div>
JavaScript
var aList;
var length;
function LinkedList() {
this.head = null;
this.tail = null;
this.length = 0;
}
function Node() {
this.content = null;
this.prev = null;
this.next = null;
return this;
}
//Create Random List
function create() {
var length = 0;
var aList = new LinkedList();
for (var j = 0; j < 20; j++) {
var chars = "abcdefghijklmnopqrstuvwxyz0123456789";
var createstring = '';
for (var i = 0; i < 3; i++) {
var randomNum = Math.floor(Math.random() * chars.length);
createstring += chars.substring(randomNum, randomNum + 1);
}
aList.add(createstring);
length++;
document.getElementById("output").innerHTML = aList.print();
}
return length;
}
LinkedList.prototype.add = function(_content) {
var node = new Node();
node.content = _content;
if (this.head == null) {
this.head = node;
this.length = 1;
return node;
}
if (this.tail == null) {
this.tail = node;
this.tail.prev = this.head;
this.head.next = this.tail;
this.length = 2;
return node;
}
this.tail.next = node;
node.prev = this.tail;
this.tail = node;
this.length++;
return node;
}
LinkedList.prototype.addSorted = function(_content) {
if (this.head == null) {
return this.add(_content);
}
var node = new Node();
node.content = _content;
if (_content < this.head.content) {
this.head.prev = node;
node.next = this.head;
this.head = node;
return node;
}
var cnode = this.head;
while (cnode.next != null) {
if ((_content > cnode.content) && (_content < cnode.next.content)) {
node.prev = cnode;
node.next = cnode.next;
cnode.next.prev = node;
cnode.next = node;
}
cnode = cnode.next;
return node;
}
return this.add(_content);
}
LinkedList.prototype.print = function() {
if (this.head == null) {
return "Empty List";
}
var s = "";
var node = this.head;
while (node != null) {
s += node.content + " <br/> ";
...