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.139 tracks/perl5/exercises/linked-list/.meta/solutions/Deque.pm
trackler-2.2.1.138 tracks/perl5/exercises/linked-list/.meta/solutions/Deque.pm
trackler-2.2.1.137 tracks/perl5/exercises/linked-list/.meta/solutions/Deque.pm
trackler-2.2.1.136 tracks/perl5/exercises/linked-list/.meta/solutions/Deque.pm
trackler-2.2.1.135 tracks/perl5/exercises/linked-list/.meta/solutions/Deque.pm
trackler-2.2.1.134 tracks/perl5/exercises/linked-list/.meta/solutions/Deque.pm
trackler-2.2.1.133 tracks/perl5/exercises/linked-list/.meta/solutions/Deque.pm
trackler-2.2.1.132 tracks/perl5/exercises/linked-list/.meta/solutions/Deque.pm
trackler-2.2.1.131 tracks/perl5/exercises/linked-list/.meta/solutions/Deque.pm
trackler-2.2.1.130 tracks/perl5/exercises/linked-list/.meta/solutions/Deque.pm
trackler-2.2.1.129 tracks/perl5/exercises/linked-list/.meta/solutions/Deque.pm
trackler-2.2.1.128 tracks/perl5/exercises/linked-list/.meta/solutions/Deque.pm
trackler-2.2.1.127 tracks/perl5/exercises/linked-list/.meta/solutions/Deque.pm
trackler-2.2.1.126 tracks/perl5/exercises/linked-list/.meta/solutions/Deque.pm
trackler-2.2.1.125 tracks/perl5/exercises/linked-list/.meta/solutions/Deque.pm
trackler-2.2.1.124 tracks/perl5/exercises/linked-list/.meta/solutions/Deque.pm
trackler-2.2.1.123 tracks/perl5/exercises/linked-list/.meta/solutions/Deque.pm
trackler-2.2.1.122 tracks/perl5/exercises/linked-list/.meta/solutions/Deque.pm
trackler-2.2.1.121 tracks/perl5/exercises/linked-list/.meta/solutions/Deque.pm
trackler-2.2.1.120 tracks/perl5/exercises/linked-list/.meta/solutions/Deque.pm