blob: ff908f0ae2f06fd2c6cb95cdca8e6971df45649f (
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
43
44
45
46
47
48
49
50
|
package Stack;
## The stack is just stored as a blessed array.
#
# Methods:
# ->push( $value ) -- push value on top of stack (return stack itself)
# ->pop( $value ) -- take value off top of stack & return it
# (if stack has no entries return undef/empty value)
# ->top( $value ) -- returns the value at the top of the stack
# (if stack has no entries return undef/empty value)
# ->min( $value ) -- returns the smallest value in the stack
# (if stack has no entries return undef/empty value)
sub new {
my $class = shift;
my $self = [];
bless $self, $class;
return $self;
}
sub push {
my( $self,$val ) = @_;
CORE::push @{$self}, $val; ## Make clear we are using the core push method
return $self;
}
sub pop {
my $self = shift;
return unless @{$self};
return CORE::pop(@{$self}); ## Make clear we are using the core pop method
}
sub top {
my $self = shift;
return unless @{$self};
return $self->[-1];
}
sub min {
my $self = shift;
return unless @{$self};
my $min = $self->[0];
foreach (@{$self}) {
$min = $_ if $_ < $min;
}
return $min;
}
1;
|