<H4>Type the node value you wish to add</H4>
<input type="textbox" id="value" value="" />
<br/>
<input type="button" value="Add Node" onClick="NewList();" />
<p id="demo"></p>
<br/>
JavaScript
// For this program I am trying to make a double linked list. Within the code I have created a function to add a node into the list. I created a statement that prints the results of the list that states the node id, node value and where that node points to.
var DoubleLinkedList = function() { //Here I have created the list function where I define the head, tail and length of the list.
this.head = 0;
this.tail = 0;
this.length = 0;
var LinkedListNode = function(id, content) { // Here I have defined the nodes that will be placed within the list.
this.next = 0;
this.last = 0;
this.id = id;
this.content = content;
};
this.add = function(id, content) { // This is the function that adds a new node value into the list.
if (this.head == 0) {
this.head = new LinkedListNode(id, content);
this.length++;
return this.head;
}
if (this.tail == 0) {
this.tail = new LinkedListNode(id, content);
this.head.next = this.tail;
this.tail.last = this.head;
this.length++;
return this.tail;
};
this.tail.next = new LinkedListNode(id, content);
this.tail.next.last = this.tail;
this.tail = this.tail.next;
this.tail.next = 0;
this.length++;
return this.tail;
};
}
DoubleLinkedList.prototype.length = function() { // Here is where I count the list and use this to point to what is next.
var i = 0;
var node = this.head;
while (node != 0) {
i++;
node = node.next;
}
return i;
};
DoubleLinkedList.prototype.toString = function() { // This is the function that prints the list information.
var str = 'Linked List with ' + this.length + ' nodes <br/>';
var node = this.head;
while (node != 0) {
str +="Node ID:" + node.id + ' | Node Value: ' + node.content + ' | Points to: ' + node.next.content;
str += "<br>";
node = node.next;
}
return str;
};
var MyLinkedList = new DoubleLinkedList(); // Here I have...
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.