Sha256: 79698302f0bf49dde1f608682af2d6628c6bf54894a97318e4018720f90b811a

Contents?: true

Size: 661 Bytes

Versions: 73

Compression:

Stored size: 661 Bytes

Contents

package LinkedList;

use strict;
use warnings;

sub new {
    my $class = shift;
    bless {data => shift, next => shift}, $class;
}

sub next :method { $_[0]->{next} }
sub data         { $_[0]->{data} }

sub from_array {
    my( $pkg, $array ) = @_;

    my $head;
    map { $head = __PACKAGE__->new($_, $head) } reverse @$array;
    return $head;
}

sub to_array {
    my $self = shift;
    
    my @array;
    do { push @array, $self->data() } while( $self = $self->next() );
    return \@array;
}

sub reverse :method {
    my $self = shift;

    my $array = $self->to_array();
    @$array = reverse @$array;

    __PACKAGE__->from_array( $array  );
}

1;

Version data entries

73 entries across 73 versions & 1 rubygems

Version Path
trackler-2.2.1.180 tracks/perl5/exercises/simple-linked-list/.meta/solutions/LinkedList.pm
trackler-2.2.1.179 tracks/perl5/exercises/simple-linked-list/.meta/solutions/LinkedList.pm
trackler-2.2.1.178 tracks/perl5/exercises/simple-linked-list/.meta/solutions/LinkedList.pm
trackler-2.2.1.177 tracks/perl5/exercises/simple-linked-list/.meta/solutions/LinkedList.pm
trackler-2.2.1.176 tracks/perl5/exercises/simple-linked-list/.meta/solutions/LinkedList.pm
trackler-2.2.1.175 tracks/perl5/exercises/simple-linked-list/.meta/solutions/LinkedList.pm
trackler-2.2.1.174 tracks/perl5/exercises/simple-linked-list/.meta/solutions/LinkedList.pm
trackler-2.2.1.173 tracks/perl5/exercises/simple-linked-list/.meta/solutions/LinkedList.pm
trackler-2.2.1.172 tracks/perl5/exercises/simple-linked-list/.meta/solutions/LinkedList.pm
trackler-2.2.1.171 tracks/perl5/exercises/simple-linked-list/.meta/solutions/LinkedList.pm
trackler-2.2.1.170 tracks/perl5/exercises/simple-linked-list/.meta/solutions/LinkedList.pm
trackler-2.2.1.169 tracks/perl5/exercises/simple-linked-list/.meta/solutions/LinkedList.pm
trackler-2.2.1.167 tracks/perl5/exercises/simple-linked-list/.meta/solutions/LinkedList.pm
trackler-2.2.1.166 tracks/perl5/exercises/simple-linked-list/.meta/solutions/LinkedList.pm
trackler-2.2.1.165 tracks/perl5/exercises/simple-linked-list/.meta/solutions/LinkedList.pm
trackler-2.2.1.164 tracks/perl5/exercises/simple-linked-list/.meta/solutions/LinkedList.pm
trackler-2.2.1.163 tracks/perl5/exercises/simple-linked-list/.meta/solutions/LinkedList.pm
trackler-2.2.1.162 tracks/perl5/exercises/simple-linked-list/.meta/solutions/LinkedList.pm
trackler-2.2.1.161 tracks/perl5/exercises/simple-linked-list/.meta/solutions/LinkedList.pm
trackler-2.2.1.160 tracks/perl5/exercises/simple-linked-list/.meta/solutions/LinkedList.pm