<input type="button" id="CreateList" value="Create List" onClick="createList();" />
<br /><br /> Name of Node being added:
<input id="LinkName" placeholder="Enter Node letter"/>
<input type="button" id="AddLink" value="Add Node to List" onClick="addNode();" />
<p id="demo"></p>
JavaScript
var list = null;
function createList() {
var value = document.getElementById("LinkName").value;
list = new List(value);
document.getElementById("demo").innerHTML = list.print();
}
function addNode() {
var value = document.getElementById("LinkName").value;
list.addNode(value,list.last);
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 A Node B Node C Node D Node E " + this.value + "<br/>";
}
// Define the List object
function List(_value) { // We will define the list with the first link defined
this.length = 1;
this.head = new Node(_value, null); // Pointer TO the head is null
this.last = this.head; // When created - head and last are the same.
}
//Function to add node to list (Changed this code)
List.prototype.addNode = function (_value,_last) {
var node = new Node(_value, _last); // create a new node to attach it to the list
if (this.length) { //If list has some node already
this.last.next = node; //Setting next node of current last node of the list
node.last = this.last; //setting last of new node
this.last = node; /// Setting node as a last node od the list
} else {
this.head = node;
this.last = node;
}
this.length++;
}
// This is a very simple print routine - it starts at the head
// and...
Please Whitelist JSFiddle in your content blocker.
Help keep JSFiddle free for always by one of two ways:
Whitelist JSFiddle in your content blocker (two clicks)
Go PRO and get access to additional PRO features →
Join the 4+ million users, and keep the JSFiddle dream alive.
Ad-free
All ads in the editor and listing pages are turned completely off.
Use pre-released features
You get to try and use features (like the Palette Color Generator) months before everyone else.
Fiddle collections
Sort and categorize your Fiddles into multiple collections.
Private collections and fiddles
You can make as many Private Fiddles, and Private Collections as you wish!
Console
Debug your Fiddle with a minimal built-in JavaScript console.