Beefy Boxes and Bandwidth Generously Provided by pair Networks
XP is just a number
 
PerlMonks  

Comment on

( #3333=superdoc: print w/ replies, xml ) Need Help??
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


In reply to Re: How to implement Linked List by Moron
in thread How to implement Linked List by msk_0984

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post; it's "PerlMonks-approved HTML":



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • Outside of code tags, you may need to use entities for some characters:
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.
  • Log In?
    Username:
    Password:

    What's my password?
    Create A New User
    Chatterbox?
    and the web crawler heard nothing...

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

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





      Results (176 votes), past polls