Beefy Boxes and Bandwidth Generously Provided by pair Networks
laziness, impatience, and hubris
 
PerlMonks  

Re: How to implement Linked List

by rinceWind (Monsignor)
on Dec 18, 2006 at 12:07 UTC ( [id://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)

Replies are listed 'Best First'.
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.

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

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: note [id://590417]
help
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others avoiding work at the Monastery: (4)
As of 2024-03-29 09:37 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found