How do I define the “next” property in the Node class so as to point to another Node object ?
When creating an instance of my Node class with the new operator, how do I link it to the list ? I mean, can I move through the linked list and insert the new node at the end ? How do I move through the list ? Do I have pointers (or similar) ? Can you show me how to manage them in ActionScript?
I’m wondering if this can be done in Flash MX. If not, is there a way of “emulating” this structure with arrays, objects and/or whatever ?
Thank you for your explanation. In fact, what I need to achieve is a little bit more complicated than this.
I need to create a linked list of custom objects (Node objects) like those made in C, Pascal, etc. I’m just trying to find out if it’s possible not to use arrays but lists, where each node points to the next one.
Example code in Pascal:
type
[COLOR=blue]nodeptr = ^node;[/COLOR]
nodetype = record
myinfo: integer;
[COLOR=red]nextnode: nodeptr;[/COLOR]
end;
I should define something like that in Flash.
So, in the class definition I need to define a link to an object of the same cl**** it is recursive.
[COLOR=green]this.next[/COLOR] corresponds to the [COLOR=red]nextnode: nodeptr;[/COLOR] in the Pascal example. I should define a pointer to an object of its own class…, but I don’t know how to do it… And I also don’t know how to move through the linked list once it’s created.
Did I make myself understood now or is it still confusing ? Sorry about my english, I’m not used to writing or speaking english so perhaps you don’t understand me…
Hey, man forget it. This is not C :P. Unless you know how they are coded, I don’t think there’s a way to use pointers in Flash MX. It’s not very useful either, especially in your case, because you can declare an array, and you don’t have to declare its size, so you can just push in your stack, as Inigo said.
OR maybe you can create your class (#initclip and registerClass things), but I don’t know how you can access the memory from Flash.
you could hack a solution with an array that contains pointers to each node, and define “next” as a method to retrieve the following item of the array:
_root.nodeArray = []
function Node(myear, mvalue) {
this.year = myear;
this.value = mvalue;
_root.nodeArray.push(this);
}
Node.prototype.next = function(){
var p = _root.nodeArray.getPlace(this);
if(p>0){ return(_root.nodeArray[p+1]); }
else{ return(null); }
}
Array.prototype.getPlace = function(targ){
var l = this.length;
while(l--){ if(this[l] == targ) return(l); }
return(-1);
}
that’s untested but should do the trick. and it will still work if you decide to shuffle the array around later.