A8 Sort Function
Sort function
by scotp71
HTML
<h1>
Sort Function
</h1>
<br/>
<input type = "button" value="Create Randomized String List" onClick="createList(20)"/>
<br/>
<div id="output">
</div>
<input type = "button" value="Sort with Bubble Sort" onClick="doBubbleSort()"/>
<br/>
<div id="output2">
</div>
<input type = "button" value="Sort with Merge Sort" onClick="doMergeSort()"/>
<br/>
<div id="output3">
</div>
<input type="textbox" id="v" value="" />
<input type="button" value="Insert String" onclick="insertString()"/>
<div id="output4">
</div>
JavaScript
function LinkedList(){
this.head = null;
this.tail = null;
this.length = 0;
this.content = null;
}
function Node(){
this.next = null;
this.prev = null;
this.content = null;
}
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.push = function(_content){
this.add(_content);
}
LinkedList.prototype.dequeue = function(){
if(this.head == null) return null;
if(this.head == this.tail){
var temp = this.head;
this.head = null;
this.tail = null;
this.length = 0;
return temp;
}
var oldhead = this.head;
this.head = this.head.next;
this.length--;
return oldhead;
}
function popNode(){
var oldTail = aList.pop();
if(oldTail != null){
document.getElementById("output2").innerHTML = "Popped node: " + oldTail.content + "<br/>";
}
else{
document.getElementById("output2").innerHTML = "";
}
document.getElementById("output2").innerHTML += aList.print();
}
function dequeueuNode(){
var oldHead = aList.dequeue();
if(oldHead != null) document.getElementById("output").innerHTML = "Dequeued Node: " + oldHead.content + "<br/>";
else document.getElementById("output2").innerHTML = "";
}
LinkedList.prototype.print = function() {
if (this.head == null) return "Empty List"
var s = "";
var node = this.head;
while (node != null) {
s += node.content + ", ";
node = node.next;
}
return s;
}
function createList(n){
for (var i = 1; i <= n; i++){
aList.push(createRandomString());
}
document.getElementById("output").innerHTML = "Random List:<br/> " +aList.print();
}
var aList = new...