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.

If you need more information than this, I can provide it...I think :)
Well a linked list is really a type of data structure, C uses structs because that is the easiest way to combine related pieces of data, but that isn't the only way to handle it in C, which also means there are many ways to handle it in Perl. My CS is a little rusty and I haven't implemented one in a while, but one way would be to use hashes. Depending on whether your list is bi-directional would determine how many keys you need, aka 2 or 3 depending on whether you needed to store the next element only, or both the next and previous. So essentially you end up with a bunch of hashes, with one key 'next' which stores a reference to the next hash, and a key 'prev' which stores a reference to the previous hash, and then a key 'data' which stores a scalar or a reference to whatever your data looks like. Another way would be to use an array, and then your methods for adding elements, shifting elements, and removing elements would need to do a bunch of splicing, which seems like it wouldn't be as clean as the hash method.

The key really is to have a way to store whatever data you need, along with a "link" to the next (and maybe previous) element. Then to write whatever methods you need, to work on the chain.

perldoc -q "linked"
perldoc perldsc
perldoc perlreftut
perldoc perlref

http://danconia.org


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

Reply via email to