line |
stmt |
bran |
cond |
sub |
pod |
time |
code |
1
|
|
|
|
|
|
|
# A node of a linked list. |
2
|
|
|
|
|
|
|
|
3
|
|
|
|
|
|
|
package DataStructure::LinkedList::Node; |
4
|
|
|
|
|
|
|
|
5
|
1
|
|
|
1
|
|
7
|
use strict; |
|
1
|
|
|
|
|
2
|
|
|
1
|
|
|
|
|
30
|
|
6
|
1
|
|
|
1
|
|
5
|
use warnings; |
|
1
|
|
|
|
|
2
|
|
|
1
|
|
|
|
|
22
|
|
7
|
1
|
|
|
1
|
|
5
|
use utf8; |
|
1
|
|
|
|
|
3
|
|
|
1
|
|
|
|
|
4
|
|
8
|
1
|
|
|
1
|
|
24
|
use feature ':5.24'; |
|
1
|
|
|
|
|
2
|
|
|
1
|
|
|
|
|
76
|
|
9
|
1
|
|
|
1
|
|
7
|
use feature 'signatures'; |
|
1
|
|
|
|
|
2
|
|
|
1
|
|
|
|
|
26
|
|
10
|
1
|
|
|
1
|
|
6
|
no warnings 'experimental::signatures'; |
|
1
|
|
|
|
|
2
|
|
|
1
|
|
|
|
|
45
|
|
11
|
|
|
|
|
|
|
|
12
|
1
|
|
|
1
|
|
7
|
use Scalar::Util qw(weaken); |
|
1
|
|
|
|
|
2
|
|
|
1
|
|
|
|
|
414
|
|
13
|
|
|
|
|
|
|
|
14
|
|
|
|
|
|
|
=pod |
15
|
|
|
|
|
|
|
|
16
|
|
|
|
|
|
|
=head1 NAME |
17
|
|
|
|
|
|
|
|
18
|
|
|
|
|
|
|
DataStructure::LinkedList::Node |
19
|
|
|
|
|
|
|
|
20
|
|
|
|
|
|
|
=head1 SYNOPSIS |
21
|
|
|
|
|
|
|
|
22
|
|
|
|
|
|
|
A single node (element) in a L. |
23
|
|
|
|
|
|
|
|
24
|
|
|
|
|
|
|
=head1 DESCRIPTION |
25
|
|
|
|
|
|
|
|
26
|
|
|
|
|
|
|
=head2 CONSTRUCTOR |
27
|
|
|
|
|
|
|
|
28
|
|
|
|
|
|
|
You can’t build a node directly. Instead you can call one of the accessors of a |
29
|
|
|
|
|
|
|
L or some of the methods below. |
30
|
|
|
|
|
|
|
|
31
|
|
|
|
|
|
|
Note that a B does not hold a reference on its |
32
|
|
|
|
|
|
|
parent list. So a node becomes invalid when the last reference to its list is |
33
|
|
|
|
|
|
|
deleted as the list itself will be destroyed. But you should also not depend on |
34
|
|
|
|
|
|
|
this behavior as it might be fixed in the future. |
35
|
|
|
|
|
|
|
|
36
|
|
|
|
|
|
|
=cut |
37
|
|
|
|
|
|
|
|
38
|
|
|
|
|
|
|
# The constructor is private and should be called only by this package and its |
39
|
|
|
|
|
|
|
# parent. |
40
|
12
|
|
|
12
|
0
|
22
|
sub new ($class, $list, $next, $value) { |
|
12
|
|
|
|
|
16
|
|
|
12
|
|
|
|
|
51
|
|
|
12
|
|
|
|
|
38
|
|
|
12
|
|
|
|
|
20
|
|
|
12
|
|
|
|
|
17
|
|
41
|
12
|
|
|
|
|
46
|
my $self = bless { |
42
|
|
|
|
|
|
|
list => $list, |
43
|
|
|
|
|
|
|
next => $next, |
44
|
|
|
|
|
|
|
value => $value, |
45
|
|
|
|
|
|
|
}, $class; |
46
|
12
|
|
|
|
|
50
|
weaken($self->{list}); |
47
|
12
|
|
|
|
|
28
|
return $self; |
48
|
|
|
|
|
|
|
} |
49
|
|
|
|
|
|
|
|
50
|
|
|
|
|
|
|
=pod |
51
|
|
|
|
|
|
|
|
52
|
|
|
|
|
|
|
=head2 METHODS |
53
|
|
|
|
|
|
|
|
54
|
|
|
|
|
|
|
All the functions below are class methods that should be called on a |
55
|
|
|
|
|
|
|
C object. |
56
|
|
|
|
|
|
|
|
57
|
|
|
|
|
|
|
=over 4 |
58
|
|
|
|
|
|
|
|
59
|
|
|
|
|
|
|
=item value() |
60
|
|
|
|
|
|
|
|
61
|
|
|
|
|
|
|
Returns the value held by this node. |
62
|
|
|
|
|
|
|
|
63
|
|
|
|
|
|
|
=cut |
64
|
|
|
|
|
|
|
|
65
|
28
|
|
|
28
|
1
|
1105
|
sub value ($self) { |
|
28
|
|
|
|
|
47
|
|
|
28
|
|
|
|
|
33
|
|
66
|
28
|
|
|
|
|
84
|
return $self->{value}; |
67
|
|
|
|
|
|
|
} |
68
|
|
|
|
|
|
|
|
69
|
|
|
|
|
|
|
=pod |
70
|
|
|
|
|
|
|
|
71
|
|
|
|
|
|
|
=item next() |
72
|
|
|
|
|
|
|
|
73
|
|
|
|
|
|
|
Returns the next B in this list or B |
74
|
|
|
|
|
|
|
if the current object is the last node in its list. |
75
|
|
|
|
|
|
|
|
76
|
|
|
|
|
|
|
The current node can still be used after that call. |
77
|
|
|
|
|
|
|
|
78
|
|
|
|
|
|
|
=cut |
79
|
|
|
|
|
|
|
|
80
|
34
|
|
|
34
|
1
|
386
|
sub next ($self) { |
|
34
|
|
|
|
|
47
|
|
|
34
|
|
|
|
|
46
|
|
81
|
34
|
|
|
|
|
95
|
return $self->{next}; |
82
|
|
|
|
|
|
|
} |
83
|
|
|
|
|
|
|
|
84
|
|
|
|
|
|
|
=pod |
85
|
|
|
|
|
|
|
|
86
|
|
|
|
|
|
|
=item insert_after($value) |
87
|
|
|
|
|
|
|
|
88
|
|
|
|
|
|
|
Inserts a new node in the list after the current one, with the given value and |
89
|
|
|
|
|
|
|
returns that new node. |
90
|
|
|
|
|
|
|
|
91
|
|
|
|
|
|
|
The current node can still be used after that call. |
92
|
|
|
|
|
|
|
|
93
|
|
|
|
|
|
|
=back |
94
|
|
|
|
|
|
|
|
95
|
|
|
|
|
|
|
=cut |
96
|
|
|
|
|
|
|
|
97
|
0
|
|
|
0
|
1
|
0
|
sub insert_after ($self, $value) { |
|
0
|
|
|
|
|
0
|
|
|
0
|
|
|
|
|
0
|
|
|
0
|
|
|
|
|
0
|
|
98
|
0
|
|
|
|
|
0
|
my $new_node = new(ref $self, $self->{list}, $self->{next}, $value); |
99
|
0
|
|
|
|
|
0
|
$self->{next} = $new_node; |
100
|
0
|
0
|
|
|
|
0
|
$self->{list}{last} = $new_node unless defined $new_node->{next}; |
101
|
0
|
|
|
|
|
0
|
$self->{list}{size}++; |
102
|
0
|
|
|
|
|
0
|
return $new_node; |
103
|
|
|
|
|
|
|
} |
104
|
|
|
|
|
|
|
|
105
|
|
|
|
|
|
|
# Delete the node, assuming that it is the first node of the list. |
106
|
8
|
|
|
8
|
|
12
|
sub _delete_first ($self) { |
|
8
|
|
|
|
|
12
|
|
|
8
|
|
|
|
|
12
|
|
107
|
8
|
|
|
|
|
19
|
my $value = $self->{value}; |
108
|
8
|
|
|
|
|
11
|
$self->{list}{size}--; |
109
|
8
|
|
|
|
|
12
|
undef %{$self}; |
|
8
|
|
|
|
|
23
|
|
110
|
8
|
|
|
|
|
42
|
return $value; |
111
|
|
|
|
|
|
|
} |
112
|
|
|
|
|
|
|
|
113
|
|
|
|
|
|
|
1; |