Lists__
Linked Lists
by dhizzybusy
HTML
<h1>
<Center>Lists</Center>
</h1>
<br><br/>
<input type="button" id="CreateList" value="Create List" onClick="createList();" />
<br/><br/> Name of Node being added:
<input type="textbox" id="LinkName" Value="New Node" />
<input type="button" id="AddLink" value="Add Node to List" onClick="addNode();" />
<p id="demo"></p>
<div id="output">
</div>
JavaScript
var DoubleLinkedList = function() {
this.head = 0;
this.tail = 0;
this.length = 0;
var LinkedListNode = function(content) {
this.next = 0;
this.last = 0;
this.content = content;
};
this.add = function(content) {
if (this.head == 0) {
this.head = new LinkedListNode(content);
return this.head;
}
if (this.tail == 0) {
this.tail = new LinkedListNode(content);
this.head.next = this.tail;
this.tail.last = this.head;
return this.tail;
};
this.tail.next = new LinkedListNode(content);
this.tail.next.last = this.tail;
this.tail = this.tail.next;
this.tail.next = 0;
return this.tail;
};
}
DoubleLinkedList.prototype.length = function() {
var i = 0;
var node = this.head;
while (node != 0) {
i++;
node = node.next;
}
return i;
};
DoubleLinkedList.prototype.toString = function() {
var i = 1;
var str="";
//var str = "Linked List with " + this.length + " nodes <br/>";
var node = this.head;
while (node != 0) {
str += i + ": Node Value: " + node.content;
str += "/ Next Node Value:" + node.next.content;
str += " / Last Node Value: ";
if (node.last == 0) str += "null";
else str += node.last.content;
i++;
str += "<br>";
node = node.next;
this.length=i;
}
this.length--;
str +="<br>"+"Linked List with " + this.length + " nodes <br/>";
return str;
};
var d = " "; //Global string for output
var aNode= new DoubleLinkedList(); //Global doublelinklist
function clearDisplay()
{
//Global string d is used to hold display
d = " ";
// The div element named output is used to display output
document.getElementById("output").innerHTML = "";
}
function createList(){
clearDisplay();
var results=[];
var listofalphabet=["B","C","D","F","G","H","J","K","L","M","N","P","Q","R","S","T","V","X","Z","A","E","I","O","U","W","Y"]
console.log(listofalphabet);
var i =0;
while (i < 20){
results.push(Math.floor(Math.random() *...