Sha256: 09642cff348aeb01d037a8d94907f8612ae19181320c041189b0c3900e84faa7

Contents?: true

Size: 1.36 KB

Versions: 73

Compression:

Stored size: 1.36 KB

Contents

package Deque;
use strict;
use warnings;

sub new {
   my ($class) = @_;
   return bless {}, $class;
}

sub push {
    my ($self, $value) = @_;

    my $element = Element->new(
       value   => $value,
       prev_e  => $self->{last_e},
       next_e  => undef,
   );
   if ($self->{last_e}) {
      $self->{last_e}{next_e} = $element;
   }
   $self->{last_e} = $element;
   if (not $self->{first_e}) {
       $self->{first_e} = $element;
   }

   return;
}

sub pop {
    my ($self) = @_;

    my $element = $self->{last_e};
    die 'List is empty' if not $element;

    $self->{last_e} = $element->{prev_e};
	$self->{last_e}{next_e} = undef;

    return $element->{value};
}

sub shift {
    my ($self) = @_;

    my $element = $self->{first_e};
    die 'List is empty' if not $element;

    $self->{first_e} = $element->{next_e};
    $self->{first_e}{prev_e} = undef;

    return $element->{value};

}

sub unshift {
    my ($self, $value) = @_;

    my $element = Element->new(
       value   => $value,
       prev_e  => undef,
       next_e  => $self->{first_e},
   );
   if ($self->{first_e}) {
      $self->{first_e}{prev_e} = $element;
   }
   $self->{first_e} = $element;
   if (not $self->{last_e}) {
       $self->{last_e} = $element;
   }
   return;
}


package Element;
use strict;
use warnings;

sub new {
   my ($class, %data) = @_;
   return bless \%data, $class;
}

1;

Version data entries

73 entries across 73 versions & 1 rubygems

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