开发者

Static variables in PHP - something other than literals and constants!

I want to create a function that appends a LinkNode to a given LinkList (created my own Node class), but I want to add the optimization that the function holds in a variable a pointer to the last appended node so all I have to do is add the new Node to the variable's next link. I thought the best w开发者_运维问答ay to do this would be to create a static variable

$i = $overallRoot; 

that is constantly updated as the function append($node) is called. (updated to point to $node), but apparently you can only make static variables in functions equal to ints and such.

What would be the best way to implement this optimization? Thanks for the help; just started learning PHP.


It seems like a much more robust optimization would be to have each linked list keep track of a pointer to the first and last node it contains. That way, the cost of appending to a linked list is O(1) regardless of when you last tried appending to it. Many linked lists use this approach, since it can dramatically speed up insertions.

Sorry if this really isn't an answer to your original question, but it seems like a much better way of getting the result you're looking for.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜