Beefy Boxes and Bandwidth Generously Provided by pair Networks
Perl Monk, Perl Meditation
 
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 avoiding work at the Monastery: (8)
As of 2014-08-20 08:59 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    The best computer themed movie is:











    Results (108 votes), past polls