A4
by Ebony McCoy
HTML
<input type="button" value="Create Node List" onclick= "createList()"/>
<br>
<br/>Enter Content Here:
<input type="textbox" id="content"/>
<input type="button" value="Add Node to List" onclick="addNode()"/>
<br/>
<p id="output"></p>
JavaScript
var DoublyList = new LinkedList();
function LinkedList() {
this.head = null;
this.tail = null;
this.length = 0;
}
function Node(){
this.content = null;
this.last = null;
this.next = null;
return this;
}
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.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;
};
function addNode() {
var content = document.getElementById("content").value;
DoublyList.add(content);
DoublyList.print();
}
LinkedList.prototype.print = function() {
if (this.head === null) return "Empty List";
var display = " ";
var counter = 0;
var node = this.head;
while (node !== null) {
counter = counter + 1;
display += "Node # " + counter + " Content: " + node.content + "</br>";
node = node.next;
}
document.getElementById("output").innerHTML = display;
};
function createList(content) {
for (var i = 0; i < 5; i++) {
content = String.fromCharCode(65 + i);
DoublyList.add(content);
DoublyList.print();
}
}