0

i am sitting on a problem. I am learning linked lists (beacuse i need this lateron for a tree solution) and try to create the links dynamically. Obviously i have the problem to generate the linkage between the objects.

Here is the Code

public function Main()
    {
        var node1:Object = {value: 1};
        var node2:Object = {value: "foo"};
        var node3:Object = {value: "bar"};
        var node4:Object = {value: "test"};


        for (var a:int = 1; a < 4 ; a++)
        {
            if (a < 3)
            {
            node[a].next = node[a + 1];
            }
            else
            {
                node[a].next = null;
            }

        }
        // ((node1.next = node2).next = node3).next = null; works, but 
        // not the code above


        var n:Object = node1;
        while (n)
        {
            trace(n.value);
            var jsonString:String = JSON.stringify(n);
            trace(jsonString);

            n = n.next;
        }
    }

Can i have a explanation please ?

i know object is not an array, but it should be possible to get the pointer on the right position. I am pretty sure, that could be a dynamic solution. I really tried different notation. Do you have a hint for me ?

2
  • You can access node1 as this['node' + a] if a is 1. However I don't understand what you are trying to achieve Commented Sep 28, 2016 at 15:03
  • its my first steps in doing linked lists. This is R & D for me Commented Sep 28, 2016 at 15:39

2 Answers 2

1

The actual problem is that you create variables for your list of nodes. You then try to use those names to access the objects. Instead, just create them in an Array like so (code not tested)

public function Main()
{
    var nodes:Array = [{value: 1}, {value: "foo"}, {value: "bar"}, {value: "test"}];

    for each(var a:int = 0; a < nodes.length - 1 ; a++)
    {
        nodes[a].next = nodes[a + 1];
    }

    var n:Object = nodes[0];
    while (n)
    {
        trace(n.value);
        var jsonString:String = JSON.stringify(n);
        trace(jsonString);
        n = n.next;
    }
}

Your current solution is bad for several reasons. It's not very flexible. If you want to add a node to the list, you have to create a new variable that has the appropriate name (otherwise it will not be picked up by the loop) You also have to modify the hard coded upper limit in the loop. Having the same information redundantly in your code is a maintenance nightmare. And here's another one: What if you want to remove node2? Yes, you have to rename all following variables.

Your idea of using a loop for a dynamic solution is good. The way you create the data however is a hindrance to this goal. Avoid using variable names as values in your program as much as possible.

Sign up to request clarification or add additional context in comments.

1 Comment

thank you very much for your comment. I would like to inform you, that this was only a test for me. The Project its self is to be in its infancy. I usually work with arrays and and try to get in touch with linked lists. Further i want to build a dynamically tree. But i have to understand this, not only copy and paste. This is learning the hard way :). P.S. i try also to get in touch with objects...
0
    for (var a:int = 1; a < 4 ; a++)
    {
        if (a < 3)
        {
        this["node"+ String(a)].next = this["node"+ String(a+1)];
        }
        else
        {
            this["node"+ String(a)].next = null;
        }

    }

I used bracket syntax to access properties dynamically (by String). you can learn more about it by searching on the Internet.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.