Reply to
You can try using hash tables. First, for every page hash all it's links to the table and then when generating a page you look at the table for the pages that link to this page. This way you process every page exactly twice.
(In other words, your algorithm is O(n^2), what I'm proposing here is linear, that is, O(n))
Responses (?)
JavaScript needs to be enabled to show responses. (Although, it is not necessary for posting them.)