Beefy Boxes and Bandwidth Generously Provided by pair Networks
Just another Perl shrine
 
PerlMonks  

Re: How to implement Linked List

by rinceWind (Monsignor)
on Dec 18, 2006 at 12:07 UTC ( #590417=note: print w/ replies, xml ) Need Help??


in reply to How to implement Linked List

Perl has built in fully dynamic data structures - arrays and hashes. These are in most cases sufficient for resident data needs. Can you explain why you need a linked list - is this a practical application or an academic exercise?

Here's something cobbled together - a class that will give you linked list:

package Data::LinkedList; sub new { my $pkg = shift; my %proto = @_; bless \%proto, $pkg; } sub data { my $self = shift; @_ ? ($self->{data} = shift) : $self->{data}; } sub next { my $self = shift; @_ ? ($self->{next} = shift) : $self->{next}; } 1; __END__ my $ele1 = Data::LinkedList->new( data => 'abel' ); my $ele2 = Data::LinkedList->new( data => 'baker', next => $ele1);

--

Oh Lord, won’t you burn me a Knoppix CD ?
My friends all rate Windows, I must disagree.
Your powers of persuasion will set them all free,
So oh Lord, won’t you burn me a Knoppix CD ?
(Missquoting Janis Joplin)


Comment on Re: How to implement Linked List
Download Code
Re^2: How to implement Linked List
by msk_0984 (Friar) on Dec 18, 2006 at 12:58 UTC
    Hi

        Felt good to see your prompt reply. Actually i was exploring Perl and thought of implementing the Linked list concept which might be Useful in for me in Future purposes........ So thought of trying it out

    Work Hard Party Harderrr!!
    Sushil Kumar

      As others have said, linked lists are never really necessary in Perl.

      See How do I handle linked lists? for an explanation (as well as a way to implement them).

      map{$a=1-$_/10;map{$d=$a;$e=$b=$_/20-2;map{($d,$e)=(2*$d*$e+$a,$e**2 -$d**2+$b);$c=$d**2+$e**2>4?$d=8:_}1..50;print$c}0..59;print$/}0..20
      Tom Melly, pm@tomandlu.co.uk
Re^2: How to implement Linked List
by tilly (Archbishop) on Dec 19, 2006 at 01:10 UTC
    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.

Log In?
Username:
Password:

What's my password?
Create A New User
Node Status?
node history
Node Type: note [id://590417]
help
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: (8)
As of 2014-09-16 23:06 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    How do you remember the number of days in each month?











    Results (51 votes), past polls