Double Linked List
by arosado417
HTML
<input type="button" value="Populate" onclick="populateLink()"/>
<input type="textbox" id="v" />
<input type="button" value="Add Node" onclick="addNode()"/>
<div id="output">
</div>
JavaScript
function LinkedList(){
this.head = null;
this.tail = null;
this.length = 0;
}
function Node(){
this.next = null;
this.prev = null;
this.value = _value;
}
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;
this.length++;
return node;
}
LinkedList.prototype.print = function(){
if (this.head == null) return "Empty List";
var s = "";
var node = this.head;
while (node != null){
s += node.content + " " +"<br/> ";
node = node.next;
}
return s;
}
var aList = new LinkedList();
function populateLink(){
aList.add('A');
aList.add( 'B');
aList.add('C');
aList.add( 'D');
aList.add('E');
document.getElementById('output').innerHTML = aList.print();
}
function addNode(){
var c = document.getElementById("v").value;
aList.add(c);
document.getElementById("output").innerHTML = aList.print();
}