Beefy Boxes and Bandwidth Generously Provided by pair Networks
The stupid question is the question not asked
 
PerlMonks  

Re: How to implement Linked List

by Moron (Curate)
on Dec 19, 2006 at 16:21 UTC ( #590694=note: print w/ replies, xml ) Need Help??


in reply to How to implement Linked List

The main raison d'etre of a linked list is that in compiled languages (but not in Perl) arrays are not supported by element deletion or insertion. But I said 'main' rather than 'only'. It is perfectly reasonable that a class might call for a peer to peer relationship and that just happens to be identical in implementation to a linked list. So without further ado, and covering my eyes to anything too complex that has been suggested (sorry no time before Christmas!) that becomes my recommendation, to whit, create a SIMPLE object solution something like:

package P2Pr; sub new { shift(); my $ref = bless {}; $ref -> link( @_ ) if ( @_ ); # support one-stop calls return $ref; } sub link { my ( $obj, $relation, $other ) = @_; $obj -> { $relation } = $other; } # ... 1; # common or garden example ... use P2Pr; my $carriage1 = P2Pr -> new(); my $train = P2Pr -> new( 'afore', $carriage1 ); $carriage1 -> link ( 'abaft', $train ); my $carriage2 = new( 'abaft', $carriage1 ); $carriage1 -> link( 'afore', $carriage2 );
Perl should automatically delete objects that have been unlinked from the list, although I am in the habit of making sure by applying a "delete" + object reference before bypassing a node into oblivion (in the example that could be done just by linking both neighbours of the victim node to each other).

-M

Free your mind


Comment on Re: How to implement Linked List
Download Code

Log In?
Username:
Password:

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

How do I use this? | Other CB clients
Other Users?
Others pondering the Monastery: (3)
As of 2014-10-25 10:25 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    For retirement, I am banking on:










    Results (142 votes), past polls