0

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");
Shulhi Sapli
  • 2,286
  • 3
  • 21
  • 31

1 Answers1

2

The this in this.count refers to to the instance of the LinkedList object. The part:

var head = null,
    tail = null,
    count = 0;

These are private variables and not considered a property of the LinkedList object.

What you want to do instead is:

this.head = null;
this.tail = null;
this.count = 0;

And that will make head, tail, count a property of the LinkedList object so that you can do this.count++.

Edit: To keep head, tail, count as private to the LinkedList object, your other code would be something like this:

// Check if list is empty
    if(head == null)
    {
        head = node;
        tail = node;
        node.next = null;
    }
    // If node is not empty
    else
    {
        var current = tail;
        current.next = node;
        tail = node;
        node.next = null;
    }

    count++;

Also keep in mind that objects are pass-by-reference. So that applies to:

var current = tail;
current.next = node;
tail = node;
node.next = null;

More: If you want count to be a public property, then instead of returning:

 return {
        Add: insert,
    };

You need to do this:

this.Add = insert;
return this;

So that the current object context gets returned upon object creation.

Amy
  • 7,388
  • 2
  • 20
  • 31
  • If the variables are only accessed internally then it would be better to keep them private and not as an object property. – Seain Malkin Mar 07 '13 at 05:33
  • No assumptions made if he wants to let them be access publicly or not. Up to the him. But you are right. – Amy Mar 07 '13 at 05:35
  • I prefer the access to be internal (private). I tried changing my code to use your method but the problem remains. I still get NaN. Why can I access head tail just fine but count no? – Shulhi Sapli Mar 07 '13 at 05:39
  • Your edit works, but this will make the var all private, right? But it's not going to make it as property of the class? Like in C#, I want to make head, tail, count as a private property. Now, it is more like private variable if I'm getting the concept right, isn't it? – Shulhi Sapli Mar 07 '13 at 05:46
  • You probably want it private so that nothing outside of the object can modify it. So to summarize, declare using `var` would make it private property to the object. And using `this._____` would make it a public property that can be changed/accessed by outside the object. And if you include a `return` statement, then whatever is returned is also a public property. – Amy Mar 07 '13 at 05:49
  • Ok thanks. I think I got it now. Since I'm using a revealing module pattern, I could just use the var method, and expose it with the return statement if I need it to be public. Thanks for your help! – Shulhi Sapli Mar 07 '13 at 05:58
  • 1
    @ShulhiSapli Highly recommend you read some of these books:[Javascript Patterns by Stoyan Stefanov](http://shop.oreilly.com/product/9780596806767.do) and [JavaScript: The Good Parts by Douglas Crockford](http://shop.oreilly.com/product/9780596517748.do). Since you have background in programming, you'll breeze through them. But they do tell you all about the idiosyncrasies of JavaScript and how it's different from other languages and other gotchas – Amy Mar 07 '13 at 05:59