Beefy Boxes and Bandwidth Generously Provided by pair Networks
Perl-Sensitive Sunglasses

Re^2: How to implement Linked List

by tilly (Archbishop)
on Dec 19, 2006 at 01:10 UTC ( #590577=note: print w/replies, xml ) Need Help??

in reply to Re: How to implement Linked List
in thread How to implement Linked List

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"); }

Log In?

What's my password?
Create A New User
Node Status?
node history
Node Type: note [id://590577]
[erix]: they should lean to gnash their teeth a bit instead of writing more ^Re:NN-nodes :)
[erix]: *learn
[erix]: comic relief: how to start a riot and a fortune at the same time
[beech]: :) thats too generous to jdporter, don't start none won't be none -- there is about two decades (rounding up) worth of "ineffective" node titles -- leave the pope alone :p
[erix]: infallibility breeds inquisition

How do I use this? | Other CB clients
Other Users?
Others browsing the Monastery: (5)
As of 2017-01-17 23:50 GMT
Find Nodes?
    Voting Booth?
    Do you watch meteor showers?

    Results (159 votes). Check out past polls.