Beefy Boxes and Bandwidth Generously Provided by pair Networks
Pathologically Eclectic Rubbish Lister
 
PerlMonks  

Comment on

( #3333=superdoc: print w/ replies, xml ) Need Help??
Linked list is doable in Perl, and in general, linked list has its own benefits under certain context. You are not the first person talking about this, and you are not alone. I read about this topic a while ago, in couple of published Perl books.

Here is a quick OO solution I put together. It is only to prove the concept, and it obviously does not implement all the needed methods. The test case I given below worked, but there is no other test case being tried.

The solution is OO. In DoubleLinkedList.pm, the process_thru_forward and process_thru_backward functions allow you to specify callback functions and parameters to callback functions, and they are very flexible.
DoubleLinkedListElement.pm: package DoubleLinkedListElement; use Hash::Util (lock_keys); use strict; sub new { my $self = {}; $self->{VALUE} = undef; $self->{NEXT} = undef; $self->{PREV} = undef; bless $self; lock_keys(%$self); return $self; } sub value { my $self = shift; if (@_) { $self->{VALUE} = shift; } return $self->{VALUE}; } sub next { my $self = shift; my $ret = $self->{NEXT}; if (@_) { $self->{NEXT} = shift; } return $ret; } sub prev { my $self = shift; my $ret = $self->{PREV}; if (@_) { $self->{PREV} = shift; } return $ret; } 1; DoubleLinkedList.pm: package DoubleLinkedList; use DoubleLinkedListElement; use Hash::Util (lock_keys); use strict; sub new { my $self = {}; $self->{FIRST} = undef; $self->{LAST} = undef; bless $self; lock_keys(%$self); return $self; } sub first { my $self = shift; return $self->{FIRST}; } sub last { my $self = shift; return $self->{LAST}; } sub append { my ($self, $value) = @_; my $element = new DoubleLinkedListElement(); $element->value($value); if ($self->last()) { $self->last()->next($element); $element->prev($self->last()); $self->{LAST} = $element; } else { $self->{FIRST} = $element; $self->{LAST} = $element; } } sub process_thru_forward { my ($self, $func, @param) = @_; for (my $this = $self->first(); $this; $this = $this->next()) { &$func($this, @param); } } sub process_thru_backward { my ($self, $func, @param) = @_; for (my $this = $self->last(); $this; $this = $this->prev()) { &$func($this, @param); } } 1; test.pl: use Data::Dumper; use DoubleLinkedList; use strict; my $dl_list = new DoubleLinkedList; foreach (1..10) { $dl_list->append($_); } $dl_list->process_thru_forward(\&plus_and_display, 10); $dl_list->process_thru_backward(\&plus_and_display, 10); sub plus_and_display { my ($this, $adj) = @_; print $this->value + $adj, "\n"; }

In reply to Re: Efficient Linked Lists by pg
in thread Efficient Linked Lists by Ionizor

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 surveying the Monastery: (10)
    As of 2014-08-23 03:03 GMT
    Sections?
    Information?
    Find Nodes?
    Leftovers?
      Voting Booth?

      The best computer themed movie is:











      Results (171 votes), past polls