Assignment 4 Updated
by Juan Alban Franco
HTML
<html>
<head>
<script>
var DoublyLinked = new Dll();
///////////////////// Doubly linked list "class" ///////////////////////////
function Dll () {
this.head = null;
this.tail = null;
this.length = 0;
}
function Node () {
this.id = null;
this.content = null;
this.next = null;
this.prev = null;
}
Dll.prototype.addNodes = function (data){
New_Node = new Node(); New_Node.id = GenerateID(); New_Node.content = data;
if (this.length > 0){
this.tail.next = New_Node;
New_Node.previous = this.tail;
this.tail = New_Node;
return New_Node;
} else {
console.log("created a new node") //this works
this.head = New_Node;
this.tail = New_Node;
console.log(New_Node);//node created with ID, content, next -> null, prev -> null
}
this.length ++;
return New_Node;
}
Dll.prototype.print = function(){
var s = " ";
var current_node = this.head;
if (this.tail == null){
s = "Empty String";
return s;
}
while(current_node){
s+="ID: " + current_node.id + " Value: " + current_node.content + "<br>";
current_node = current_node.next;
}
return s;
}
function GenerateID(){
var ID = " ";
var i = 0;
while( i < 4)
{
ID += String.fromCharCode(Math.floor( (Math.random() * 100) + 200));
i++
}
return ID;
}
///////////////////// Doubly linked list "class" end///////////////////////////
////////////////////////////main//////////////////////////////////////////////
function addNode(){
var input = document.getElementById("add_txt").value;
DoublyLinked.addNodes(input);
document.getElementById("out").innerHTML = DoublyLinked.print();
}
function createlist(){
console.log("created list")
var a = ["A","B","C","D","E"];
var s = a.length;
var i = 0;
for (i; i < s; i++){
console.log(a[i])
DoublyLinked.addNodes(a[i]);
}
...