I'm learning my way into JS (but not new to programming). So, I'm trying to implement a LinkedList just to play around with JS.
It works okay except that count
always returning NaN
. I've googled, and thought that the reason was I wasn't initially setting the count
to a number, but I did.
Below is my code:
function LinkedList() {
var head = null,
tail = null,
count = 0;
var insert = function add(data)
{
// Create the new node
var node = {
data: data,
next: null
};
// Check if list is empty
if(this.head == null)
{
this.head = node;
this.tail = node;
node.next = null;
}
// If node is not empty
else
{
var current = this.tail;
current.next = node;
this.tail = node;
node.next = null;
}
this.count++;
};
return {
Add: insert,
};
}
var list = new LinkedList();
list.Add("A");
list.Add("B");