Create Doubly Linked List
This assignment is to create a Doubly Linked List utilizing Node Properties and List Properties. Then populate with 5 nodes having contents 'A,B,C,D,E'. A print function will be used to order the nodes. An interface will be created to allow user to add nodes to end of list, after adding new node a new list will be create to reflect addition.
by Neil Daley
HTML
<input type="button" id="ShowList" value="Show List" onClick="showList();" style="color:white; background-color:blue" />
<br/>
<p id="demo"></p>
<!--
<input type="button" id="CreateList" value="Create List" onClick="createList();" />
-->
<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();" style="color:white; background-color:blue" />
JavaScript
var list = null;
/* This function displays the original list as requestedper assignment detail. User clicks 'Show List' button to display the original contents.*/
function showList() {
list = new originaList("A");
//list.addNode("A");
list.addNode("B");
list.addNode("C");
list.addNode("D");
list.addNode("E");
document.getElementById("demo").innerHTML = list.print();
}
// This function adds a new node to the list.
function addNode() {
var value = document.getElementById("LinkName").value;
list.addNode(value);
document.getElementById("demo").innerHTML = list.print();
}
// Define the link object
function Node(_value, _last) {
// This is a possible implementation of a Doubly Linked List
this.value = _value; // The value stored
this.last = _last; // A pointer to the previous link
this.next = null; // a pointer to the next link
return this; // returns the created node
}
Node.prototype.asString = function() {
return "Node Value:" + this.value + "<br/>";
}
// Define the List object
function originaList(_value) { // We will define the list with the first link defined
this.length = 0;
this.head = new Node(_value, null); // Pointer TO the head is null
this.last = this.head; // When created - head and last are the same.
}
/* A function to add a link to the list - you will have to write this
alert("This function must be written");
*/
originaList.prototype.addNode = function(_value) {
/* With the new value to add, the list is checked begining at the top (head). If pointer to the Head is empty then Head assumes new value */
if (this.head == null) {
this.head = new Node(_value);
return this.head;
}
/* If pointer to Head is not empty, then Last node is checked. If Last node is empty, then Last node assumes new value */
if (this.last == null) {
this.last = new Node(_value);
this.head.next = this.last; /* Head and Next pointer holds same value as Last node */
return this.last;
}
/* The Last...