|
|
I added a couple of lines:
function sum(list)
> var total = 0;
> var position = list.start();
> while (position != null)
> {
> total = total + position.get_value();
> position = position.get_next();
> }
return total
end
> Pretty much a verbatim match there.
OK, so for your "Other Method" we have:
function sum(list)
if( list.start() == NULL )
return 0
else
return list.get_value() + sum(list.get_next())
endif
end
> And of course, accessing parts of a list requires splitting them, and
> recusion isn't looping. ;-)
I suspect the recursion method is slower, because calling a function has
some overheads (storing and recalling registers and return address on stack
etc).
Post a reply to this message
|
|