Beefy Boxes and Bandwidth Generously Provided by pair Networks
Your skill will accomplish
what the force of many cannot
 
PerlMonks  

Re: Efficient Linked Lists

by jdporter (Canon)
on Jan 31, 2003 at 20:13 UTC ( #231702=note: print w/ replies, xml ) Need Help??


in reply to Efficient Linked Lists

Why do you need a linked list? You already have an array; can't you just iterate over the array backwards and forwards?

What I sometimes do is create a reverse-lookup table:
my %index_of; @index_of{ @filenames } = (0..$#filenames); # given a filename, find its predecessor or successor: my $predecessor = $filenames[ $index_of{ $filename } - 1 ]; my $successor = $filenames[ $index_of{ $filename } + 1 ];

jdporter
The 6th Rule of Perl Club is -- There is no Rule #6.


Comment on Re: Efficient Linked Lists
Download Code
Re^2: Efficient Linked Lists
by Ionizor (Pilgrim) on Jan 31, 2003 at 20:49 UTC

    The goal is to create a set of static HTML pages with previous and next links to move from file to file, so it's not straight iteration (Note to self: Think while typing). I need to print out the filename, then a link to the previous filename, then a link to the next filename.

    I think a reverse lookup table will do the job nicely. Thanks.

    --
    Grant me the wisdom to shut my mouth when I don't know what I'm talking about.

      I think a reverse lookup table will do the job nicely. Thanks.
      A whuh?

      Sounds like just a simple list, again. As four or five answers in this thread have already said. What requirement prevents you from using the code that has been posted four or five times?

      Do not construct a doubly linked list. People build those things because they don't have perlfunc:splice, so in Perl, it's almost always the wrong way to go.

      -- Randal L. Schwartz, Perl hacker
      Be sure to read my standard disclaimer if this is a reply.

        What I currently have is a function using jdporter's code above (which he described as a lookup table) that will look up the previous or next filename. Is this the code you're talking about or am I missing something?

        --
        Grant me the wisdom to shut my mouth when I don't know what I'm talking about.

Log In?
Username:
Password:

What's my password?
Create A New User
Node Status?
node history
Node Type: note [id://231702]
help
Chatterbox?
and the web crawler heard nothing...

How do I use this? | Other CB clients
Other Users?
Others perusing the Monastery: (7)
As of 2014-12-26 05:14 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    Is guessing a good strategy for surviving in the IT business?





    Results (165 votes), past polls