Assignment 4 - Lists
by Mike Kerney
HTML
Mike Kerney<br/>
Assignment 4 - Lists<br/>
<br/>
<input type="button" value="Populate List" onclick="populateList()" />
<br/>
<br/>
<input type="textbox" id="nodeValue" value="Node 1" />
<input type="button" value="Add Node" onclick="addNode()" />
<br/>
<br/>
<div id="output">
</div>
<br/>
<div id="output2">
</div>
JavaScript
function LinkedList() {
this.head = null;
this.tail = null;
this.length = 0;
}
function Node() {
this.ID = null;
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++;
return node;
}
this.tail.next = node;
node.prev = this.tail;
this.tail = node;
return node
}
LinkedList.prototype.print = function() {
if (this.head == null) return "Empty List";
var string = "";
var node = this.head;
while (node != null) {
string += node.content + " ";
node = node.next;
}
return string;
}
var aList = new LinkedList();
function populateList() {
for (i = 65; i < 70; i++) {
var g = String.fromCharCode(i);
aList.add(g);
document.getElementById("output").innerHTML = aList.print();
}
}
function addNode() {
var c = document.getElementById("nodeValue").value;
aList.add(c);
document.getElementById('output').innerHTML = " "
document.getElementById('output').innerHTML = aList.print();
}