Assignment 8
by Jeremy Boss
HTML
Create List Then Sort/Add String
<br/>
<br/>
<div class='a'>
<input type="textbox" id="value" value="3b2a1" />
<button id='stringInput' onclick='stringInsert()'>Insert String
<button id='createList' onclick='createList(20)'>Create Random List
<button id = 'bubble' onclick='bubble()'>Bubble Sort
<button id = 'Sort1' onclick='goSort1()' />Merge Sort </button>
<div id='mergeClock'></div>
<div id='bubbleClock'></div>
<div id='insertClock'></div>
<div class="row">
<div id='firstOut'><b>List</b>
</div>
<div id='mOut'><b>Merge</b>
</div>
<div id='bOut'><b>Bubble</b>
</div>
<div id='insertOut'><b>Inserted String</b>
</div>
</div>
<div id='merge'>
</div>
JavaScript
var newList = new LinkedList();
function Node() {
this.next = null;
this.prev = null;
this.content = null;
}
function LinkedList() {
this.head = null;
this.tail = null;
this.length = 0;
}
LinkedList.prototype.push = function(_content) {
this.sum(_content);
}
LinkedList.prototype.sum = 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++;
return node;
}
this.tail.next = node;
node.prev = this.tail;
this.tail = node;
this.length++;
return node;
}
LinkedList.prototype.burst = function(_content) {
if (this.head == null) {
alert('Empty List!');
document.getElementById('value').focus();
return null;
}
if (this.head == this.tail) {
var hold = this.head;
this.head = null;
this.tail = null;
this.length = 0;
return hold;
}
var last = this.tail;
var ntail = this.tail.prev;
ntail.next = null;
this.tail = ntail;
this.length--;
document.getElementById('output').innerHTML = 'burst ' + last.content + '<br>' + '<br>';
document.getElementById("output1").innerHTML = newList.print();
return last;
}
LinkedList.prototype.enqueue = function(_content) {
var node = new Node();
node.content = _content;
if (document.getElementById('value').value == '') {
alert('Input string ');
document.getElementById('value').focus();
} else if (this.head == null) {
this.head = node;
this.tail = this.head;
this.length++;
document.getElementById('value').value = '';
document.getElementById('value').focus();
return this;
} else if (this.head == this.tail) {
this.head = node;
this.head.next = this.tail;
this.tail.prev = node;
this.length++;
document.getElementById('value').value = '';
...