line |
stmt |
bran |
cond |
sub |
pod |
time |
code |
1
|
|
|
|
|
|
|
# A linked list data-structure. |
2
|
|
|
|
|
|
|
|
3
|
|
|
|
|
|
|
package DataStructure::LinkedList; |
4
|
|
|
|
|
|
|
|
5
|
1
|
|
|
1
|
|
73139
|
use strict; |
|
1
|
|
|
|
|
13
|
|
|
1
|
|
|
|
|
30
|
|
6
|
1
|
|
|
1
|
|
5
|
use warnings; |
|
1
|
|
|
|
|
2
|
|
|
1
|
|
|
|
|
25
|
|
7
|
1
|
|
|
1
|
|
17
|
use utf8; |
|
1
|
|
|
|
|
3
|
|
|
1
|
|
|
|
|
6
|
|
8
|
1
|
|
|
1
|
|
41
|
use feature ':5.24'; |
|
1
|
|
|
|
|
2
|
|
|
1
|
|
|
|
|
174
|
|
9
|
1
|
|
|
1
|
|
7
|
use feature 'signatures'; |
|
1
|
|
|
|
|
2
|
|
|
1
|
|
|
|
|
39
|
|
10
|
1
|
|
|
1
|
|
6
|
no warnings 'experimental::signatures'; |
|
1
|
|
|
|
|
2
|
|
|
1
|
|
|
|
|
43
|
|
11
|
|
|
|
|
|
|
|
12
|
1
|
|
|
1
|
|
488
|
use DataStructure::LinkedList::Node; |
|
1
|
|
|
|
|
2
|
|
|
1
|
|
|
|
|
602
|
|
13
|
|
|
|
|
|
|
|
14
|
|
|
|
|
|
|
=pod |
15
|
|
|
|
|
|
|
|
16
|
|
|
|
|
|
|
=head1 NAME |
17
|
|
|
|
|
|
|
|
18
|
|
|
|
|
|
|
DataStructure::LinkedList |
19
|
|
|
|
|
|
|
|
20
|
|
|
|
|
|
|
=head1 SYNOPSIS |
21
|
|
|
|
|
|
|
|
22
|
|
|
|
|
|
|
A linked list data-structure, written in pure Perl. |
23
|
|
|
|
|
|
|
|
24
|
|
|
|
|
|
|
See also L for a double-linked list version that |
25
|
|
|
|
|
|
|
offers a richer interface. |
26
|
|
|
|
|
|
|
|
27
|
|
|
|
|
|
|
=head1 DESCRIPTION |
28
|
|
|
|
|
|
|
|
29
|
|
|
|
|
|
|
=head2 CONSTRUCTOR |
30
|
|
|
|
|
|
|
|
31
|
|
|
|
|
|
|
Cnew()> |
32
|
|
|
|
|
|
|
|
33
|
|
|
|
|
|
|
Creates an empty list. |
34
|
|
|
|
|
|
|
|
35
|
|
|
|
|
|
|
=cut |
36
|
|
|
|
|
|
|
|
37
|
2
|
|
|
2
|
0
|
85046
|
sub new ($class) { |
|
2
|
|
|
|
|
6
|
|
|
2
|
|
|
|
|
4
|
|
38
|
2
|
|
|
|
|
12
|
return bless { size => 0, first => undef}, $class; |
39
|
|
|
|
|
|
|
} |
40
|
|
|
|
|
|
|
|
41
|
|
|
|
|
|
|
=pod |
42
|
|
|
|
|
|
|
|
43
|
|
|
|
|
|
|
=head2 METHODS |
44
|
|
|
|
|
|
|
|
45
|
|
|
|
|
|
|
All the functions below are class methods that should be called on a |
46
|
|
|
|
|
|
|
B object. Unless documented, they run in constant |
47
|
|
|
|
|
|
|
time. |
48
|
|
|
|
|
|
|
|
49
|
|
|
|
|
|
|
=head3 I |
50
|
|
|
|
|
|
|
|
51
|
|
|
|
|
|
|
Returns the first L of the list, or B if |
52
|
|
|
|
|
|
|
the list is empty. |
53
|
|
|
|
|
|
|
|
54
|
|
|
|
|
|
|
=cut |
55
|
|
|
|
|
|
|
|
56
|
12
|
|
|
12
|
1
|
21
|
sub first ($self) { |
|
12
|
|
|
|
|
16
|
|
|
12
|
|
|
|
|
17
|
|
57
|
12
|
|
|
|
|
32
|
return $self->{first}; |
58
|
|
|
|
|
|
|
} |
59
|
|
|
|
|
|
|
|
60
|
|
|
|
|
|
|
=pod |
61
|
|
|
|
|
|
|
|
62
|
|
|
|
|
|
|
=head3 I |
63
|
|
|
|
|
|
|
|
64
|
|
|
|
|
|
|
Adds a new node at the beginning of the list with the given value. Returns the |
65
|
|
|
|
|
|
|
newly added node. |
66
|
|
|
|
|
|
|
|
67
|
|
|
|
|
|
|
For conveniance, I can be used as a synonym of I. |
68
|
|
|
|
|
|
|
|
69
|
|
|
|
|
|
|
=cut |
70
|
|
|
|
|
|
|
|
71
|
4
|
|
|
4
|
1
|
11
|
sub unshift ($self, $value) { |
|
4
|
|
|
|
|
5
|
|
|
4
|
|
|
|
|
9
|
|
|
4
|
|
|
|
|
5
|
|
72
|
4
|
|
|
|
|
16
|
my $new_node = DataStructure::LinkedList::Node->new($self, $self->{first}, $value); |
73
|
4
|
|
|
|
|
8
|
$self->{first} = $new_node; |
74
|
4
|
|
|
|
|
6
|
$self->{size}++; |
75
|
4
|
|
|
|
|
8
|
return $new_node; |
76
|
|
|
|
|
|
|
} |
77
|
|
|
|
|
|
|
|
78
|
0
|
|
|
0
|
0
|
0
|
sub push ($self, $value) { |
|
0
|
|
|
|
|
0
|
|
|
0
|
|
|
|
|
0
|
|
|
0
|
|
|
|
|
0
|
|
79
|
0
|
|
|
|
|
0
|
return $self->unshift($value); |
80
|
|
|
|
|
|
|
} |
81
|
|
|
|
|
|
|
|
82
|
|
|
|
|
|
|
=pod |
83
|
|
|
|
|
|
|
|
84
|
|
|
|
|
|
|
=head3 I |
85
|
|
|
|
|
|
|
|
86
|
|
|
|
|
|
|
Removes the first node of the list and returns its value. Returns B if |
87
|
|
|
|
|
|
|
the list is empty. Note that the method can also return B if the first |
88
|
|
|
|
|
|
|
node’s value is B |
89
|
|
|
|
|
|
|
|
90
|
|
|
|
|
|
|
For conveniance, I can be used as a synonym of I. |
91
|
|
|
|
|
|
|
|
92
|
|
|
|
|
|
|
=cut |
93
|
|
|
|
|
|
|
|
94
|
4
|
|
|
4
|
1
|
7
|
sub shift ($self) { |
|
4
|
|
|
|
|
8
|
|
|
4
|
|
|
|
|
6
|
|
95
|
4
|
100
|
|
|
|
19
|
return unless defined $self->{first}; |
96
|
2
|
|
|
|
|
5
|
my $old_first = $self->first(); |
97
|
2
|
|
|
|
|
19
|
$self->{first} = $old_first->next(); |
98
|
2
|
|
|
|
|
7
|
return $old_first->_delete_first(); |
99
|
|
|
|
|
|
|
} |
100
|
|
|
|
|
|
|
|
101
|
1
|
|
|
1
|
0
|
3
|
sub pop ($self) { |
|
1
|
|
|
|
|
2
|
|
|
1
|
|
|
|
|
2
|
|
102
|
1
|
|
|
|
|
3
|
$self->shift(); |
103
|
|
|
|
|
|
|
} |
104
|
|
|
|
|
|
|
|
105
|
|
|
|
|
|
|
=pod |
106
|
|
|
|
|
|
|
|
107
|
|
|
|
|
|
|
=head3 I |
108
|
|
|
|
|
|
|
|
109
|
|
|
|
|
|
|
Returns the number of nodes in the list. |
110
|
|
|
|
|
|
|
|
111
|
|
|
|
|
|
|
=cut |
112
|
|
|
|
|
|
|
|
113
|
14
|
|
|
14
|
1
|
24
|
sub size ($self) { |
|
14
|
|
|
|
|
24
|
|
|
14
|
|
|
|
|
16
|
|
114
|
14
|
|
|
|
|
59
|
return $self->{size}; |
115
|
|
|
|
|
|
|
} |
116
|
|
|
|
|
|
|
|
117
|
|
|
|
|
|
|
=pod |
118
|
|
|
|
|
|
|
|
119
|
|
|
|
|
|
|
=head3 I |
120
|
|
|
|
|
|
|
|
121
|
|
|
|
|
|
|
Returns whether the list is empty. |
122
|
|
|
|
|
|
|
|
123
|
|
|
|
|
|
|
=cut |
124
|
|
|
|
|
|
|
|
125
|
3
|
|
|
3
|
1
|
7
|
sub empty ($self) { |
|
3
|
|
|
|
|
6
|
|
|
3
|
|
|
|
|
3
|
|
126
|
3
|
|
|
|
|
8
|
return $self->size() == 0; |
127
|
|
|
|
|
|
|
} |
128
|
|
|
|
|
|
|
|
129
|
|
|
|
|
|
|
=pod |
130
|
|
|
|
|
|
|
|
131
|
|
|
|
|
|
|
=head3 I |
132
|
|
|
|
|
|
|
|
133
|
|
|
|
|
|
|
Returns all the values of the list, as a normal Perl list. This runs in linear |
134
|
|
|
|
|
|
|
time with the size of the list. |
135
|
|
|
|
|
|
|
|
136
|
|
|
|
|
|
|
=cut |
137
|
|
|
|
|
|
|
|
138
|
5
|
|
|
5
|
1
|
578
|
sub values ($self) { |
|
5
|
|
|
|
|
11
|
|
|
5
|
|
|
|
|
5
|
|
139
|
5
|
50
|
|
|
|
19
|
return $self->size() unless wantarray; |
140
|
5
|
|
|
|
|
16
|
my @ret = (0) x $self->size(); |
141
|
5
|
|
|
|
|
8
|
my $i = 0; |
142
|
5
|
|
|
|
|
13
|
my $cur = $self->first(); |
143
|
5
|
|
|
|
|
14
|
while (defined $cur) { |
144
|
4
|
|
|
|
|
11
|
$ret[$i++] = $cur->value(); |
145
|
4
|
|
|
|
|
9
|
$cur = $cur->next(); |
146
|
|
|
|
|
|
|
} |
147
|
5
|
|
|
|
|
27
|
return @ret; |
148
|
|
|
|
|
|
|
} |
149
|
|
|
|
|
|
|
|
150
|
2
|
|
|
2
|
|
1286
|
sub DESTROY ($self) { |
|
2
|
|
|
|
|
4
|
|
|
2
|
|
|
|
|
4
|
|
151
|
2
|
|
|
|
|
4
|
my $next = $self->{first}; |
152
|
2
|
|
|
|
|
6
|
while (defined $next) { |
153
|
2
|
|
|
|
|
4
|
my $cur = $next; |
154
|
2
|
|
|
|
|
3
|
$next = $cur->{next}; |
155
|
2
|
|
|
|
|
5
|
undef %{$cur}; |
|
2
|
|
|
|
|
6
|
|
156
|
|
|
|
|
|
|
} |
157
|
2
|
|
|
|
|
142
|
return; |
158
|
|
|
|
|
|
|
} |
159
|
|
|
|
|
|
|
|
160
|
|
|
|
|
|
|
=pod |
161
|
|
|
|
|
|
|
|
162
|
|
|
|
|
|
|
=head1 AUTHOR |
163
|
|
|
|
|
|
|
|
164
|
|
|
|
|
|
|
Mathias Kende |
165
|
|
|
|
|
|
|
|
166
|
|
|
|
|
|
|
=head1 LICENCE |
167
|
|
|
|
|
|
|
|
168
|
|
|
|
|
|
|
Copyright 2021 Mathias Kende |
169
|
|
|
|
|
|
|
|
170
|
|
|
|
|
|
|
This program is distributed under the MIT (X11) License: |
171
|
|
|
|
|
|
|
L |
172
|
|
|
|
|
|
|
|
173
|
|
|
|
|
|
|
Permission is hereby granted, free of charge, to any person |
174
|
|
|
|
|
|
|
obtaining a copy of this software and associated documentation |
175
|
|
|
|
|
|
|
files (the "Software"), to deal in the Software without |
176
|
|
|
|
|
|
|
restriction, including without limitation the rights to use, |
177
|
|
|
|
|
|
|
copy, modify, merge, publish, distribute, sublicense, and/or sell |
178
|
|
|
|
|
|
|
copies of the Software, and to permit persons to whom the |
179
|
|
|
|
|
|
|
Software is furnished to do so, subject to the following |
180
|
|
|
|
|
|
|
conditions: |
181
|
|
|
|
|
|
|
|
182
|
|
|
|
|
|
|
The above copyright notice and this permission notice shall be |
183
|
|
|
|
|
|
|
included in all copies or substantial portions of the Software. |
184
|
|
|
|
|
|
|
|
185
|
|
|
|
|
|
|
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, |
186
|
|
|
|
|
|
|
EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES |
187
|
|
|
|
|
|
|
OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND |
188
|
|
|
|
|
|
|
NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT |
189
|
|
|
|
|
|
|
HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, |
190
|
|
|
|
|
|
|
WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING |
191
|
|
|
|
|
|
|
FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR |
192
|
|
|
|
|
|
|
OTHER DEALINGS IN THE SOFTWARE. |
193
|
|
|
|
|
|
|
|
194
|
|
|
|
|
|
|
=head1 SEE ALSO |
195
|
|
|
|
|
|
|
|
196
|
|
|
|
|
|
|
L |
197
|
|
|
|
|
|
|
|
198
|
|
|
|
|
|
|
=cut |
199
|
|
|
|
|
|
|
|
200
|
|
|
|
|
|
|
1; |