Beefy Boxes and Bandwidth Generously Provided by pair Networks
Your skill will accomplish
what the force of many cannot
 
PerlMonks  

Comment on

( #3333=superdoc: print w/ replies, xml ) Need Help??
And making it a doubly linked list is only slightly harder. However making a doubly linked list which does not leak memory is far, far harder.

Here is an example off of my scratchpad that does this. After examining it (and trying it if you want to see abysmal performance) I think that the point will be adequately made that linked lists are possible in Perl, but you really want to use arrays instead.

#! /usr/bin/perl use strict; my $list = new LinkedList(qw(software cheese fruit wine pasta)); print "Listing list forward:\n"; do { print " Node: ", $list->data, "\n"; } while $list->move_next; print "\nListing list backward:\n"; do { print " Node: ", $list->data, "\n"; } while $list->move_prev; package LinkedList; use WeakRef; sub add_next { my $self = shift; $self->{current}->add_next(LinkedList::Node->new(shift)); return 1; } sub add_prev { my $self = shift; $self->{current}->add_prev(LinkedList::Node->new(shift)); return 1; } sub clone { my $self = shift; my $clone = bless { %$self }, ref($self); my $is_clone = $self->{is_clone}; $is_clone->{$clone} = $clone; weaken($is_clone->{$clone}); return $clone; } sub data { (shift)->{current}->data; } sub move_next { my $self = shift; my $next = $self->{current}->next(); $self->{current} = $next if defined($next); return defined($next); } sub move_prev { my $self = shift; my $prev = $self->{current}->prev(); $self->{current} = $prev if defined($prev); return defined($prev); } sub new { my $class = shift; my $self = bless {}, $class; my $data = shift; $self->{current} = LinkedList::Node->new($data); $self->{is_clone} = {$self=>{$self}}; weaken($self->{is_clone}->{$self}); my $clone = $self->clone; while (@_) { $clone->add_next(shift); $clone->move_next; } return $self; } sub next { my $clone = (shift)->clone; $clone->move_next ? $clone : undef; } sub prev { my $clone = (shift)->clone; $clone->move_prev ? $clone : undef; } sub DESTROY { my $self = shift; #warn("Destroying linked list $self\n"); delete $self->{is_clone}->{$self}; if (not keys %{$self->{is_clone}}) { # Clean up circular stuff my $node = $self->{current}; while (defined($node)) { $node = delete $node->{next}; } $node = $self->{current}; while (defined($node)) { $node = delete $node->{prev}; } } #warn("Linked list $self destroyed\n"); } package LinkedList::Node; sub add_next { my ($self, $next) = @_; my $old_next = $self->{next}; $self->{next} = $next; $next->{prev} = $self; $next->{next} = $old_next; $old_next->{prev} = $next if defined($old_next); } sub add_prev { my ($self, $prev) = @_; my $old_prev = $self->{prev}; $self->{prev} = $prev; $prev->{next} = $self; $prev->{prev} = $old_prev; $old_prev->{next} = $prev if defined($old_prev); } sub data { (shift)->{data}; } sub new { my ($class, $data) = @_; return bless { data => $data}, $class; } sub next { (shift)->{next}; } sub prev { (shift)->{prev}; } sub DESTROY { my $self = shift; #warn("Node: $self->{data} destroyed"); }

In reply to Re^2: How to implement Linked List by tilly
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 having an uproarious good time at the Monastery: (14)
    As of 2014-11-24 14:18 GMT
    Sections?
    Information?
    Find Nodes?
    Leftovers?
      Voting Booth?

      My preferred Perl binaries come from:














      Results (141 votes), past polls