Assignment 4
by Ebony McCoy
HTML
<input type="button" value="Creat List" onclick="createList()" />
<br>
<br> Enter Content Here:
<input type="textbox" id="content" />
<input type="button" value="Add Node to List" onclick="addNode()" />
<br>
<br>
<p id="output1"></p>
JavaScript
var dList = new LinkedList();
function LinkedList() {
this.head = null;
this.tail = null; //should it be this.tail??
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;
dList.add(content);
dList.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("output1").innerHTML = display;
};
function createList(content) {
for (var i = 0; i < 5; i++) {
content = String.fromCharCode(65 + i);
dList.add(content);
dList.print();
}
}