> "JC(oesd)" wrote:
> I just had a friend explain in some little detail about linked
> lists in C using structures.
> How would such a thing be done with Perl?
> This may help me understand C's version a little better.

The idea is simply to have one thing tell you where the next thing is
in a preferably editably way. Perl has more and *potentially*
simplerways to do this, but it's flexibility also makes it potentially
more confusing.

Here's one example: a list of hashes, similar to a list of structs in
C:

  my %list = ( a => { value => 'first',
                      next  => 'b',      },
               b => { value => 'second',
                      next  => 'c',      },
               c => { value => 'other',
                      next  => 'a',      },
  );

In this SIMPLISTIC case, is't a circular list; each item's "next"
points to the next item in the list, but the last one happens to point
to the first one. It could be MUCH more complex, with genuine objects
pointing to each other and back to their previous and just about
anything else you want to hang on them -- hashes are really handy that
way, lol -- but unless you explicitly break a link somewhere, be aware
that this causes a loop which prevents Perl's reference-counting
garbage-collection system from ever freeing up that space.

You could do the same thing with arrays, using cell [0] and [1] or
whever you chose as standards instead of the hash keys, and nothing
else would really change much. You could do a similar thing in C, with
an array of pointers to arrays instead of structs. Don't get hung up on
the underlying pieces -- it's the logic you need to grasp, so that you
can reimplement it efficiently for any given task in any given
language.

Of course, you'd also probably want to generate the contents
dynamically -- can I leave that as the infamous "exercise for the
reader"? :)

Paul

__________________________________________________
Do you Yahoo!?
Yahoo! Shopping - Send Flowers for Valentine's Day
http://shopping.yahoo.com

-- 
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]

Reply via email to