blob: 8d522184646c99ffb6baccc82475bf4057ea141b (
plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
|
package LL;
# The linked list object consists of 3 values;
# * 'val' => the value of the node;
# * 'next' => the next node in the list;
# * 'last' => the last node in the list; {so can easily add next value}
# Two methods:
# * ->add( $val ) => Add another value to the end of the list
# * ->flatten => Flatten to array of values
sub new {
my $class = shift;
my $self = { 'val' => shift, 'next' => undef };
$self->{'last'} = $self;
bless $self, $class;
}
sub next {
my $self = shift;
$self = $self->{'next'};
}
sub val {
my $self = shift;
return $self->{'val'};
}
sub add {
my( $self, $val ) =@_;
my $new = LL->new( $val );
$self->{'last'}{'next'} = $new;
$self->{'last'} = $new;
return $self;
}
sub flatten {
my $self = shift;
return $self->{'val'} unless $self->{'next'};
return ( $self->{'val'}, $self->{'next'}->flatten );
}
1;
|